From 5506e66e350cffa0abcf311a862ef316743f21b6 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Tue, 26 Sep 2017 19:47:43 +0300 Subject: [PATCH] Bungee-Bukkit Plugin channel --- .../main/java/com/djrapitops/plan/Plan.java | 14 +++++ .../java/com/djrapitops/plan/PlanBungee.java | 4 ++ .../BukkitPluginChannelListener.java | 47 +++++++++++++++ .../BungeePluginChannelListener.java | 58 +++++++++++++++++++ .../systems/listeners/PlanPlayerListener.java | 14 +++-- .../BungeePluginChannelSenderProcessor.java | 38 ++++++++++++ 6 files changed, 170 insertions(+), 5 deletions(-) create mode 100644 Plan/src/main/java/com/djrapitops/plan/systems/info/pluginchannel/BukkitPluginChannelListener.java create mode 100644 Plan/src/main/java/com/djrapitops/plan/systems/info/pluginchannel/BungeePluginChannelListener.java create mode 100644 Plan/src/main/java/com/djrapitops/plan/systems/processing/info/BungeePluginChannelSenderProcessor.java diff --git a/Plan/src/main/java/com/djrapitops/plan/Plan.java b/Plan/src/main/java/com/djrapitops/plan/Plan.java index 3415e55c5..2cdfa461e 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Plan.java +++ b/Plan/src/main/java/com/djrapitops/plan/Plan.java @@ -41,6 +41,7 @@ import main.java.com.djrapitops.plan.systems.cache.GeolocationCache; import main.java.com.djrapitops.plan.systems.info.BukkitInformationManager; import main.java.com.djrapitops.plan.systems.info.ImporterManager; import main.java.com.djrapitops.plan.systems.info.InformationManager; +import main.java.com.djrapitops.plan.systems.info.pluginchannel.BukkitPluginChannelListener; import main.java.com.djrapitops.plan.systems.info.server.BukkitServerInfoManager; import main.java.com.djrapitops.plan.systems.listeners.*; import main.java.com.djrapitops.plan.systems.processing.Processor; @@ -51,6 +52,7 @@ import main.java.com.djrapitops.plan.systems.webserver.PageCache; import main.java.com.djrapitops.plan.systems.webserver.WebServer; import main.java.com.djrapitops.plan.utilities.Benchmark; import main.java.com.djrapitops.plan.utilities.metrics.BStats; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import java.util.HashSet; @@ -199,6 +201,10 @@ public class Plan extends BukkitPlugin implements IPlan { ImporterManager.registerImporter(new OfflinePlayerImporter()); + if (Settings.BUNGEE_OVERRIDE_STANDALONE_MODE.isFalse()) { + registerPluginChannelListener(); + } + BStats bStats = new BStats(this); bStats.registerMetrics(); @@ -213,6 +219,11 @@ public class Plan extends BukkitPlugin implements IPlan { } } + private void registerPluginChannelListener() { + Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); + Bukkit.getMessenger().registerIncomingPluginChannel(this, "Plan", new BukkitPluginChannelListener(this)); + } + private void registerTasks() { RunnableFactory runnableFactory = getRunnableFactory(); String bootAnalysisMsg = Locale.get(Msg.ENABLE_BOOT_ANALYSIS_INFO).toString(); @@ -423,6 +434,9 @@ public class Plan extends BukkitPlugin implements IPlan { public void addToProcessQueue(Processor... processors) { for (Processor processor : processors) { + if (processor == null) { + continue; + } processingQueue.addToQueue(processor); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java b/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java index d42a4b523..3563d9e40 100644 --- a/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java +++ b/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java @@ -14,6 +14,7 @@ import main.java.com.djrapitops.plan.locale.Locale; import main.java.com.djrapitops.plan.locale.Msg; import main.java.com.djrapitops.plan.systems.info.BungeeInformationManager; import main.java.com.djrapitops.plan.systems.info.InformationManager; +import main.java.com.djrapitops.plan.systems.info.pluginchannel.BungeePluginChannelListener; import main.java.com.djrapitops.plan.systems.info.server.BungeeServerInfoManager; import main.java.com.djrapitops.plan.systems.listeners.BungeePlayerListener; import main.java.com.djrapitops.plan.systems.processing.Processor; @@ -85,6 +86,9 @@ public class PlanBungee extends BungeePlugin implements IPlan { return; } + getProxy().registerChannel("Plan"); + registerListener(new BungeePluginChannelListener(this)); + processingQueue = new ProcessingQueue(); registerListener(new BungeePlayerListener(this)); diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/info/pluginchannel/BukkitPluginChannelListener.java b/Plan/src/main/java/com/djrapitops/plan/systems/info/pluginchannel/BukkitPluginChannelListener.java new file mode 100644 index 000000000..19aaed135 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/info/pluginchannel/BukkitPluginChannelListener.java @@ -0,0 +1,47 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package main.java.com.djrapitops.plan.systems.info.pluginchannel; + +import main.java.com.djrapitops.plan.Log; +import main.java.com.djrapitops.plan.Plan; +import org.bukkit.entity.Player; +import org.bukkit.plugin.messaging.PluginMessageListener; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class BukkitPluginChannelListener implements PluginMessageListener { + + private final Plan plugin; + + public BukkitPluginChannelListener(Plan plugin) { + this.plugin = plugin; + } + + @Override + public void onPluginMessageReceived(String channel, Player player, byte[] message) { + try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(message))) { + String subChannel = in.readUTF(); + String address = in.readUTF(); + + if ("bungee_address".equals(subChannel)) { + plugin.getServerInfoManager().saveBungeeConnectionAddress(address); + Log.info("-----------------------------------"); + Log.info("Recieved Bungee WebServer address through plugin channel, restarting Plan."); + Log.info("-----------------------------------"); + plugin.restart(); + notifyAll(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/info/pluginchannel/BungeePluginChannelListener.java b/Plan/src/main/java/com/djrapitops/plan/systems/info/pluginchannel/BungeePluginChannelListener.java new file mode 100644 index 000000000..3ddfc5c6b --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/info/pluginchannel/BungeePluginChannelListener.java @@ -0,0 +1,58 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package main.java.com.djrapitops.plan.systems.info.pluginchannel; + +import main.java.com.djrapitops.plan.Log; +import main.java.com.djrapitops.plan.PlanBungee; +import net.md_5.bungee.api.config.ServerInfo; +import net.md_5.bungee.api.event.PluginMessageEvent; +import net.md_5.bungee.api.plugin.Listener; +import net.md_5.bungee.event.EventHandler; + +import java.io.*; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class BungeePluginChannelListener implements Listener { + + private final PlanBungee plugin; + + public BungeePluginChannelListener(PlanBungee plugin) { + this.plugin = plugin; + } + + @EventHandler + public void onPluginMessage(PluginMessageEvent e) { + if (!e.getTag().equalsIgnoreCase("BungeeCord")) { + return; + } + + try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(e.getData()))) { + String channel = in.readUTF(); // channel we delivered + if (channel.equals("bungee_address_get")) { + ServerInfo server = plugin.getProxy().getPlayer(e.getReceiver().toString()).getServer().getInfo(); + sendToBukkit(server); + } + } catch (IOException e1) { + e1.printStackTrace(); + } + } + + private void sendToBukkit(ServerInfo server) { + try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) { + try (DataOutputStream out = new DataOutputStream(stream)) { + out.writeUTF("bungee_address"); + out.writeUTF(plugin.getWebServer().getAccessAddress()); + } + server.sendData("Return", stream.toByteArray()); + } catch (IOException e) { + Log.toLog(this.getClass().getName(), e); + } + } + +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/listeners/PlanPlayerListener.java b/Plan/src/main/java/com/djrapitops/plan/systems/listeners/PlanPlayerListener.java index 56b0f7b4c..6741655d5 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/listeners/PlanPlayerListener.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/listeners/PlanPlayerListener.java @@ -2,8 +2,10 @@ package main.java.com.djrapitops.plan.systems.listeners; import com.djrapitops.plugin.utilities.player.Fetch; import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.Settings; import main.java.com.djrapitops.plan.data.Session; import main.java.com.djrapitops.plan.systems.cache.DataCache; +import main.java.com.djrapitops.plan.systems.processing.info.BungeePluginChannelSenderProcessor; import main.java.com.djrapitops.plan.systems.processing.info.NetworkPageUpdateProcessor; import main.java.com.djrapitops.plan.systems.processing.player.*; import main.java.com.djrapitops.plan.utilities.MiscUtils; @@ -93,14 +95,16 @@ public class PlanPlayerListener implements Listener { int playersOnline = plugin.getTpsCountTimer().getLatestPlayersOnline(); + BungeePluginChannelSenderProcessor bungeePluginChannelSenderProcessor = null; + if (!plugin.getInfoManager().isUsingAnotherWebServer() && Settings.BUNGEE_OVERRIDE_STANDALONE_MODE.isFalse()) { + bungeePluginChannelSenderProcessor = new BungeePluginChannelSenderProcessor(player); + } cache.cacheSession(uuid, Session.start(time, world, gm)); - - IPUpdateProcessor ipUpdateProcessor = new IPUpdateProcessor(uuid, ip); - NameProcessor nameProcessor = new NameProcessor(uuid, playerName, displayName); plugin.addToProcessQueue( new RegisterProcessor(uuid, player.getFirstPlayed(), time, playerName, playersOnline, - ipUpdateProcessor, - nameProcessor + new IPUpdateProcessor(uuid, ip), + new NameProcessor(uuid, playerName, displayName), + bungeePluginChannelSenderProcessor ), new NetworkPageUpdateProcessor(plugin.getInfoManager()) ); diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/processing/info/BungeePluginChannelSenderProcessor.java b/Plan/src/main/java/com/djrapitops/plan/systems/processing/info/BungeePluginChannelSenderProcessor.java new file mode 100644 index 000000000..a7501a1a5 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/processing/info/BungeePluginChannelSenderProcessor.java @@ -0,0 +1,38 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package main.java.com.djrapitops.plan.systems.processing.info; + +import main.java.com.djrapitops.plan.Log; +import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.systems.processing.Processor; +import org.bukkit.entity.Player; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class BungeePluginChannelSenderProcessor extends Processor { + + public BungeePluginChannelSenderProcessor(Player object) { + super(object); + } + + @Override + public void process() { + try (ByteArrayOutputStream b = new ByteArrayOutputStream()) { + try (DataOutputStream out = new DataOutputStream(b)) { + out.writeUTF("bungee_address_get"); + object.sendPluginMessage(Plan.getInstance(), "BungeeCord", b.toByteArray()); + } + } catch (IOException e) { + Log.toLog(this.getClass().getName(), e); + } + } +} \ No newline at end of file