From 183ac735797f8f906bf9d1dc5514038b701ee7d7 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Tue, 28 Apr 2020 00:09:05 +1200 Subject: [PATCH] Remove commands and hopefully permissions from registered information if not used, mainly applies to api version and disabled commands. --- .../libraryaddict/disguise/LibsDisguises.java | 39 +++++++++++++++++-- .../disguise/utilities/LibsPremium.java | 2 +- .../reflection/ReflectionManager.java | 32 +++++++++++++++ 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java index 0ed16218..09ad387d 100644 --- a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java @@ -27,9 +27,7 @@ import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import me.libraryaddict.disguise.utilities.reflection.asm.WatcherSanitizer; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.PluginCommand; -import org.bukkit.command.TabCompleter; +import org.bukkit.command.*; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.craftbukkit.libs.org.apache.commons.io.FileUtils; import org.bukkit.entity.Player; @@ -39,6 +37,8 @@ import org.bukkit.util.FileUtil; import java.io.File; import java.io.IOException; import java.util.Arrays; +import java.util.Iterator; +import java.util.Map; import java.util.stream.Collectors; public class LibsDisguises extends JavaPlugin { @@ -161,9 +161,40 @@ public class LibsDisguises extends JavaPlugin { getLogger().info("Commands has been disabled, as per config"); } + unregisterCommands(); + new MetricsInitalizer(); } + private void unregisterCommands() { + CommandMap map = ReflectionManager.getCommandMap(); + Map commands = ReflectionManager.getCommands(map); + + for (String command : getDescription().getCommands().keySet()) { + PluginCommand cmd = getCommand("libsdisguises:" + command); + + if (cmd.getExecutor() != this) { + continue; + } + + if (cmd.getPermission() != null && cmd.getPermission().startsWith("libsdisguises.seecmd")) { + Bukkit.getPluginManager().removePermission(cmd.getPermission()); + } + + Iterator> itel = commands.entrySet().iterator(); + + while (itel.hasNext()) { + Map.Entry entry = itel.next(); + + if (entry.getValue() != cmd) { + continue; + } + + itel.remove(); + } + } + } + @Override public File getFile() { return super.getFile(); @@ -195,7 +226,7 @@ public class LibsDisguises extends JavaPlugin { } private void registerCommand(String commandName, CommandExecutor executioner) { - PluginCommand command = getCommand(commandName); + PluginCommand command = getCommand("libsdisguises:" + commandName); command.setExecutor(executioner); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java b/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java index 51c3425f..8994f450 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java @@ -65,7 +65,7 @@ public class LibsPremium { * @return true if userID does not contain __USER__ */ private static Boolean isPremium(String resourceID, String userID) { - return !userID.contains("__USER__") && !resourceID.equals("81"); + return !userID.contains("__USER__") && resourceID.equals("32453"); } public static Boolean isAPIPlugin() { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java b/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java index a07836c0..8cfd5339 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java @@ -16,12 +16,16 @@ import me.libraryaddict.disguise.utilities.LibsPremium; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.bukkit.*; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.SimpleCommandMap; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.*; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.plugin.SimplePluginManager; import org.bukkit.potion.PotionEffect; import org.bukkit.util.Vector; @@ -1853,6 +1857,34 @@ public class ReflectionManager { } } + public static Map getCommands(CommandMap map) { + try { + Field field = SimpleCommandMap.class.getDeclaredField("knownCommands"); + field.setAccessible(true); + + return (Map) field.get(map); + } + catch (Exception ex) { + ex.printStackTrace(); + } + + return null; + } + + public static SimpleCommandMap getCommandMap() { + try { + Field commandMap = SimplePluginManager.class.getDeclaredField("commandMap"); + commandMap.setAccessible(true); + + return (SimpleCommandMap) commandMap.get(Bukkit.getPluginManager()); + } + catch (Exception ex) { + ex.printStackTrace(); + } + + return null; + } + private static String[] splitReadable(String string) { String[] split = string.split("_");