From 0c6bfe6491352ef01991ee9e1f332c9133999028 Mon Sep 17 00:00:00 2001 From: Jerry Date: Fri, 19 May 2023 18:47:01 +0800 Subject: [PATCH] TGBridge --- pom.xml | 25 ++++ src/main/java/de/strifel/VTools/VTools.java | 8 +- .../VTools/commands/CommandGlobalChat.java | 2 +- .../strifel/VTools/listeners/GlobalChat.java | 44 ++++++ .../de/strifel/VTools/listeners/TGBridge.java | 135 ++++++++++++++++++ 5 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/strifel/VTools/listeners/GlobalChat.java create mode 100644 src/main/java/de/strifel/VTools/listeners/TGBridge.java diff --git a/pom.xml b/pom.xml index 8305827..ec4a81f 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,26 @@ 17 + + org.apache.maven.plugins + maven-shade-plugin + 3.4.1 + + + package + + shade + + + + + com.velocitypowered:velocity-api + + + + + + @@ -34,6 +54,11 @@ 3.2.0-SNAPSHOT provided + + com.github.pengrad + java-telegram-bot-api + 6.7.0 + diff --git a/src/main/java/de/strifel/VTools/VTools.java b/src/main/java/de/strifel/VTools/VTools.java index 9ff6658..d4e89ff 100644 --- a/src/main/java/de/strifel/VTools/VTools.java +++ b/src/main/java/de/strifel/VTools/VTools.java @@ -3,6 +3,7 @@ package de.strifel.VTools; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.plugin.Plugin; +import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.ProxyServer; import de.strifel.VTools.commands.*; import de.strifel.VTools.listeners.*; @@ -10,18 +11,21 @@ import net.kyori.adventure.text.format.TextColor; import org.slf4j.Logger; import javax.inject.Inject; +import java.nio.file.Path; @Plugin(id = "vtools", name="VTools", version="1.0-SNAPSHOT", description="Some commands!", authors="unnamed") public class VTools { private final ProxyServer server; + public final Path dataDirectory; public static final TextColor COLOR_RED = TextColor.fromCSSHexString("#FF5555"); public static final TextColor COLOR_YELLOW = TextColor.fromCSSHexString("#FFFF55"); public static final TextColor COLOR_ORANGE = TextColor.fromCSSHexString("#FFA500"); @Inject - public VTools(ProxyServer server, Logger logger) { + public VTools(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) { this.server = server; + this.dataDirectory = dataDirectory; } @@ -35,7 +39,9 @@ public class VTools { server.getCommandManager().register("restart", new CommandRestart(server)); server.getCommandManager().register("tps", new CommandTp(server), "jump"); server.getCommandManager().register("servers", new CommandServers(server), "allservers"); + new TGBridge(this).register(); new PlayerStatus(this).register(); + new GlobalChat(this).register(); } public ProxyServer getServer() { diff --git a/src/main/java/de/strifel/VTools/commands/CommandGlobalChat.java b/src/main/java/de/strifel/VTools/commands/CommandGlobalChat.java index f7e0cda..5cfbcdf 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandGlobalChat.java +++ b/src/main/java/de/strifel/VTools/commands/CommandGlobalChat.java @@ -59,6 +59,6 @@ public class CommandGlobalChat implements SimpleCommand { @Override public boolean hasPermission(Invocation invocation) { - return invocation.source().hasPermission("vtools.globalchat"); + return invocation.source().hasPermission("vtools.globalchat.old"); } } diff --git a/src/main/java/de/strifel/VTools/listeners/GlobalChat.java b/src/main/java/de/strifel/VTools/listeners/GlobalChat.java new file mode 100644 index 0000000..7dc4c60 --- /dev/null +++ b/src/main/java/de/strifel/VTools/listeners/GlobalChat.java @@ -0,0 +1,44 @@ +package de.strifel.VTools.listeners; + +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.player.PlayerChatEvent; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import com.velocitypowered.api.proxy.server.RegisteredServer; +import de.strifel.VTools.VTools; +import net.kyori.adventure.text.Component; + +public class GlobalChat { + private final VTools plugin; + private final ProxyServer server; + + public GlobalChat(VTools plugin) { + this.plugin = plugin; + this.server = plugin.getServer(); + } + + public void register() { + server.getEventManager().register(plugin, this); + } + + @Subscribe + public void onPlayerChat(PlayerChatEvent event) { + RegisteredServer senderServer = null; + if (event.getPlayer().getCurrentServer().isPresent()) { + senderServer = event.getPlayer().getCurrentServer().get().getServer(); + if (event.getPlayer().hasPermission("vtools.globalchat")) { + for (Player player : server.getAllPlayers()) { + if (player.hasPermission("vtools.globalchat")) { + if (player.getCurrentServer().isPresent()) { + RegisteredServer receiverServer = player.getCurrentServer().get().getServer(); + if (!senderServer.equals(receiverServer)) { + player.sendMessage(Component.text(String.format("[%s] <%s> %s", senderServer.getServerInfo().getName(), event.getPlayer().getUsername(), event.getMessage()))); + } + } + } + } + } + } + TGBridge.INSTANCE.outbound(String.format("[%s] <%s> %s", senderServer == null ? "null" : senderServer.getServerInfo().getName(), event.getPlayer().getUsername(), event.getMessage())); + } +} diff --git a/src/main/java/de/strifel/VTools/listeners/TGBridge.java b/src/main/java/de/strifel/VTools/listeners/TGBridge.java new file mode 100644 index 0000000..4891cc9 --- /dev/null +++ b/src/main/java/de/strifel/VTools/listeners/TGBridge.java @@ -0,0 +1,135 @@ +package de.strifel.VTools.listeners; + +import com.pengrad.telegrambot.Callback; +import com.pengrad.telegrambot.TelegramBot; +import com.pengrad.telegrambot.UpdatesListener; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.request.SendMessage; +import com.pengrad.telegrambot.response.SendResponse; +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.connection.DisconnectEvent; +import com.velocitypowered.api.event.player.ServerConnectedEvent; +import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import de.strifel.VTools.VTools; +import net.kyori.adventure.text.Component; +import org.yaml.snakeyaml.Yaml; + +import java.io.IOException; +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; + +public class TGBridge { + private final VTools plugin; + private final ProxyServer server; + protected static TGBridge INSTANCE = null; + + private TelegramBot bot; + private String TOKEN = ""; + private long CHAT_ID = 0L; + + public TGBridge(VTools plugin) { + INSTANCE = this; + this.plugin = plugin; + this.server = plugin.getServer(); + } + + public void register() { + server.getEventManager().register(plugin, this); + botInit(); + } + + private void loadConfig() { + try { + File configDir = plugin.dataDirectory.toFile(); + if (!configDir.exists()) { + configDir.mkdir(); + } + File configFile = new File(configDir, "config.yaml"); + if (!configFile.exists()) { + Files.write(Path.of(configFile.toURI()), "chat_id: \"0\"\ntoken: \"\"\n".getBytes(StandardCharsets.UTF_8)); + } + String configStr = Files.readString(Path.of(configFile.toURI()), StandardCharsets.UTF_8); + Yaml yaml = new Yaml(); + Map config = yaml.load(configStr); + synchronized (this) { + this.CHAT_ID = Long.parseLong(config.getOrDefault("chat_id", "0")); + this.TOKEN = config.getOrDefault("token", ""); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void botInit() { + loadConfig(); + if (TOKEN.isEmpty() || CHAT_ID == 0L) return; + bot = new TelegramBot(TOKEN); + bot.setUpdatesListener(updates -> { + for (Update update : updates) { + try { + if (update != null && + update.message() != null && + update.message().chat() != null && + update.message().chat().id() == CHAT_ID && + update.message().text() != null && + !update.message().text().isEmpty() && + update.message().from() != null + ) { + inbound(String.format("[tg] <%s> %s", update.message().from().lastName() == null ? update.message().from().firstName(): String.format("%s %s", update.message().from().firstName(), update.message().from().lastName()), update.message().text())); + } + } + catch (Exception e) { + e.printStackTrace(); + } + } + return UpdatesListener.CONFIRMED_UPDATES_ALL; + }, Throwable::printStackTrace); + } + + protected void inbound(String content) { + for (Player player : server.getAllPlayers()) { + if (player.getCurrentServer().isPresent()) { + player.sendMessage(Component.text(content)); + } + } + } + + protected void outbound(String content) { + if (TOKEN.isEmpty() || CHAT_ID == 0L) return; + if (content.length() > 4000) { + content = content.substring(0, 4000); + } + bot.execute(new SendMessage(CHAT_ID, content), new Callback() { + @Override + public void onResponse(SendMessage sendMessage, SendResponse sendResponse) { + } + + @Override + public void onFailure(SendMessage sendMessage, IOException e) { + } + }); + } + + @Subscribe + public void onProxyShutdown(ProxyShutdownEvent event) { + bot.removeGetUpdatesListener(); + bot.shutdown(); + } + + @Subscribe + public void onServerConnected(ServerConnectedEvent event) { + if (event.getPreviousServer().isEmpty()) { + outbound(String.format("%s joined the proxy", event.getPlayer().getUsername())); + } + } + + @Subscribe + public void onDisconnect(DisconnectEvent event) { + outbound(String.format("%s left the proxy", event.getPlayer().getUsername())); + } +}