From 40167877e3e048fa3a75c3cf281e3cd1386f12c9 Mon Sep 17 00:00:00 2001 From: lax1dude Date: Tue, 7 May 2024 15:01:17 -0700 Subject: [PATCH] bungee impl almost complete --- .../bungee/EaglerMOTDConnectionBungee.java | 2 +- .../bungee/EaglerMOTDListenerBungee.java | 25 ++++++++++- .../bungee/EaglerMOTDPluginBungee.java | 42 ++++++++++++++++++- .../command/CommandMOTDReloadBungee.java | 32 +++++++++++++- 4 files changed, 97 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/EaglerMOTDConnectionBungee.java b/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/EaglerMOTDConnectionBungee.java index ab4c3fa..64961ba 100644 --- a/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/EaglerMOTDConnectionBungee.java +++ b/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/EaglerMOTDConnectionBungee.java @@ -29,7 +29,7 @@ import net.md_5.bungee.api.connection.ProxiedPlayer; * POSSIBILITY OF SUCH DAMAGE. * */ -public class EaglerMOTDConnectionBungee implements EaglerMOTDConnectionAdapter { +public class EaglerMOTDConnectionBungee implements EaglerMOTDConnectionAdapter { private final MOTDConnection con; private String listenerString = null; diff --git a/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/EaglerMOTDListenerBungee.java b/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/EaglerMOTDListenerBungee.java index 2826558..f12b530 100644 --- a/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/EaglerMOTDListenerBungee.java +++ b/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/EaglerMOTDListenerBungee.java @@ -1,5 +1,6 @@ package net.lax1dude.eaglercraft.v1_8.plugin.eaglermotd.bungee; +import net.lax1dude.eaglercraft.v1_8.plugin.eaglermotd.EaglerMOTDConnectionUpdater; import net.lax1dude.eaglercraft.v1_8.plugin.gateway_bungeecord.api.event.EaglercraftMOTDEvent; import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.event.EventHandler; @@ -21,9 +22,31 @@ import net.md_5.bungee.event.EventHandler; */ public class EaglerMOTDListenerBungee implements Listener { + private final EaglerMOTDPluginBungee plugin; + + public EaglerMOTDListenerBungee(EaglerMOTDPluginBungee plugin) { + this.plugin = plugin; + } + @EventHandler public void handleMOTDEvent(EaglercraftMOTDEvent evt) { - + if(!evt.getAccept().equalsIgnoreCase("motd") && !evt.getAccept().equalsIgnoreCase("motd.noicon")) { + return; + } + EaglerMOTDConnectionUpdater con = new EaglerMOTDConnectionUpdater(plugin.conf, + EaglerMOTDPluginBungee.getListenerName(evt.getListener()), evt.getListener().getMaxPlayer(), + new EaglerMOTDConnectionBungee(evt.getConnection())); + if(con.execute()) { + synchronized(plugin.motdConnections) { + if(plugin.conf.max_total_sockets > 0) { + while(plugin.motdConnections.size() >= plugin.conf.max_total_sockets) { + EaglerMOTDConnectionUpdater c = plugin.motdConnections.remove(plugin.motdConnections.size() - 1); + c.close(); + } + } + plugin.motdConnections.add(0, con); + } + } } } diff --git a/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/EaglerMOTDPluginBungee.java b/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/EaglerMOTDPluginBungee.java index d4c5527..2cf0128 100644 --- a/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/EaglerMOTDPluginBungee.java +++ b/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/EaglerMOTDPluginBungee.java @@ -1,7 +1,12 @@ package net.lax1dude.eaglercraft.v1_8.plugin.eaglermotd.bungee; import java.io.File; +import java.io.IOException; +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.InetSocketAddress; import java.util.ArrayList; +import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -14,7 +19,9 @@ import net.lax1dude.eaglercraft.v1_8.plugin.eaglermotd.EaglerMOTDConfiguration; import net.lax1dude.eaglercraft.v1_8.plugin.eaglermotd.EaglerMOTDConnectionUpdater; import net.lax1dude.eaglercraft.v1_8.plugin.eaglermotd.EaglerMOTDLoggerAdapter; import net.lax1dude.eaglercraft.v1_8.plugin.eaglermotd.bungee.command.CommandMOTDReloadBungee; +import net.lax1dude.eaglercraft.v1_8.plugin.gateway_bungeecord.EaglerXBungee; import net.lax1dude.eaglercraft.v1_8.plugin.gateway_bungeecord.api.query.EaglerQueryHandler; +import net.lax1dude.eaglercraft.v1_8.plugin.gateway_bungeecord.config.EaglerListenerConfig; import net.md_5.bungee.api.plugin.Plugin; /** @@ -68,7 +75,12 @@ public class EaglerMOTDPluginBungee extends Plugin { if(!dataFolder.isDirectory() && !dataFolder.mkdirs()) { throw new RuntimeException("Could not create config folder!"); } - conf.reload(dataFolder, loggerAdapter); + + try { + conf.reload(dataFolder, loggerAdapter, getListenerNames()); + }catch(IOException ex) { + throw new RuntimeException("Could not reload config!", ex); + } } public void installQueryHandlers() { @@ -85,8 +97,34 @@ public class EaglerMOTDPluginBungee extends Plugin { installedQueries.clear(); } + public Collection getListenerNames() { + Collection figs = EaglerXBungee.getEagler().getConfig().getServerListeners(); + Collection ret = new ArrayList(figs.size()); + for(EaglerListenerConfig el : figs) { + ret.add(getListenerName(el)); + } + return ret; + } + + public static String getListenerName(EaglerListenerConfig listenerConf) { + InetSocketAddress sockAddr = listenerConf.getAddress(); + if(sockAddr == null) { + sockAddr = listenerConf.getAddressV6(); + if(sockAddr == null) { + throw new RuntimeException("Listener doesn't have an address: " + listenerConf); + } + } + InetAddress addr = sockAddr.getAddress(); + if(addr instanceof Inet6Address) { + return "[" + addr.getHostAddress() + "]:" + sockAddr.getPort(); + }else { + return addr.getHostAddress() + ":" + sockAddr.getPort(); + } + } + @Override public void onEnable() { + getProxy().getPluginManager().registerListener(this, new EaglerMOTDListenerBungee(this)); getProxy().getPluginManager().registerCommand(this, new CommandMOTDReloadBungee(this)); installQueryHandlers(); if(tickTimer == null) { @@ -117,6 +155,8 @@ public class EaglerMOTDPluginBungee extends Plugin { @Override public void onDisable() { + getProxy().getPluginManager().unregisterListeners(this); + getProxy().getPluginManager().unregisterCommands(this); removeQueryHandlers(); if(tickTimer != null) { tickTimer.cancel(); diff --git a/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/command/CommandMOTDReloadBungee.java b/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/command/CommandMOTDReloadBungee.java index b8918a7..1b10dfa 100644 --- a/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/command/CommandMOTDReloadBungee.java +++ b/src/main/java/net/lax1dude/eaglercraft/v1_8/plugin/eaglermotd/bungee/command/CommandMOTDReloadBungee.java @@ -1,7 +1,12 @@ package net.lax1dude.eaglercraft.v1_8.plugin.eaglermotd.bungee.command; +import java.util.logging.Level; + +import net.lax1dude.eaglercraft.v1_8.plugin.eaglermotd.EaglerMOTDLoggerAdapter; import net.lax1dude.eaglercraft.v1_8.plugin.eaglermotd.bungee.EaglerMOTDPluginBungee; +import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.plugin.Command; /** @@ -30,7 +35,32 @@ public class CommandMOTDReloadBungee extends Command { @Override public void execute(CommandSender arg0, String[] arg1) { - + try { + plugin.removeQueryHandlers(); + plugin.conf.reload(plugin.getDataFolder(), new EaglerMOTDLoggerAdapter() { + @Override + public void info(String msg) { + CommandMOTDReloadBungee.this.plugin.getLogger().info(msg); + arg0.sendMessage(new TextComponent(ChatColor.GREEN + "[EaglerMOTD] " + msg)); + } + + @Override + public void warn(String msg) { + CommandMOTDReloadBungee.this.plugin.getLogger().warning(msg); + arg0.sendMessage(new TextComponent(ChatColor.YELLOW + "[EaglerMOTD] " + msg)); + } + + @Override + public void error(String msg) { + CommandMOTDReloadBungee.this.plugin.getLogger().severe(msg); + arg0.sendMessage(new TextComponent(ChatColor.RED + "[EaglerMOTD] " + msg)); + } + }, plugin.getListenerNames()); + plugin.installQueryHandlers(); + }catch(Throwable ex) { + arg0.sendMessage(new TextComponent(ChatColor.RED + "[EaglerMOTD] Failed to reload! " + ex.toString())); + plugin.getLogger().log(Level.SEVERE, "Exception thrown while reloading config!", ex); + } } }