From d5a82e600535907359b4742ce68dacc7f360242b Mon Sep 17 00:00:00 2001 From: JaTiTV Date: Mon, 1 Jul 2024 18:15:08 +0200 Subject: [PATCH] Add Velocity ALP Classes --- .../VELOCITY/api/commands/T2CVcmd.java | 32 +++++++++ .../api/pluginMessaging/T2CplmsgBcmd.java | 56 +++++++++++++++ .../autoResponse/T2CapiAutoResponse.java | 56 +++++++++++++++ .../commandgui/T2CVapiCGUI.java | 69 +++++++++++++++++++ .../opSecurity/T2CapiOpSecurity.java | 52 ++++++++++++++ .../VELOCITY/system/T2CodeVMain.java | 37 ++++++++-- 6 files changed, 298 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/t2code/t2codelib/VELOCITY/api/commands/T2CVcmd.java create mode 100644 src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/T2CplmsgBcmd.java create mode 100644 src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/autoResponse/T2CapiAutoResponse.java create mode 100644 src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/commandgui/T2CVapiCGUI.java create mode 100644 src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/opSecurity/T2CapiOpSecurity.java diff --git a/src/main/java/net/t2code/t2codelib/VELOCITY/api/commands/T2CVcmd.java b/src/main/java/net/t2code/t2codelib/VELOCITY/api/commands/T2CVcmd.java new file mode 100644 index 0000000..543de72 --- /dev/null +++ b/src/main/java/net/t2code/t2codelib/VELOCITY/api/commands/T2CVcmd.java @@ -0,0 +1,32 @@ +// This class was created by JaTiTV. + +package net.t2code.t2codelib.VELOCITY.api.commands; + +import com.velocitypowered.api.command.CommandManager; +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.proxy.ConsoleCommandSource; +import com.velocitypowered.api.proxy.ProxyServer; +import net.t2code.t2codelib.VELOCITY.system.T2CodeVMain; +import org.slf4j.Logger; + +public class T2CVcmd { + + private static final ProxyServer server = T2CodeVMain.getServer(); + private static final Logger logger = T2CodeVMain.getLogger(); + + public static void Console(String cmd) { + // Get the console command source + ConsoleCommandSource console = server.getConsoleCommandSource(); + + // Dispatch the command + server.getCommandManager().executeAsync(console, cmd).join(); + } + + public static void Player(CommandSource player, String cmd) { + CommandManager commandManager = server.getCommandManager(); + + // Dispatch the command + commandManager.executeImmediatelyAsync(player, cmd); + } + +} \ No newline at end of file diff --git a/src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/T2CplmsgBcmd.java b/src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/T2CplmsgBcmd.java new file mode 100644 index 0000000..83ec498 --- /dev/null +++ b/src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/T2CplmsgBcmd.java @@ -0,0 +1,56 @@ +// This class was created by JaTiTV. + +package net.t2code.t2codelib.VELOCITY.api.pluginMessaging; + +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteStreams; +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.connection.PluginMessageEvent; +import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import net.t2code.t2codelib.Util; +import net.t2code.t2codelib.VELOCITY.api.commands.T2CVcmd; +import net.t2code.t2codelib.VELOCITY.system.T2CodeVMain; +import org.slf4j.Logger; + +public class T2CplmsgBcmd { + + private static final ProxyServer server = T2CodeVMain.getServer(); + private static final Logger logger = T2CodeVMain.getLogger(); + + + @Subscribe + public void onProxyInitialization(ProxyInitializeEvent event) { + server.getChannelRegistrar().register(T2CodeVMain.bcmd); + } + + @Subscribe + public void onPluginMessageFromPlayer(PluginMessageEvent event) { + if (event.getIdentifier() != T2CodeVMain.bcmd) { + return; + } + ByteArrayDataInput stream = ByteStreams.newDataInput(event.getData()); + String channel = stream.readUTF(); + String input = stream.readUTF(); + String serverID; + try { + serverID = stream.readUTF(); + } catch (Exception i) { + serverID = "not Found"; + } + + if (channel.equals("T2Code-Console")) { + logger.info("{} [{}] T2C BCMD Command Console: {}", Util.getVPrefix(), serverID, input); + T2CVcmd.Console(input); + } else { + Player player = server.getPlayer(channel).orElse(null); + if (player != null) { + logger.info("{} [{}] T2C BCMD Command {}: {}", Util.getVPrefix(), serverID, player, input); + T2CVcmd.Player(player, input); + } + } + event.setResult(PluginMessageEvent.ForwardResult.handled()); + } + +} \ No newline at end of file diff --git a/src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/autoResponse/T2CapiAutoResponse.java b/src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/autoResponse/T2CapiAutoResponse.java new file mode 100644 index 0000000..d578d71 --- /dev/null +++ b/src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/autoResponse/T2CapiAutoResponse.java @@ -0,0 +1,56 @@ +// This class was created by JaTiTV. + +package net.t2code.t2codelib.VELOCITY.api.pluginMessaging.autoResponse; + +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.connection.PluginMessageEvent; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import net.t2code.t2codelib.VELOCITY.api.commands.T2CVcmd; +import net.t2code.t2codelib.VELOCITY.api.messages.T2CVsend; +import net.t2code.t2codelib.VELOCITY.system.T2CodeVMain; +import org.slf4j.Logger; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; + +public class T2CapiAutoResponse { + private static final ProxyServer server = T2CodeVMain.getServer(); + private static final Logger logger = T2CodeVMain.getLogger(); + + @Subscribe + public void onPluginmessage(PluginMessageEvent event) { + if (event.getIdentifier() != T2CodeVMain.aresp) { + return; + } + DataInputStream stream = new DataInputStream(new ByteArrayInputStream(event.getData())); + + try { + String channel = stream.readUTF(); + String input = stream.readUTF(); + + switch (channel) { + case "ConC": + logger.info("Command Console: {}", input); + T2CVcmd.Console(input); + break; + case "ALLPMSG": + for (Player player : server.getAllPlayers()) { + T2CVsend.player(player, input); + } + break; + default: + Player player = server.getPlayer(channel).orElse(null); + if (player != null) { + logger.info("Command Console: {}", input); + T2CVcmd.Player(player, input); + } + break; + } + } catch (IOException e) { + e.printStackTrace(); + } + + } +} diff --git a/src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/commandgui/T2CVapiCGUI.java b/src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/commandgui/T2CVapiCGUI.java new file mode 100644 index 0000000..c4e64bd --- /dev/null +++ b/src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/commandgui/T2CVapiCGUI.java @@ -0,0 +1,69 @@ +// This class was created by JaTiTV. + +package net.t2code.t2codelib.VELOCITY.api.pluginMessaging.commandgui; + +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.connection.DisconnectEvent; +import com.velocitypowered.api.event.connection.PostLoginEvent; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import com.velocitypowered.api.proxy.server.RegisteredServer; +import net.t2code.t2codelib.VELOCITY.system.T2CodeVMain; +import org.slf4j.Logger; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class T2CVapiCGUI { + + private static final ProxyServer server = T2CodeVMain.getServer(); + private static final Logger logger = T2CodeVMain.getLogger(); + + + @Subscribe + public void onJoin(PostLoginEvent event) { + Player player = event.getPlayer(); + sendToSpigotPlayer(player.getUsername(), true); + } + + @Subscribe + public void onDisconnect(DisconnectEvent event) { + Player player = event.getPlayer(); + sendToSpigotPlayer(player.getUsername(), false); + } + + public void sendToSpigotPlayer(String name, Boolean join) { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + DataOutputStream output = new DataOutputStream(stream); + try { + if (join) { + output.writeUTF("join"); + } else { + output.writeUTF("left"); + } + output.writeUTF(name); + } catch (IOException e) { + logger.warn(e.getMessage()); + } + byte[] data = stream.toByteArray(); + for (RegisteredServer server : server.getAllServers()) { + server.sendPluginMessage(T2CodeVMain.cguiopl, data); + } + } + + public static void sendToSpigotDeleteAll() { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + DataOutputStream output = new DataOutputStream(stream); + try { + output.writeUTF("clear"); + output.writeUTF(""); + } catch (IOException e) { + logger.warn(e.getMessage()); + } + byte[] data = stream.toByteArray(); + for (RegisteredServer server : server.getAllServers()) { + server.sendPluginMessage(T2CodeVMain.cguiopl, data); + } + } +} \ No newline at end of file diff --git a/src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/opSecurity/T2CapiOpSecurity.java b/src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/opSecurity/T2CapiOpSecurity.java new file mode 100644 index 0000000..3fe7999 --- /dev/null +++ b/src/main/java/net/t2code/t2codelib/VELOCITY/api/pluginMessaging/opSecurity/T2CapiOpSecurity.java @@ -0,0 +1,52 @@ +// This class was created by JaTiTV. + +package net.t2code.t2codelib.VELOCITY.api.pluginMessaging.opSecurity; + +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.connection.PluginMessageEvent; +import com.velocitypowered.api.proxy.ProxyServer; +import com.velocitypowered.api.proxy.server.RegisteredServer; +import net.t2code.t2codelib.VELOCITY.system.T2CodeVMain; +import org.slf4j.Logger; + +import java.io.*; + +public class T2CapiOpSecurity { + private static final ProxyServer server = T2CodeVMain.getServer(); + private static final Logger logger = T2CodeVMain.getLogger(); + + @Subscribe + public void onPluginmessage(PluginMessageEvent event) { + if (event.getIdentifier() != T2CodeVMain.opsec) { + return; + } + + DataInputStream stream = new DataInputStream(new ByteArrayInputStream(event.getData())); + try { + String channel = stream.readUTF(); + String mode = stream.readUTF(); + String information = stream.readUTF(); + if (channel.equals("T2Cconsole")) { + sendToSpigotPlayer(channel,mode,information); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + private static void sendToSpigotPlayer(String channel,String mode,String information) { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + DataOutputStream output = new DataOutputStream(stream); + try { + output.writeUTF(channel); + output.writeUTF(mode); + output.writeUTF(information); + } catch (IOException e) { + logger.error(e.getMessage()); + } + for (RegisteredServer server : server.getAllServers()) { + server.sendPluginMessage(T2CodeVMain.opsec, stream.toByteArray()); + } + } +} \ No newline at end of file diff --git a/src/main/java/net/t2code/t2codelib/VELOCITY/system/T2CodeVMain.java b/src/main/java/net/t2code/t2codelib/VELOCITY/system/T2CodeVMain.java index 781c50d..8fe4cae 100644 --- a/src/main/java/net/t2code/t2codelib/VELOCITY/system/T2CodeVMain.java +++ b/src/main/java/net/t2code/t2codelib/VELOCITY/system/T2CodeVMain.java @@ -8,6 +8,7 @@ import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.ProxyServer; +import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; import lombok.Getter; import net.t2code.t2codelib.Util; @@ -21,7 +22,8 @@ import java.nio.file.Path; public class T2CodeVMain { - private final ProxyServer server; + @Getter + private static ProxyServer server; @Getter private static Logger logger; @Getter @@ -34,10 +36,17 @@ public class T2CodeVMain { private final Metrics.Factory metricsFactory; + public static final MinecraftChannelIdentifier bcmd = MinecraftChannelIdentifier.from("t2c:bcmd"); + public static final MinecraftChannelIdentifier bonlp = MinecraftChannelIdentifier.from("t2c:bonlp"); + public static final MinecraftChannelIdentifier cguiopl = MinecraftChannelIdentifier.from("t2c:cguiopl"); + public static final MinecraftChannelIdentifier aresp = MinecraftChannelIdentifier.from("t2c:aresp"); + public static final MinecraftChannelIdentifier opsec = MinecraftChannelIdentifier.from("t2c:opsec"); + + @Inject public T2CodeVMain(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory, Metrics.Factory metricsFactory) { starttime = System.currentTimeMillis(); - this.server = server; + T2CodeVMain.server = server; T2CodeVMain.logger = logger; T2CodeVMain.dataDirectory = dataDirectory; this.metricsFactory = metricsFactory; @@ -69,12 +78,32 @@ public class T2CodeVMain { // new T2CVcmd(server, logger); // server.getEventManager().register(this, new T2CVpluginMessagingCmd(server, logger)); + //API + server.getChannelRegistrar().register(T2CodeVMain.bcmd); + + server.getChannelRegistrar().register(T2CodeVMain.bonlp); + T2CVPlayers.sendToSpigotDeleteAll(); + + // if (T2CVlibConfig.getApiCommandGUIEnable()) { + server.getChannelRegistrar().register(T2CodeVMain.cguiopl); + T2CVapiCGUI.sendToSpigotDeleteAll(); + // } + + // if (T2CVlibConfig.getApiAutoResponse()) { + server.getChannelRegistrar().register(T2CodeVMain.aresp); + // } + + // if (T2CVlibConfig.getApiOpSecurity()) { + server.getChannelRegistrar().register(T2CodeVMain.opsec); + //} + + + Metrics.bStats(this, Util.getBstatsID(), metricsFactory); + T2CVupdateAPI.onUpdateCheckTimer(logger, plugin, server, Util.getVPrefix(), Util.getDiscord(), Util.getSpigotID(), Util.getGit()); logger.info("║"); logger.info("║ Plugin loaded successfully - {}ms", System.currentTimeMillis() - starttime); logger.info("╚════════════════════════════════════"); - Metrics.bStats(this, Util.getBstatsID(), metricsFactory); - T2CVupdateAPI.onUpdateCheckTimer(logger, plugin, server, Util.getVPrefix(), Util.getDiscord(), Util.getSpigotID(), Util.getGit()); }