From ca18d17c13d5412e244df6da53321c9b97cd0628 Mon Sep 17 00:00:00 2001 From: youhavetrouble Date: Sun, 2 Aug 2020 21:28:12 +0200 Subject: [PATCH] 1.8 - 1.12 support --- pom.xml | 11 +- .../spigot/CommandWhitelist.java | 57 ++++++++- .../LegacyPlayerTabChatCompleteListener.java | 110 ++++++++++++++++++ src/main/resources/plugin.yml | 3 +- 4 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 src/main/java/eu/endermite/commandwhitelist/spigot/listeners/LegacyPlayerTabChatCompleteListener.java diff --git a/pom.xml b/pom.xml index 31c101b..a82c44b 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ eu.endermite CommandWhitelist - 1.2.0 + 1.3.0 jar CommandWhitelist @@ -66,6 +66,10 @@ jitpack.io https://jitpack.io + + dmulloy2-repo + https://repo.dmulloy2.net/nexus/repository/public/ + @@ -87,5 +91,10 @@ 1.15-SNAPSHOT provided + + com.comphenix.protocol + ProtocolLib + 4.5.0 + diff --git a/src/main/java/eu/endermite/commandwhitelist/spigot/CommandWhitelist.java b/src/main/java/eu/endermite/commandwhitelist/spigot/CommandWhitelist.java index 7d4331f..ee0c8c6 100644 --- a/src/main/java/eu/endermite/commandwhitelist/spigot/CommandWhitelist.java +++ b/src/main/java/eu/endermite/commandwhitelist/spigot/CommandWhitelist.java @@ -1,7 +1,10 @@ package eu.endermite.commandwhitelist.spigot; +import com.comphenix.protocol.ProtocolLib; +import com.comphenix.protocol.ProtocolLibrary; import eu.endermite.commandwhitelist.spigot.command.MainCommand; import eu.endermite.commandwhitelist.spigot.config.ConfigCache; +import eu.endermite.commandwhitelist.spigot.listeners.LegacyPlayerTabChatCompleteListener; import eu.endermite.commandwhitelist.spigot.listeners.PlayerCommandPreProcessListener; import eu.endermite.commandwhitelist.spigot.listeners.PlayerCommandSendListener; import org.bukkit.Bukkit; @@ -14,15 +17,30 @@ public class CommandWhitelist extends JavaPlugin { private static CommandWhitelist commandWhitelist; private static ConfigCache configCache; + private static boolean isLegacy; @Override public void onEnable() { commandWhitelist = this; + + isLegacy = checkLegacy(); + + reloadPluginConfig(); getServer().getPluginManager().registerEvents(new PlayerCommandPreProcessListener(), this); - getServer().getPluginManager().registerEvents(new PlayerCommandSendListener(), this); + if (!isLegacy) { + getServer().getPluginManager().registerEvents(new PlayerCommandSendListener(), this); + } else { + getLogger().info(ChatColor.AQUA+"Running in legacy mode..."); + if (getServer().getPluginManager().getPlugin("ProtocolLib") != null) { + LegacyPlayerTabChatCompleteListener.protocol(this); + } else { + getLogger().info(ChatColor.YELLOW+"ProtocolLib is required for tab completion blocking!"); + } + } + getCommand("commandwhitelist").setExecutor(new MainCommand()); getCommand("commandwhitelist").setTabCompleter(new MainCommand()); @@ -37,13 +55,46 @@ public void reloadPluginConfig() { public void reloadPluginConfig(CommandSender sender) { getServer().getScheduler().runTaskAsynchronously(this, () -> { reloadPluginConfig(); - for (Player p : Bukkit.getOnlinePlayers()) { - p.updateCommands(); + if (!isLegacy()) { + for (Player p : Bukkit.getOnlinePlayers()) { + p.updateCommands(); + } } sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelist.getConfigCache().getPrefix() + CommandWhitelist.getConfigCache().getConfigReloaded())); }); } + public boolean isLegacy() { + return isLegacy; + } + + private boolean checkLegacy() { + + String version = getServer().getVersion(); + + if (version.contains("1.8")) { + return true; + } else if (version.contains("1.9")) { + return true; + } else if (version.contains("1.10")) { + return true; + } else if (version.contains("1.11")) { + return true; + } else if (version.contains("1.12")) { + return true; + } else if (version.contains("1.13")) { + return false; + } else if (version.contains("1.14")) { + return false; + } else if (version.contains("1.15")) { + return false; + } else if (version.contains("1.16")) { + return false; + } + + return false; + } + public static CommandWhitelist getPlugin() {return commandWhitelist;} public static ConfigCache getConfigCache() {return configCache;} } diff --git a/src/main/java/eu/endermite/commandwhitelist/spigot/listeners/LegacyPlayerTabChatCompleteListener.java b/src/main/java/eu/endermite/commandwhitelist/spigot/listeners/LegacyPlayerTabChatCompleteListener.java new file mode 100644 index 0000000..3cc1a9d --- /dev/null +++ b/src/main/java/eu/endermite/commandwhitelist/spigot/listeners/LegacyPlayerTabChatCompleteListener.java @@ -0,0 +1,110 @@ +package eu.endermite.commandwhitelist.spigot.listeners; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.ProtocolManager; +import com.comphenix.protocol.events.ListenerPriority; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.events.PacketEvent; +import eu.endermite.commandwhitelist.spigot.CommandWhitelist; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class LegacyPlayerTabChatCompleteListener { + + public static void protocol(CommandWhitelist plugin) { + ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager(); + tabCompleteServerBound(protocolManager, plugin); + tabCompleteClientBound(protocolManager, plugin); + } + + public static void tabCompleteServerBound(ProtocolManager protocolManager, Plugin plugin) { + protocolManager.addPacketListener(new PacketAdapter(plugin, ListenerPriority.HIGHEST, PacketType.Play.Server.TAB_COMPLETE) { + @Override + public void onPacketSending(PacketEvent event) { + + try { + Player player = event.getPlayer(); + if (player.hasPermission("commandwhitelist.bypass")) { + return; + } + PacketContainer packet = event.getPacket(); + String[] message = packet.getSpecificModifier(String[].class).read(0); + + List commandList = new ArrayList<>(); + for (Map.Entry> s : CommandWhitelist.getConfigCache().getPermList().entrySet()) { + if (player.hasPermission("commandwhitelist.commands." + s.getKey())) { + commandList.addAll(s.getValue()); + } + } + + List finalList = new ArrayList<>(); + int components = 0; + + for (String cmd : message) { + for (String cmdFromList : commandList) { + if (cmd.equalsIgnoreCase("/" + cmdFromList) || !cmd.startsWith("/")) { + finalList.add(components++, cmd); + break; + } + } + } + + String[] toWrite = new String[components]; + int counter = 0; + for (String cmd : finalList) { + toWrite[counter++] = cmd; + } + + packet.getSpecificModifier(String[].class).write(0, toWrite); + + } catch (Exception e) { + e.printStackTrace(); + } + + } + }); + } + + public static void tabCompleteClientBound(ProtocolManager protocolManager, Plugin plugin) { + protocolManager.addPacketListener(new PacketAdapter(plugin, ListenerPriority.HIGHEST, PacketType.Play.Client.TAB_COMPLETE) { + @Override + public void onPacketReceiving(PacketEvent event) { + try { + Player player = event.getPlayer(); + if (player.hasPermission("commandwhitelist.bypass")) { + return; + } + PacketContainer packet = event.getPacket(); + String command = packet.getSpecificModifier(String.class).read(0); + System.out.println(command); + + for (Map.Entry> s : CommandWhitelist.getConfigCache().getPermList().entrySet()) { + if (player.hasPermission("commandwhitelist.commands." + s.getKey())) { + for (String comm : s.getValue()) { + comm = comm.toLowerCase(); + if (command.equalsIgnoreCase("/"+comm)) + return; + else if (command.startsWith("/" + comm + " ")) { + return; + } + } + } + } + event.setCancelled(true); + + + } catch (Exception e) { + e.printStackTrace(); + } + + } + }); + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index bf674e0..6297f6d 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,8 +1,9 @@ name: CommandWhitelist version: ${project.version} main: eu.endermite.commandwhitelist.spigot.CommandWhitelist -api-version: 1.13 authors: [YouHaveTrouble] +softdepend: + - ProtocolLib description: Control what commands players can use commands: commandwhitelist: