1
0
Fork 0
forked from mc/VTools
This commit is contained in:
JerryXiao 2023-05-19 18:47:01 +08:00
parent 9072e19d91
commit 0c6bfe6491
Signed by untrusted user: Jerry
GPG key ID: 22618F758B5BE2E5
5 changed files with 212 additions and 2 deletions

25
pom.xml
View file

@ -17,6 +17,26 @@
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>com.velocitypowered:velocity-api</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
@ -34,6 +54,11 @@
<version>3.2.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.pengrad</groupId>
<artifactId>java-telegram-bot-api</artifactId>
<version>6.7.0</version>
</dependency>
</dependencies>
</project>

View file

@ -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() {

View file

@ -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");
}
}

View file

@ -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()));
}
}

View file

@ -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<String, String> 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<SendMessage, SendResponse>() {
@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()));
}
}