Skip to content

Commit

Permalink
add helper-lilypad
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Aug 29, 2017
1 parent e4f9089 commit bba16a7
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ A utility to reduce boilerplate code in Bukkit plugins. It gets boring writing t
##### [`helper-sql`](https://github.com/lucko/helper/tree/master/helper-sql): Provides SQL datasources using HikariCP.
[![Artifact](https://img.shields.io/badge/build-artifact-green.svg)](https://ci.lucko.me/job/helper/lastSuccessfulBuild/artifact/helper-sql/target/helper-sql.jar) [![Dependency Info](https://img.shields.io/badge/api-dependency_info-orange.svg)](#helper-sql) [![JavaDoc](https://img.shields.io/badge/api-javadoc-blue.svg)](https://lucko.me/helper-sql/)

##### [`helper-redis`](https://github.com/lucko/helper/tree/master/helper-redis): Provides Redis clients and implements the helper Messaging interface using Jedis.
##### [`helper-redis`](https://github.com/lucko/helper/tree/master/helper-redis): Provides Redis clients and implements the helper Messaging system using Jedis.
[![Artifact](https://img.shields.io/badge/build-artifact-green.svg)](https://ci.lucko.me/job/helper/lastSuccessfulBuild/artifact/helper-redis/target/helper-redis.jar) [![Dependency Info](https://img.shields.io/badge/api-dependency_info-orange.svg)](#helper-redis) [![JavaDoc](https://img.shields.io/badge/api-javadoc-blue.svg)](https://lucko.me/helper-redis/)

##### [`helper-lilypad`](https://github.com/lucko/helper/tree/master/helper-lilypad): Implements the helper Messaging system using LilyPad.
[![Artifact](https://img.shields.io/badge/build-artifact-green.svg)](https://ci.lucko.me/job/helper/lastSuccessfulBuild/artifact/helper-lilypad/target/helper-lilypad.jar)

## Feature Overview

* [`Events`](#events) - functional event handling and flexible listener registration
Expand Down
78 changes: 78 additions & 0 deletions helper-lilypad/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>helper-parent</artifactId>
<groupId>me.lucko</groupId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>helper-lilypad</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>

<name>helper-lilypad</name>
<description>Implements the helper Messaging system using LilyPad.</description>
<url>https://github.com/lucko/helper</url>

<build>
<defaultGoal>clean package</defaultGoal>
<finalName>${project.name}</finalName>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*.yml</include>
</includes>
</resource>
<resource>
<directory>../</directory>
<filtering>false</filtering>
<includes>
<include>LICENSE.txt</include>
</includes>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- helper related dependencies -->
<dependency>
<groupId>me.lucko</groupId>
<artifactId>helper</artifactId>
<version>2.0.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>${bukkit.version}</version>
<scope>provided</scope>
</dependency>

<!-- helper-lilypad related dependencies -->
<dependency>
<groupId>lilypad.client.connect</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* This file is part of helper, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.lucko.helper.lilypad;

import me.lucko.helper.messaging.InstanceData;
import me.lucko.helper.messaging.Messenger;

import org.bukkit.entity.Player;

import lilypad.client.connect.api.Connect;

import javax.annotation.Nonnull;

/**
* Represents a hook with LilyPad {@link Connect}.
*/
public interface HelperLilyPad extends Messenger, InstanceData {

/**
* Gets the JedisPool instance backing the redis instance
*
* @return the JedisPool instance
*/
@Nonnull
Connect getLilyPadConnect();

/**
* Requests that a certain player is moved to the given server.
*
* @param serverId the id of the server
* @param playerUsername the username of the player
*/
void redirectPlayer(@Nonnull String serverId, @Nonnull String playerUsername);

/**
* Requests that a certain player is moved to the given server.
*
* @param serverId the id of the server
* @param player the player
*/
default void redirectPlayer(@Nonnull String serverId, @Nonnull Player player) {
redirectPlayer(serverId, player.getName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of helper, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.lucko.helper.lilypad.plugin;

import me.lucko.helper.lilypad.HelperLilyPad;
import me.lucko.helper.messaging.InstanceData;
import me.lucko.helper.messaging.Messenger;
import me.lucko.helper.plugin.ExtendedJavaPlugin;

import lilypad.client.connect.api.Connect;

public class LilyPadPlugin extends ExtendedJavaPlugin {

@Override
protected void enable() {
Connect connect = getService(Connect.class);
if (connect == null) {
throw new RuntimeException("Connect not registered.");
}

HelperLilyPad globalLilyPad = new LilyPadWrapper(connect);

// expose all instances as services.
provideService(HelperLilyPad.class, globalLilyPad);
provideService(Messenger.class, globalLilyPad);
provideService(InstanceData.class, globalLilyPad);

getLogger().info("Hooked with LilyPad-Connect");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* This file is part of helper, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.lucko.helper.lilypad.plugin;

import com.google.common.reflect.TypeToken;

import me.lucko.helper.lilypad.HelperLilyPad;
import me.lucko.helper.messaging.AbstractMessenger;
import me.lucko.helper.messaging.Channel;

import lilypad.client.connect.api.Connect;
import lilypad.client.connect.api.event.EventListener;
import lilypad.client.connect.api.event.MessageEvent;
import lilypad.client.connect.api.request.RequestException;
import lilypad.client.connect.api.request.impl.MessageRequest;
import lilypad.client.connect.api.request.impl.RedirectRequest;

import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.annotation.Nonnull;

public class LilyPadWrapper implements HelperLilyPad {

private final Connect connect;
private final AbstractMessenger messenger;
private final AtomicBoolean listening = new AtomicBoolean(false);

public LilyPadWrapper(@Nonnull Connect connect) {
this.connect = connect;

messenger = new AbstractMessenger(
(channel, message) -> {
try {
connect.request(new MessageRequest(Collections.emptyList(), channel, message));
} catch (RequestException | UnsupportedEncodingException e) {
e.printStackTrace();
}
},
channel -> {
if (listening.getAndSet(true)) {
return;
}
try {
connect.registerEvents(this);
} catch (Exception e) {
listening.set(false);
}
},
channel -> {}
);
}

@EventListener
public void onMessage(MessageEvent event) {
String channel = event.getChannel();
String message;
try {
message = event.getMessageAsString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return;
}

messenger.registerIncomingMessage(channel, message);
}

@Override
public void redirectPlayer(@Nonnull String serverId, @Nonnull String playerUsername) {
try {
connect.request(new RedirectRequest(serverId, playerUsername));
} catch (RequestException e) {
e.printStackTrace();
}
}

@Nonnull
@Override
public Connect getLilyPadConnect() {
return connect;
}

@Nonnull
@Override
public String getId() {
return connect.getSettings().getUsername();
}

@Nonnull
@Override
public Set<String> getGroups() {
return Collections.emptySet();
}

@Nonnull
@Override
public <T> Channel<T> getChannel(@Nonnull String name, @Nonnull TypeToken<T> type) {
return messenger.getChannel(name, type);
}

}
8 changes: 8 additions & 0 deletions helper-lilypad/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: helper-lilypad
author: Luck
version: ${project.version}
description: ${project.description}
main: me.lucko.helper.lilypad.plugin.LilyPadPlugin
website: https://github.com/lucko/helper
load: STARTUP
depend: [helper, LilyPad-Connect]
2 changes: 1 addition & 1 deletion helper-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<version>1.0.3</version>

<name>helper-redis</name>
<description>Provides Redis clients and implements the helper Messaging interface using Jedis.</description>
<description>Provides Redis clients and implements the helper Messaging system using Jedis.</description>
<url>https://github.com/lucko/helper</url>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.function.Consumer;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* An abstract implementation of {@link Messenger}.
Expand Down Expand Up @@ -190,6 +191,7 @@ public CompletableFuture<Boolean> sendMessage(T message) {
}

private static class AbstractChannelAgent<T> implements ChannelAgent<T> {
@Nullable
private AbstractChannel<T> channel;
private final Set<ChannelListener<T>> listeners = ConcurrentHashMap.newKeySet();

Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<module>helper</module>
<module>helper-sql</module>
<module>helper-redis</module>
<module>helper-lilypad</module>
</modules>

<name>helper-parent</name>
Expand Down

0 comments on commit bba16a7

Please sign in to comment.