attempt to implement execution of console commands

This commit is contained in:
MasterCake 2021-05-04 02:47:56 +02:00
parent 61ee137c95
commit 27097fae67
4 changed files with 133 additions and 5 deletions

15
pom.xml
View File

@ -16,17 +16,26 @@
<id>spigot-repo</id> <id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url> <url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository> </repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.bukkit</groupId>
<artifactId>spigot-api</artifactId> <artifactId>craftbukkit</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version> <version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<!-- Uses the properties in this file for plugin.yml and config.yml --> <!-- Uses the properties in this file for plugin.yml and config.yml -->
<resources> <resources>

View File

@ -0,0 +1,113 @@
package de.Linus122.Handlers;
import java.security.Permissions;
import java.util.Arrays;
import java.util.Locale;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginLogger;
import org.bukkit.plugin.RegisteredServiceProvider;
import de.Linus122.Telegram.Telegram;
import de.Linus122.Telegram.TelegramActionListener;
import de.Linus122.TelegramChat.TelegramChat;
import de.Linus122.TelegramComponents.ChatMessageToMc;
import de.Linus122.TelegramComponents.ChatMessageToTelegram;
import net.milkbowl.vault.permission.Permission;
public class CommandHandler extends ConsoleHandler implements TelegramActionListener {
private Permission permissionsAdapter;
private int lastChatId = -1;
private long lastCommandTyped;
private Telegram telegram;
private Plugin plugin;
public CommandHandler(Telegram telegram, Plugin plugin) {
java.util.logging.Logger global = java.util.logging.Logger.getLogger("");
//global.addHandler(this);
LogManager.getLogManager().getLoggerNames().asIterator().forEachRemaining(c -> LogManager.getLogManager().getLogger(c).addHandler(this));
//Bukkit.getLogger().addHandler(this);
//Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(k -> k.getLogger().addHandler(this));
setupVault();
this.telegram = telegram;
this.plugin = plugin;
}
private void setupVault() {
RegisteredServiceProvider<Permission> rsp = Bukkit.getServer().getServicesManager().getRegistration(Permission.class);
permissionsAdapter = rsp.getProvider();
}
@Override
public void onSendToTelegram(ChatMessageToTelegram chat) {
// TODO Auto-generated method stub
}
@Override
public void onSendToMinecraft(ChatMessageToMc chatMsg) {
if(permissionsAdapter == null) {
// setting up vault permissions
this.setupVault();
}
if(chatMsg.getContent().startsWith("/")) {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(chatMsg.getUuid_sender());
if(permissionsAdapter.playerHas(null, offlinePlayer, "telegramchat.console")) {
lastChatId = chatMsg.getChatID_sender();
lastCommandTyped = System.currentTimeMillis();
Bukkit.getScheduler().runTask(this.plugin, () -> {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), chatMsg.getContent().substring(1, chatMsg.getContent().length()-1));
});
}
}
}
@Override
public void close() throws SecurityException {
// TODO Auto-generated method stub
}
@Override
public void flush() {
// TODO Auto-generated method stub
}
@Override
public void publish(LogRecord record) {
if(lastChatId != -1) {
// String s = String.format(record.getMessage(), record.getParameters());
String s = this.getFormatter().format(record);
telegram.sendMsg(lastChatId, s);
}
}
}

View File

@ -26,6 +26,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import com.google.gson.Gson; import com.google.gson.Gson;
import de.Linus122.Handlers.BanHandler; import de.Linus122.Handlers.BanHandler;
import de.Linus122.Handlers.CommandHandler;
import de.Linus122.Metrics.Metrics; import de.Linus122.Metrics.Metrics;
import de.Linus122.Telegram.Telegram; import de.Linus122.Telegram.Telegram;
import de.Linus122.Telegram.Utils; import de.Linus122.Telegram.Utils;
@ -85,7 +86,11 @@ public class TelegramChat extends JavaPlugin implements Listener {
telegramHook = new Telegram(); telegramHook = new Telegram();
telegramHook.auth(data.getToken()); telegramHook.auth(data.getToken());
// Ban Handler (Prevents banned players from chatting)
telegramHook.addListener(new BanHandler()); telegramHook.addListener(new BanHandler());
// Console sender handler, allows players to send console commands (telegram.console permission)
// telegramHook.addListener(new CommandHandler(telegramHook, this));
Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> { Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> {
boolean connectionLost = false; boolean connectionLost = false;

View File

@ -1,7 +1,8 @@
name: TelegramChat name: TelegramChat
main: de.Linus122.TelegramChat.Main main: de.Linus122.TelegramChat.TelegramChat
version: ${project.version} version: ${project.version}
authors: [Linus122] authors: [Linus122]
softdepend: [Vault]
description: Brings minecraft chat to Telegram! description: Brings minecraft chat to Telegram!
commands: commands:
telegram: telegram:
@ -11,4 +12,4 @@ commands:
linktelegram: linktelegram:
description: Main command description: Main command
usage: /<command> usage: /<command>
permission: telegram.linktelegram permission: telegram.linktelegram