forked from lucko/helper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
341 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
68 changes: 68 additions & 0 deletions
68
helper-lilypad/src/main/java/me/lucko/helper/lilypad/HelperLilyPad.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
helper-lilypad/src/main/java/me/lucko/helper/lilypad/plugin/LilyPadPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
helper-lilypad/src/main/java/me/lucko/helper/lilypad/plugin/LilyPadWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters