From 7b00e6fefd002224f6252fa014e0b4c6cba6da8e Mon Sep 17 00:00:00 2001 From: Steven Scott Date: Sun, 3 Feb 2013 21:00:06 -0800 Subject: [PATCH] Update to be compatible with the latest build of BungeeCord --- README | 2 + src/net/TheDgtl/Stargate/Portal.java | 6 +- src/net/TheDgtl/Stargate/Stargate.java | 75 ++++++++++++++++++++---- src/net/TheDgtl/Stargate/pmListener.java | 52 +++++++++++++--- src/plugin.yml | 2 +- 5 files changed, 114 insertions(+), 23 deletions(-) diff --git a/README b/README index abef3cc..1d50380 100644 --- a/README +++ b/README @@ -215,6 +215,8 @@ Bukkit Issue: Stargate will randomly NPE when drawing a sign. Long-standing Bukk ============= Changes ============= +[Version 0.7.9.3] + - Update BungeeCord integration for b152+ [Version 0.7.9.2] - Remove my custom sign class. Stupid Bukkit team. - Will work with CB 1.4.5 builds, but now will break randomly due to Bukkit screwup diff --git a/src/net/TheDgtl/Stargate/Portal.java b/src/net/TheDgtl/Stargate/Portal.java index 1cb276c..95d91ef 100644 --- a/src/net/TheDgtl/Stargate/Portal.java +++ b/src/net/TheDgtl/Stargate/Portal.java @@ -850,14 +850,14 @@ public class Portal { } else { // Check if network exists in our network list if (!lookupNamesNet.containsKey(getNetwork().toLowerCase())) { - Stargate.debug("register", "Network not in lookupNamesNet, adding"); + Stargate.debug("register", "Network " + getNetwork() + " not in lookupNamesNet, adding"); lookupNamesNet.put(getNetwork().toLowerCase(), new HashMap()); } lookupNamesNet.get(getNetwork().toLowerCase()).put(getName().toLowerCase(), this); // Check if this network exists if (!allPortalsNet.containsKey(getNetwork().toLowerCase())) { - Stargate.debug("register", "Network not in allPortalsNet, adding"); + Stargate.debug("register", "Network " + getNetwork() + " not in allPortalsNet, adding"); allPortalsNet.put(getNetwork().toLowerCase(), new ArrayList()); } allPortalsNet.get(getNetwork().toLowerCase()).add(getName().toLowerCase()); @@ -1204,7 +1204,7 @@ public class Portal { } public static Portal getBungeeGate(String name) { - return bungeePortals.get(name); + return bungeePortals.get(name.toLowerCase()); } public static void saveAllGates(World world) { diff --git a/src/net/TheDgtl/Stargate/Stargate.java b/src/net/TheDgtl/Stargate/Stargate.java index e172b19..07ca274 100644 --- a/src/net/TheDgtl/Stargate/Stargate.java +++ b/src/net/TheDgtl/Stargate/Stargate.java @@ -1,9 +1,13 @@ package net.TheDgtl.Stargate; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; import java.io.File; import java.io.IOException; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; +import java.util.Map; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.logging.Level; @@ -40,6 +44,7 @@ import org.bukkit.event.block.BlockPistonRetractEvent; import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerPortalEvent; import org.bukkit.event.server.PluginDisableEvent; @@ -116,6 +121,9 @@ public class Stargate extends JavaPlugin { // Used for populating gate open/closed material. public static Queue blockPopulatorQueue = new LinkedList(); + // HashMap of player names for Bungee support + public static Map bungeeQueue = new HashMap(); + public void onDisable() { Portal.closeAllGates(); Portal.clearGates(); @@ -150,8 +158,8 @@ public class Stargate extends JavaPlugin { // Enable the required channels for Bungee support if (enableBungee) { - Bukkit.getMessenger().registerOutgoingPluginChannel(this, "SGBungee"); - Bukkit.getMessenger().registerIncomingPluginChannel(this, "SGBungee", new pmListener()); + Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); + Bukkit.getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new pmListener()); } // It is important to load languages here, as they are used during reloadGates() @@ -723,6 +731,21 @@ public class Stargate extends JavaPlugin { } private class pListener implements Listener { + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + if (!enableBungee) return; + + Player player = event.getPlayer(); + String destination = bungeeQueue.get(player.getName().toLowerCase()); + if (destination == null) return; + + Portal portal = Portal.getBungeeGate(destination); + if (portal == null) { + Stargate.debug("PlayerJoin", "Error fetching destination portal: " + destination); + return; + } + portal.teleport(player, portal, null); + } @EventHandler public void onPlayerPortal(PlayerPortalEvent event) { @@ -820,19 +843,51 @@ public class Stargate extends JavaPlugin { } Stargate.sendMessage(player, Stargate.getString("teleportMsg"), false); + + // BungeeCord Support if (portal.isBungee()) { - if (!enableBungee) { player.sendMessage(Stargate.getString("bungeeDisabled")); portal.close(false); return; } + // Teleport the player back to this gate, for sanity's sake portal.teleport(player, portal, event); - // Teleport player via BungeeCord - String pMsg = portal.getNetwork() + "@#@" + portal.getDestinationName(); - player.sendPluginMessage(stargate, "SGBungee", pMsg.getBytes()); + // Send the SGBungee packet first, it will be queued by BC if required + try { + // Build the message, format is #@# + String msg = event.getPlayer().getName() + "#@#" + portal.getDestinationName(); + // Build the message data, sent over the SGBungee bungeecord channel + ByteArrayOutputStream bao = new ByteArrayOutputStream(); + DataOutputStream msgData = new DataOutputStream(bao); + msgData.writeUTF("Forward"); + msgData.writeUTF(portal.getNetwork()); // Server + msgData.writeUTF("SGBungee"); // Channel + msgData.writeShort(msg.length()); // Data Length + msgData.writeBytes(msg); // Data + player.sendPluginMessage(stargate, "BungeeCord", bao.toByteArray()); + } catch (IOException ex) { + Stargate.log.severe("[Stargate] Error sending BungeeCord teleport packet"); + ex.printStackTrace(); + return; + } + + // Connect player to new server + try { + ByteArrayOutputStream bao = new ByteArrayOutputStream(); + DataOutputStream msgData = new DataOutputStream(bao); + msgData.writeUTF("Connect"); + msgData.writeUTF(portal.getNetwork()); + + player.sendPluginMessage(stargate, "BungeeCord", bao.toByteArray()); + bao.reset(); + } catch(IOException ex) { + Stargate.log.severe("[Stargate] Error sending BungeeCord connect packet"); + ex.printStackTrace(); + return; + } // Close portal if required (Should never be) portal.close(false); @@ -1346,11 +1401,11 @@ public class Stargate extends JavaPlugin { // Enable the required channels for Bungee support if (oldEnableBungee != enableBungee) { if (enableBungee) { - Bukkit.getMessenger().registerOutgoingPluginChannel(this, "SGBungee"); - Bukkit.getMessenger().registerIncomingPluginChannel(this, "SGBungee", new pmListener()); + Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); + Bukkit.getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new pmListener()); } else { - Bukkit.getMessenger().unregisterIncomingPluginChannel(this, "SGBungee"); - Bukkit.getMessenger().unregisterOutgoingPluginChannel(this, "SGBungee"); + Bukkit.getMessenger().unregisterIncomingPluginChannel(this, "BungeeCord"); + Bukkit.getMessenger().unregisterOutgoingPluginChannel(this, "BungeeCord"); } } diff --git a/src/net/TheDgtl/Stargate/pmListener.java b/src/net/TheDgtl/Stargate/pmListener.java index a8d5e58..ee83ba9 100644 --- a/src/net/TheDgtl/Stargate/pmListener.java +++ b/src/net/TheDgtl/Stargate/pmListener.java @@ -1,23 +1,57 @@ package net.TheDgtl.Stargate; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; + import org.bukkit.entity.Player; import org.bukkit.plugin.messaging.PluginMessageListener; public class pmListener implements PluginMessageListener { @Override - public void onPluginMessageReceived(String channel, Player player, byte[] message) { - if (!channel.equals("SGBungee") || !Stargate.enableBungee) return; + public void onPluginMessageReceived(String channel, Player unused, byte[] message) { + if (!Stargate.enableBungee || !channel.equals("BungeeCord")) return; - // Message should be destination gate name. - Portal dest = Portal.getBungeeGate(new String(message)); - - // Specified an invalid gate. For now we'll just let them connect at their current location - if (dest == null) { + // Get data from message + String inChannel; + byte[] data; + try { + DataInputStream in = new DataInputStream(new ByteArrayInputStream(message)); + inChannel = in.readUTF(); + short len = in.readShort(); + data = new byte[len]; + in.readFully(data); + } catch (IOException ex) { + Stargate.log.severe("[Stargate] Error receiving BungeeCord message"); + ex.printStackTrace(); return; } - // Teleport the player to their destination portal - dest.teleport(player, dest, null); + // Verify that it's an SGBungee packet + if (!inChannel.equals("SGBungee")) { + return; + } + + // Data should be player name, and destination gate name + String msg = new String(data); + String[] parts = msg.split("#@#"); + + String playerName = parts[0]; + String destination = parts[1]; + + // Check if the player is online, if so, teleport, otherwise, queue + Player player = Stargate.server.getPlayer(playerName); + if (player == null) { + Stargate.bungeeQueue.put(playerName.toLowerCase(), destination); + } else { + Portal dest = Portal.getBungeeGate(destination); + // Specified an invalid gate. For now we'll just let them connect at their current location + if (dest == null) { + Stargate.log.info("[Stargate] Bungee gate " + destination + " does not exist"); + return; + } + dest.teleport(player, dest, null); + } } } diff --git a/src/plugin.yml b/src/plugin.yml index 713cac5..6154ef0 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: Stargate main: net.TheDgtl.Stargate.Stargate -version: 0.7.9.2 +version: 0.7.9.3 description: Stargate mod for Bukkit author: Drakia website: http://www.thedgtl.net