From 60a6f2219472999817c2fe50dad4badf2eec588b Mon Sep 17 00:00:00 2001 From: Peter Miller Date: Fri, 27 Jun 2014 17:01:54 -0400 Subject: [PATCH] Added functionality for retrieving serverName from Bungee --- .../src/mineplex/core/portal/Portal.java | 74 ++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/portal/Portal.java b/Plugins/Mineplex.Core/src/mineplex/core/portal/Portal.java index 8e4708c17..e93e69db4 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/portal/Portal.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/portal/Portal.java @@ -1,6 +1,8 @@ package mineplex.core.portal; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.ArrayList; @@ -15,6 +17,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.plugin.messaging.PluginMessageListener; import mineplex.core.MiniPlugin; import mineplex.core.account.CoreClientManager; @@ -23,7 +26,7 @@ import mineplex.core.common.util.NautHashMap; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; -public class Portal extends MiniPlugin +public class Portal extends MiniPlugin implements PluginMessageListener { private static Object _transferLock = new Object(); @@ -34,12 +37,14 @@ public class Portal extends MiniPlugin private boolean _retrieve = true; private CoreClientManager _clientier; + private String serverName = ""; public Portal(JavaPlugin plugin) { super("Portal", plugin); Bukkit.getMessenger().registerOutgoingPluginChannel(GetPlugin(), "BungeeCord"); + Bukkit.getMessenger().registerIncomingPluginChannel(GetPlugin(), "BungeeCord", this); _repository.initialize(plugin.getConfig().getBoolean("serverstatus.us")); _clientier = CoreClientManager.Initialize(plugin, plugin.getConfig().getString("webServer")); @@ -93,6 +98,34 @@ public class Portal extends MiniPlugin } }, 20L); } + + private void ProcessServerName(final Player player) + { + ByteArrayOutputStream b = new ByteArrayOutputStream(); + DataOutputStream out = new DataOutputStream(b); + + try + { + out.writeUTF("GetServer"); + } + catch (IOException e) + { + // Can never happen + } + finally + { + try + { + out.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + player.sendPluginMessage(GetPlugin(), "BungeeCord", b.toByteArray()); + } private void SendPlayerToServerWithMessage(Player player, String serverName) { @@ -180,7 +213,8 @@ public class Portal extends MiniPlugin if(args.length == 0) { - player.sendMessage(ChatColor.YELLOW + "You are currently on server: " + ChatColor.GOLD + GetPlugin().getServer().getName() + " " + GetPlugin().getServer().getServerName()); + ProcessServerName(player); + player.sendMessage(ChatColor.YELLOW + "You are currently on server: " + ChatColor.GOLD + serverName); return; } else if(args.length == 1) @@ -256,4 +290,40 @@ public class Portal extends MiniPlugin } } } + + @Override + public void onPluginMessageReceived(String channel, Player player, byte[] message) + { + if (!channel.equals("BungeeCord")) + { + return; + } + + DataInputStream in = new DataInputStream(new ByteArrayInputStream(message)); + + try + { + String subchannel = in.readUTF(); + + if (subchannel.equals("GetServer")) + { + serverName = in.readUTF(); + } + } + catch (IOException e) + { + // Should never happen + } + finally + { + try + { + in.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + } }