Improve code readability

This commit is contained in:
filoghost 2019-10-16 19:39:09 +02:00
parent 7496bf8d4d
commit 6d3c774f64

View File

@ -36,6 +36,7 @@ import com.gmail.filoghost.holographicdisplays.util.NMSVersion;
public class BungeeChannel implements PluginMessageListener { public class BungeeChannel implements PluginMessageListener {
private static BungeeChannel instance; private static BungeeChannel instance;
private String redisBungeeChannel;
public static BungeeChannel getInstance() { public static BungeeChannel getInstance() {
if (instance == null) { if (instance == null) {
@ -48,19 +49,27 @@ public class BungeeChannel implements PluginMessageListener {
Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord"); Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord");
Bukkit.getMessenger().registerIncomingPluginChannel(plugin, "BungeeCord", this); Bukkit.getMessenger().registerIncomingPluginChannel(plugin, "BungeeCord", this);
String targetChannel = "RedisBungee"; if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) {
if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) targetChannel = "legacy:redisbungee"; redisBungeeChannel = "legacy:redisbungee";
} else {
redisBungeeChannel = "RedisBungee";
}
Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, targetChannel); Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, redisBungeeChannel);
Bukkit.getMessenger().registerIncomingPluginChannel(plugin, targetChannel, this); Bukkit.getMessenger().registerIncomingPluginChannel(plugin, redisBungeeChannel, this);
}
private String getTargetChannel() {
if (Configuration.useRedisBungee) {
return redisBungeeChannel;
} else {
return "BungeeCord";
}
} }
@Override @Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) { public void onPluginMessageReceived(String channel, Player player, byte[] message) {
String targetChannel = Configuration.useRedisBungee ? "RedisBungee" : "BungeeCord"; if (channel.equals(getTargetChannel())) {
if (targetChannel.equalsIgnoreCase("RedisBungee") && NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) targetChannel = "legacy:redisbungee";
if (channel.equals(targetChannel)) {
DataInputStream in = new DataInputStream(new ByteArrayInputStream(message)); DataInputStream in = new DataInputStream(new ByteArrayInputStream(message));
try { try {
@ -86,7 +95,6 @@ public class BungeeChannel implements PluginMessageListener {
} }
} }
public void askPlayerCount(String server) { public void askPlayerCount(String server) {
ByteArrayOutputStream b = new ByteArrayOutputStream(); ByteArrayOutputStream b = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(b); DataOutputStream out = new DataOutputStream(b);
@ -102,9 +110,7 @@ public class BungeeChannel implements PluginMessageListener {
// OR, if you don't need to send it to a specific player // OR, if you don't need to send it to a specific player
Collection<? extends Player> players = Bukkit.getOnlinePlayers(); Collection<? extends Player> players = Bukkit.getOnlinePlayers();
if (players.size() > 0) { if (players.size() > 0) {
String targetChannel = Configuration.useRedisBungee ? "RedisBungee" : "BungeeCord"; players.iterator().next().sendPluginMessage(HolographicDisplays.getInstance(), getTargetChannel(), b.toByteArray());
if (targetChannel.equalsIgnoreCase("RedisBungee") && NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) targetChannel = "legacy:redisbungee";
players.iterator().next().sendPluginMessage(HolographicDisplays.getInstance(), targetChannel, b.toByteArray());
} }
} }
} }