From ceef1f51840ba36e1c94e3bcf202f48877d5069f Mon Sep 17 00:00:00 2001 From: mohammed jasem alaajel Date: Fri, 15 Jul 2022 03:05:53 +0400 Subject: [PATCH] rename listener classes depending on the platform, readd plugin message back #38 --- ...er.java => RedisBungeeBungeeListener.java} | 8 +- .../redisbungee/RedisBungeeBungeePlugin.java | 3 +- ....java => RedisBungeeVelocityListener.java} | 117 ++++++++++++++++-- .../RedisBungeeVelocityPlugin.java | 10 +- 4 files changed, 120 insertions(+), 18 deletions(-) rename RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/{RedisBungeeListener.java => RedisBungeeBungeeListener.java} (95%) rename RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/{RedisBungeeListener.java => RedisBungeeVelocityListener.java} (54%) diff --git a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeBungeeListener.java similarity index 95% rename from RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java rename to RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeBungeeListener.java index 245e851..67e0560 100644 --- a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeListener.java +++ b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeBungeeListener.java @@ -26,10 +26,10 @@ import redis.clients.jedis.Pipeline; import java.net.InetAddress; import java.util.*; -public class RedisBungeeListener extends AbstractRedisBungeeListener implements Listener { +public class RedisBungeeBungeeListener extends AbstractRedisBungeeListener implements Listener { - public RedisBungeeListener(RedisBungeePlugin plugin, List exemptAddresses) { + public RedisBungeeBungeeListener(RedisBungeePlugin plugin, List exemptAddresses) { super(plugin, exemptAddresses); } @@ -194,7 +194,7 @@ public class RedisBungeeListener extends AbstractRedisBungeeListener { +public class RedisBungeeVelocityListener extends AbstractRedisBungeeListener { - public RedisBungeeListener(RedisBungeePlugin plugin, List exemptAddresses) { + public RedisBungeeVelocityListener(RedisBungeePlugin plugin, List exemptAddresses) { super(plugin, exemptAddresses); } @@ -136,12 +143,106 @@ public class RedisBungeeListener extends AbstractRedisBungeeListener { + ByteArrayDataInput in = ByteStreams.newDataInput(data); + + String subchannel = in.readUTF(); + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + String type; + + switch (subchannel) { + case "PlayerList": + out.writeUTF("PlayerList"); + Set original = Collections.emptySet(); + type = in.readUTF(); + if (type.equals("ALL")) { + out.writeUTF("ALL"); + original = plugin.getPlayers(); + } else { + try { + original = plugin.getApi().getPlayersOnServer(type); + } catch (IllegalArgumentException ignored) { + } + } + Set players = new HashSet<>(); + for (UUID uuid : original) + players.add(plugin.getUuidTranslator().getNameFromUuid(uuid, false)); + out.writeUTF(Joiner.on(',').join(players)); + break; + case "PlayerCount": + out.writeUTF("PlayerCount"); + type = in.readUTF(); + if (type.equals("ALL")) { + out.writeUTF("ALL"); + out.writeInt(plugin.getCount()); + } else { + out.writeUTF(type); + try { + out.writeInt(plugin.getApi().getPlayersOnServer(type).size()); + } catch (IllegalArgumentException e) { + out.writeInt(0); + } + } + break; + case "LastOnline": + String user = in.readUTF(); + out.writeUTF("LastOnline"); + out.writeUTF(user); + out.writeLong(plugin.getApi().getLastOnline(Objects.requireNonNull(plugin.getUuidTranslator().getTranslatedUuid(user, true)))); + break; + case "ServerPlayers": + String type1 = in.readUTF(); + out.writeUTF("ServerPlayers"); + Multimap multimap = plugin.getApi().getServerToPlayers(); + + boolean includesUsers; + + switch (type1) { + case "COUNT": + includesUsers = false; + break; + case "PLAYERS": + includesUsers = true; + break; + default: + // TODO: Should I raise an error? + return; + } + + out.writeUTF(type1); + + if (includesUsers) { + Multimap human = HashMultimap.create(); + for (Map.Entry entry : multimap.entries()) { + human.put(entry.getKey(), plugin.getUuidTranslator().getNameFromUuid(entry.getValue(), false)); + } + serializeMultimap(human, true, out); + } else { + serializeMultiset(multimap.keys(), out); + } + break; + case "Proxy": + out.writeUTF("Proxy"); + out.writeUTF(plugin.getConfiguration().getServerId()); + break; + case "PlayerProxy": + String username = in.readUTF(); + out.writeUTF("PlayerProxy"); + out.writeUTF(username); + out.writeUTF(plugin.getApi().getProxy(Objects.requireNonNull(plugin.getUuidTranslator().getTranslatedUuid(username, true)))); + break; + default: + return; + } + + ((ServerConnection) event.getSource()).sendPluginMessage(new LegacyChannelIdentifier(currentChannel), out.toByteArray()); + }); + } } @@ -153,7 +254,7 @@ public class RedisBungeeListener extends AbstractRedisBungeeListener { } }).repeat(3, TimeUnit.SECONDS).schedule(); - getProxy().getEventManager().register(this, new RedisBungeeListener(this, configuration.getExemptAddresses())); + getProxy().getEventManager().register(this, new RedisBungeeVelocityListener(this, configuration.getExemptAddresses())); getProxy().getEventManager().register(this, dataManager); getProxy().getScheduler().buildTask(this, psl).schedule(); integrityCheck = getProxy().getScheduler().buildTask(this, () -> { @@ -473,10 +475,10 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin { } }).repeat(1, TimeUnit.MINUTES).schedule(); - // plugin messages are disabled for now - //getProxy().registerChannel("legacy:redisbungee"); - //getProxy().registerChannel("RedisBungee"); + getProxy().getChannelRegistrar().register(new LegacyChannelIdentifier("RedisBungee")); + getProxy().getChannelRegistrar().register(new LegacyChannelIdentifier("legacy:redisbungee")); + getProxy().getChannelRegistrar().register(MinecraftChannelIdentifier.create("legacy", "redisbungee")); // register commands // those still bungeecord commands will migrate them later. // if (configuration.doOverrideBungeeCommands()) {