From 0f81fb82be556f33dba95010b67a21e0d242d078 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sat, 8 May 2021 12:36:18 +0200 Subject: [PATCH] PluginMessage passthrough utility for bungeecord --- .../bungee/util/PluginMessagePassthrough.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Bungee/src/main/java/fr/pandacube/lib/bungee/util/PluginMessagePassthrough.java diff --git a/Bungee/src/main/java/fr/pandacube/lib/bungee/util/PluginMessagePassthrough.java b/Bungee/src/main/java/fr/pandacube/lib/bungee/util/PluginMessagePassthrough.java new file mode 100644 index 0000000..5652f31 --- /dev/null +++ b/Bungee/src/main/java/fr/pandacube/lib/bungee/util/PluginMessagePassthrough.java @@ -0,0 +1,72 @@ +package fr.pandacube.lib.bungee.util; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.connection.Server; +import net.md_5.bungee.api.event.PluginMessageEvent; +import net.md_5.bungee.api.plugin.Listener; +import net.md_5.bungee.api.plugin.Plugin; +import net.md_5.bungee.api.plugin.PluginManager; +import net.md_5.bungee.event.EventHandler; + +public class PluginMessagePassthrough implements Listener { + private static final List channels = Collections.synchronizedList(new ArrayList<>()); + private static final PluginMessagePassthrough instance = new PluginMessagePassthrough(); + + public static void init(Plugin plugin) { + PluginManager pm = ProxyServer.getInstance().getPluginManager(); + pm.unregisterListener(instance); + pm.registerListener(plugin, instance); + } + + public static void clear() { + synchronized (channels) { + new ArrayList<>(channels).forEach(c -> unregister(c)); + } + } + + public static void registerAll(String... channels) { + for (String channel : channels) + register(channel); + } + + public static void unregisterAll(String... channels) { + for (String channel : channels) + unregister(channel); + } + + public static void register(String channel) { + synchronized (channels) { + if (channels.contains(channel)) + return; + ProxyServer.getInstance().registerChannel(channel); + channels.add(channel); + } + } + + public static void unregister(String channel) { + synchronized (channels) { + if (!channels.contains(channel)) + return; + ProxyServer.getInstance().unregisterChannel(channel); + channels.remove(channel); + } + } + + + @EventHandler + public void onPluginMessage(PluginMessageEvent event) { + String channel = event.getTag(); + if (!channels.contains(channel)) + return; + if (event.getReceiver() instanceof ProxiedPlayer) { + ((ProxiedPlayer)event.getReceiver()).sendData(channel, event.getData()); + } else if (event.getReceiver() instanceof Server) { + ((Server)event.getReceiver()).sendData(channel, event.getData()); + } + } +}