2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-11-23 04:28:01 +00:00

code clean up and lambda etc

This commit is contained in:
mohammed jasem alaajel 2021-05-21 16:51:44 +04:00
parent 39b3c03604
commit 96793e81c5
2 changed files with 90 additions and 92 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.imaginarycode.minecraft</groupId> <groupId>com.imaginarycode.minecraft</groupId>
<artifactId>RedisBungee</artifactId> <artifactId>RedisBungee</artifactId>
<version>0.6.3</version> <version>0.6.4-SNAPSHOT</version>
<repositories> <repositories>

View File

@ -14,7 +14,6 @@ import net.md_5.bungee.api.AbstractReconnectHandler;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server; import net.md_5.bungee.api.connection.Server;
@ -62,7 +61,7 @@ public class RedisBungeeListener implements Listener {
if (player != null) { if (player != null) {
event.setCancelled(true); event.setCancelled(true);
// TODO: Make it accept a BaseComponent[] like everything else. // TODO: Make it accept a BaseComponent[] like everything else.
event.setCancelReason(TextComponent.toLegacyText(ONLINE_MODE_RECONNECT)); event.setCancelReason(ONLINE_MODE_RECONNECT);
return null; return null;
} }
} }
@ -71,7 +70,7 @@ public class RedisBungeeListener implements Listener {
if (jedis.sismember("proxy:" + s + ":usersOnline", event.getConnection().getUniqueId().toString())) { if (jedis.sismember("proxy:" + s + ":usersOnline", event.getConnection().getUniqueId().toString())) {
event.setCancelled(true); event.setCancelled(true);
// TODO: Make it accept a BaseComponent[] like everything else. // TODO: Make it accept a BaseComponent[] like everything else.
event.setCancelReason(TextComponent.toLegacyText(ALREADY_LOGGED_IN)); event.setCancelReason(ALREADY_LOGGED_IN);
return null; return null;
} }
} }
@ -146,107 +145,105 @@ public class RedisBungeeListener implements Listener {
event.getResponse().getPlayers().setOnline(plugin.getCount()); event.getResponse().getPlayers().setOnline(plugin.getCount());
} }
@SuppressWarnings("UnstableApiUsage")
@EventHandler @EventHandler
public void onPluginMessage(final PluginMessageEvent event) { public void onPluginMessage(final PluginMessageEvent event) {
if ((event.getTag().equals("legacy:redisbungee") || event.getTag().equals("RedisBungee")) && event.getSender() instanceof Server) { if ((event.getTag().equals("legacy:redisbungee") || event.getTag().equals("RedisBungee")) && event.getSender() instanceof Server) {
final String currentChannel = event.getTag(); final String currentChannel = event.getTag();
final byte[] data = Arrays.copyOf(event.getData(), event.getData().length); final byte[] data = Arrays.copyOf(event.getData(), event.getData().length);
plugin.getProxy().getScheduler().runAsync(plugin, new Runnable() { plugin.getProxy().getScheduler().runAsync(plugin, () -> {
@Override ByteArrayDataInput in = ByteStreams.newDataInput(data);
public void run() {
ByteArrayDataInput in = ByteStreams.newDataInput(data);
String subchannel = in.readUTF(); String subchannel = in.readUTF();
ByteArrayDataOutput out = ByteStreams.newDataOutput(); ByteArrayDataOutput out = ByteStreams.newDataOutput();
String type; String type;
switch (subchannel) { switch (subchannel) {
case "PlayerList": case "PlayerList":
out.writeUTF("PlayerList"); out.writeUTF("PlayerList");
Set<UUID> original = Collections.emptySet(); Set<UUID> original = Collections.emptySet();
type = in.readUTF(); type = in.readUTF();
if (type.equals("ALL")) { if (type.equals("ALL")) {
out.writeUTF("ALL"); out.writeUTF("ALL");
original = plugin.getPlayers(); original = plugin.getPlayers();
} else { } else {
try { try {
original = RedisBungee.getApi().getPlayersOnServer(type); original = RedisBungee.getApi().getPlayersOnServer(type);
} catch (IllegalArgumentException ignored) { } catch (IllegalArgumentException ignored) {
}
} }
Set<String> players = new HashSet<>(); }
for (UUID uuid : original) Set<String> players = new HashSet<>();
players.add(plugin.getUuidTranslator().getNameFromUuid(uuid, false)); for (UUID uuid : original)
out.writeUTF(Joiner.on(',').join(players)); players.add(plugin.getUuidTranslator().getNameFromUuid(uuid, false));
break; out.writeUTF(Joiner.on(',').join(players));
case "PlayerCount": break;
out.writeUTF("PlayerCount"); case "PlayerCount":
type = in.readUTF(); out.writeUTF("PlayerCount");
if (type.equals("ALL")) { type = in.readUTF();
out.writeUTF("ALL"); if (type.equals("ALL")) {
out.writeInt(plugin.getCount()); out.writeUTF("ALL");
} else { out.writeInt(plugin.getCount());
out.writeUTF(type); } else {
try { out.writeUTF(type);
out.writeInt(RedisBungee.getApi().getPlayersOnServer(type).size()); try {
} catch (IllegalArgumentException e) { out.writeInt(RedisBungee.getApi().getPlayersOnServer(type).size());
out.writeInt(0); } catch (IllegalArgumentException e) {
} out.writeInt(0);
} }
break; }
case "LastOnline": break;
String user = in.readUTF(); case "LastOnline":
out.writeUTF("LastOnline"); String user = in.readUTF();
out.writeUTF(user); out.writeUTF("LastOnline");
out.writeLong(RedisBungee.getApi().getLastOnline(plugin.getUuidTranslator().getTranslatedUuid(user, true))); out.writeUTF(user);
break; out.writeLong(RedisBungee.getApi().getLastOnline(plugin.getUuidTranslator().getTranslatedUuid(user, true)));
case "ServerPlayers": break;
String type1 = in.readUTF(); case "ServerPlayers":
out.writeUTF("ServerPlayers"); String type1 = in.readUTF();
Multimap<String, UUID> multimap = RedisBungee.getApi().getServerToPlayers(); out.writeUTF("ServerPlayers");
Multimap<String, UUID> multimap = RedisBungee.getApi().getServerToPlayers();
boolean includesUsers; boolean includesUsers;
switch (type1) { switch (type1) {
case "COUNT": case "COUNT":
includesUsers = false; includesUsers = false;
break; break;
case "PLAYERS": case "PLAYERS":
includesUsers = true; includesUsers = true;
break; break;
default: default:
// TODO: Should I raise an error? // TODO: Should I raise an error?
return; return;
}
out.writeUTF(type1);
if (includesUsers) {
Multimap<String, String> human = HashMultimap.create();
for (Map.Entry<String, UUID> entry : multimap.entries()) {
human.put(entry.getKey(), plugin.getUuidTranslator().getNameFromUuid(entry.getValue(), false));
} }
serializeMultimap(human, true, out);
out.writeUTF(type1); } else {
serializeMultiset(multimap.keys(), out);
if (includesUsers) { }
Multimap<String, String> human = HashMultimap.create(); break;
for (Map.Entry<String, UUID> entry : multimap.entries()) { case "Proxy":
human.put(entry.getKey(), plugin.getUuidTranslator().getNameFromUuid(entry.getValue(), false)); out.writeUTF("Proxy");
} out.writeUTF(RedisBungee.getConfiguration().getServerId());
serializeMultimap(human, true, out); break;
} else { case "PlayerProxy":
serializeMultiset(multimap.keys(), out); String username = in.readUTF();
} out.writeUTF("PlayerProxy");
break; out.writeUTF(username);
case "Proxy": out.writeUTF(RedisBungee.getApi().getProxy(plugin.getUuidTranslator().getTranslatedUuid(username, true)));
out.writeUTF("Proxy"); break;
out.writeUTF(RedisBungee.getConfiguration().getServerId()); default:
break; return;
case "PlayerProxy":
String username = in.readUTF();
out.writeUTF("PlayerProxy");
out.writeUTF(username);
out.writeUTF(RedisBungee.getApi().getProxy(plugin.getUuidTranslator().getTranslatedUuid(username, true)));
break;
default:
return;
}
((Server) event.getSender()).sendData(currentChannel, out.toByteArray());
} }
((Server) event.getSender()).sendData(currentChannel, out.toByteArray());
}); });
} }
} }
@ -259,6 +256,7 @@ public class RedisBungeeListener implements Listener {
} }
} }
@SuppressWarnings("SameParameterValue")
private void serializeMultimap(Multimap<String, String> collection, boolean includeNames, ByteArrayDataOutput output) { private void serializeMultimap(Multimap<String, String> collection, boolean includeNames, ByteArrayDataOutput output) {
output.writeInt(collection.keySet().size()); output.writeInt(collection.keySet().size());
for (Map.Entry<String, Collection<String>> entry : collection.asMap().entrySet()) { for (Map.Entry<String, Collection<String>> entry : collection.asMap().entrySet()) {