diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/events/PlayerChangedServerNetworkEvent.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/events/PlayerChangedServerNetworkEvent.java new file mode 100644 index 0000000..ad48522 --- /dev/null +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/events/PlayerChangedServerNetworkEvent.java @@ -0,0 +1,36 @@ +package com.imaginarycode.minecraft.redisbungee.events; + +import java.util.UUID; + +/** + * This event is sent when a player connects to a new server. RedisBungee sends the event only when + * the proxy the player has been connected to is different than the local proxy. + *

+ * This event corresponds to net.md_5.bungee.api.event.ServerConnectedEvent on bungee and is fired + * asynchronously. + * + * @since 0.3.4 + */ +public class PlayerChangedServerNetworkEvent { + private final UUID uuid; + private final String previousServer; + private final String server; + + public PlayerChangedServerNetworkEvent(UUID uuid, String previousServer, String server) { + this.uuid = uuid; + this.previousServer = previousServer; + this.server = server; + } + + public UUID getUuid() { + return uuid; + } + + public String getServer() { + return server; + } + + public String getPreviousServer() { + return previousServer; + } +} diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/events/PlayerJoinedNetworkEvent.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/events/PlayerJoinedNetworkEvent.java new file mode 100644 index 0000000..d113edf --- /dev/null +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/events/PlayerJoinedNetworkEvent.java @@ -0,0 +1,24 @@ +package com.imaginarycode.minecraft.redisbungee.events; + +import java.util.UUID; + +/** + * This event is sent when a player joins the network. RedisBungee sends the event only when + * the proxy the player has been connected to is different than the local proxy. + *

+ * This event corresponds to net.md_5.bungee.api.event.PostLoginEvent on bungee, and is fired + * asynchronously. + * + * @since 0.3.4 + */ +public class PlayerJoinedNetworkEvent { + private final UUID uuid; + + public PlayerJoinedNetworkEvent(UUID uuid) { + this.uuid = uuid; + } + + public UUID getUuid() { + return uuid; + } +} diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/events/PlayerLeftNetworkEvent.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/events/PlayerLeftNetworkEvent.java new file mode 100644 index 0000000..4ae176b --- /dev/null +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/events/PlayerLeftNetworkEvent.java @@ -0,0 +1,26 @@ +package com.imaginarycode.minecraft.redisbungee.events; + + + +import java.util.UUID; + +/** + * This event is sent when a player disconnects. RedisBungee sends the event only when + * the proxy the player has been connected to is different than the local proxy. + *

+ * This event corresponds to net.md_5.bungee.api.event.PlayerDisconnectEvent on bungee, and is fired + * asynchronously. + * + * @since 0.3.4 + */ +public class PlayerLeftNetworkEvent { + private final UUID uuid; + + public PlayerLeftNetworkEvent(UUID uuid) { + this.uuid = uuid; + } + + public UUID getUuid() { + return uuid; + } +} diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/DataManager.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/DataManager.java index 298aa67..879d1c1 100644 --- a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/DataManager.java +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/DataManager.java @@ -11,6 +11,7 @@ import com.google.gson.JsonParser; import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; import redis.clients.jedis.Jedis; +import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; import java.util.Objects; import java.util.UUID; @@ -113,7 +114,7 @@ public abstract class DataManager { } catch (ExecutionException | UncheckedExecutionException e) { if (e.getCause() instanceof NullPointerException && e.getCause().getMessage().equals("user not found")) return null; // HACK - plugin.logFatal( "Unable to get IP"); + plugin.logFatal("Unable to get IP"); throw new RuntimeException("Unable to get IP for " + uuid, e); } } @@ -148,13 +149,9 @@ public abstract class DataManager { } - public void onPostLogin(PL event) { + public abstract void onPostLogin(PL event); - } - - public void onPlayerDisconnect(PD event) { - - } + public abstract void onPlayerDisconnect(PD event); public abstract void onPubSubMessage(PS event); @@ -178,11 +175,18 @@ public abstract class DataManager { }.getType()); proxyCache.put(message1.getTarget(), message1.getSource()); lastOnlineCache.put(message1.getTarget(), (long) 0); - ipCache.put(message1.getTarget(), ((LoginPayload)message1.getPayload()).getAddress()); + ipCache.put(message1.getTarget(), ((LoginPayload) message1.getPayload()).getAddress()); plugin.executeAsync(new Runnable() { @Override public void run() { - //plugin.getProxy().getPluginManager().callEvent(new PlayerJoinedNetworkEvent(message1.getTarget())); + Object event; + try { + event = plugin.getNetworkJoinEventClass().getDeclaredConstructor(UUID.class).newInstance(message1.getTarget()); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + throw new RuntimeException("unable to dispatch an network join event", e); + } + plugin.callEvent(event); + } }); break; @@ -190,22 +194,34 @@ public abstract class DataManager { final DataManagerMessage message2 = gson.fromJson(jsonObject, new TypeToken() { }.getType()); invalidate(message2.getTarget()); - lastOnlineCache.put(message2.getTarget(), ((LogoutPayload)message2.getPayload()).getTimestamp()); + lastOnlineCache.put(message2.getTarget(), ((LogoutPayload) message2.getPayload()).getTimestamp()); plugin.executeAsync(new Runnable() { @Override public void run() { - // plugin.getProxy().getPluginManager().callEvent(new PlayerLeftNetworkEvent(message2.getTarget())); + Object event; + try { + event = plugin.getNetworkQuitEventClass().getDeclaredConstructor(UUID.class).newInstance(message2.getTarget()); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + throw new RuntimeException("unable to dispatch an network quit event", e); + } + plugin.callEvent(event); } }); break; case SERVER_CHANGE: final DataManagerMessage message3 = gson.fromJson(jsonObject, new TypeToken() { }.getType()); - serverCache.put(message3.getTarget(), ((ServerChangePayload)message3.getPayload()).getServer()); + serverCache.put(message3.getTarget(), ((ServerChangePayload) message3.getPayload()).getServer()); plugin.executeAsync(new Runnable() { @Override public void run() { - //plugin.getProxy().getPluginManager().callEvent(new PlayerChangedServerNetworkEvent(message3.getTarget(), message3.getPayload().getOldServer(), message3.getPayload().getServer())); + Object event; + try { + event = plugin.getServerChangeEventClass().getDeclaredConstructor(UUID.class, String.class, String.class).newInstance(message3.getTarget(), ((ServerChangePayload) message3.getPayload()).getOldServer(), ((ServerChangePayload) message3.getPayload()).getServer()); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + throw new RuntimeException("unable to dispatch an server change event", e); + } + plugin.callEvent(event); } }); break; @@ -213,7 +229,6 @@ public abstract class DataManager { } - public static class DataManagerMessage { private final UUID target; private final String source; @@ -254,7 +269,7 @@ public abstract class DataManager { } - public static class LoginPayload extends Payload{ + public static class LoginPayload extends Payload { private final InetAddress address; public LoginPayload(InetAddress address) { @@ -266,7 +281,7 @@ public abstract class DataManager { } } - public static class ServerChangePayload extends Payload{ + public static class ServerChangePayload extends Payload { private final String server; private final String oldServer; diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/JedisPubSubHandler.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/JedisPubSubHandler.java index 453ab53..ecafdfc 100644 --- a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/JedisPubSubHandler.java +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/JedisPubSubHandler.java @@ -1,47 +1,34 @@ package com.imaginarycode.minecraft.redisbungee.internal; -import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; + import redis.clients.jedis.JedisPubSub; import java.lang.reflect.InvocationTargetException; + public class JedisPubSubHandler extends JedisPubSub { private final RedisBungeePlugin plugin; - + private final Class eventClass; public JedisPubSubHandler(RedisBungeePlugin plugin) { this.plugin = plugin; + this.eventClass = plugin.getPubSubEventClass(); } - private Class bungeeEvent; - @Override public void onMessage(final String s, final String s2) { if (s2.trim().length() == 0) return; plugin.executeAsync(new Runnable() { @Override public void run() { - if (isBungeeEvent()) { - try { - Object object = bungeeEvent.getConstructor(String.class, String.class).newInstance(s, s2); - plugin.callEvent(object); - } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { - e.printStackTrace(); - throw new RuntimeException("unable to fire pubsub event."); - } - return; + Object event; + try { + event = eventClass.getDeclaredConstructor(String.class, String.class).newInstance(s, s2); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + throw new RuntimeException("unable to dispatch an pubsub event", e); } - PubSubMessageEvent event = new PubSubMessageEvent(s, s2); plugin.callEvent(event); } }); } - - public boolean isBungeeEvent() { - return bungeeEvent != null; - } - - public void setBungeeEvent(Class clazz) { - bungeeEvent = clazz; - } } \ No newline at end of file diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/PubSubListener.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/PubSubListener.java index c14a271..c8e0fd4 100644 --- a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/PubSubListener.java +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/PubSubListener.java @@ -11,7 +11,7 @@ import java.util.logging.Level; public class PubSubListener implements Runnable { private JedisPubSubHandler jpsh; - private Set addedChannels = new HashSet(); + private final Set addedChannels = new HashSet(); private final RedisBungeePlugin plugin; @@ -24,6 +24,7 @@ public class PubSubListener implements Runnable { boolean broken = false; try (Jedis rsc = plugin.requestJedis()) { try { + jpsh = new JedisPubSubHandler(plugin); addedChannels.add("redisbungee-" + plugin.getConfiguration().getServerId()); addedChannels.add("redisbungee-allservers"); diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/RedisBungeePlugin.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/RedisBungeePlugin.java index 3111728..02a193e 100644 --- a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/RedisBungeePlugin.java +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/internal/RedisBungeePlugin.java @@ -2,6 +2,10 @@ package com.imaginarycode.minecraft.redisbungee.internal; import com.google.common.collect.Multimap; import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI; +import com.imaginarycode.minecraft.redisbungee.events.PlayerChangedServerNetworkEvent; +import com.imaginarycode.minecraft.redisbungee.events.PlayerJoinedNetworkEvent; +import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent; +import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.internal.util.uuid.UUIDTranslator; import redis.clients.jedis.Jedis; @@ -75,5 +79,20 @@ public interface RedisBungeePlugin

{ void executeProxyCommand(String cmd); + default Class getPubSubEventClass() { + return PubSubMessageEvent.class; + } + + default Class getNetworkJoinEventClass() { + return PlayerJoinedNetworkEvent.class; + } + + default Class getServerChangeEventClass() { + return PlayerChangedServerNetworkEvent.class; + } + + default Class getNetworkQuitEventClass() { + return PlayerLeftNetworkEvent.class; + } } diff --git a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/events/bungee/PlayerChangedServerNetworkEvent.java b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/events/bungee/PlayerChangedServerNetworkEvent.java new file mode 100644 index 0000000..e817858 --- /dev/null +++ b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/events/bungee/PlayerChangedServerNetworkEvent.java @@ -0,0 +1,38 @@ +package com.imaginarycode.minecraft.redisbungee.events.bungee; + +import net.md_5.bungee.api.plugin.Event; + +import java.util.UUID; + +/** + * This event is sent when a player connects to a new server. RedisBungee sends the event only when + * the proxy the player has been connected to is different than the local proxy. + *

+ * This event corresponds to {@link net.md_5.bungee.api.event.ServerConnectedEvent}, and is fired + * asynchronously. + * + * @since 0.3.4 + */ +public class PlayerChangedServerNetworkEvent extends Event { + private final UUID uuid; + private final String previousServer; + private final String server; + + public PlayerChangedServerNetworkEvent(UUID uuid, String previousServer, String server) { + this.uuid = uuid; + this.previousServer = previousServer; + this.server = server; + } + + public UUID getUuid() { + return uuid; + } + + public String getServer() { + return server; + } + + public String getPreviousServer() { + return previousServer; + } +} diff --git a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/events/bungee/PlayerJoinedNetworkEvent.java b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/events/bungee/PlayerJoinedNetworkEvent.java new file mode 100644 index 0000000..8f952b0 --- /dev/null +++ b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/events/bungee/PlayerJoinedNetworkEvent.java @@ -0,0 +1,26 @@ +package com.imaginarycode.minecraft.redisbungee.events.bungee; + +import net.md_5.bungee.api.plugin.Event; + +import java.util.UUID; + +/** + * This event is sent when a player joins the network. RedisBungee sends the event only when + * the proxy the player has been connected to is different than the local proxy. + *

+ * This event corresponds to {@link net.md_5.bungee.api.event.PostLoginEvent}, and is fired + * asynchronously. + * + * @since 0.3.4 + */ +public class PlayerJoinedNetworkEvent extends Event { + private final UUID uuid; + + public PlayerJoinedNetworkEvent(UUID uuid) { + this.uuid = uuid; + } + + public UUID getUuid() { + return uuid; + } +} diff --git a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/events/bungee/PlayerLeftNetworkEvent.java b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/events/bungee/PlayerLeftNetworkEvent.java new file mode 100644 index 0000000..1d42429 --- /dev/null +++ b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/events/bungee/PlayerLeftNetworkEvent.java @@ -0,0 +1,26 @@ +package com.imaginarycode.minecraft.redisbungee.events.bungee; + +import net.md_5.bungee.api.plugin.Event; + +import java.util.UUID; + +/** + * This event is sent when a player disconnects. RedisBungee sends the event only when + * the proxy the player has been connected to is different than the local proxy. + *

+ * This event corresponds to {@link net.md_5.bungee.api.event.PlayerDisconnectEvent}, and is fired + * asynchronously. + * + * @since 0.3.4 + */ +public class PlayerLeftNetworkEvent extends Event { + private final UUID uuid; + + public PlayerLeftNetworkEvent(UUID uuid) { + this.uuid = uuid; + } + + public UUID getUuid() { + return uuid; + } +}