diff --git a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java index 8479b45a..c9b74fa7 100644 --- a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java @@ -18,6 +18,7 @@ import me.libraryaddict.disguise.commands.undisguise.UndisguiseRadiusCommand; import me.libraryaddict.disguise.commands.utils.*; import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.LibsPremium; +import me.libraryaddict.disguise.utilities.config.DisguiseCommandConfig; import me.libraryaddict.disguise.utilities.listeners.DisguiseListener; import me.libraryaddict.disguise.utilities.listeners.PaperDisguiseListener; import me.libraryaddict.disguise.utilities.listeners.PlayerSkinHandler; @@ -54,6 +55,7 @@ public class LibsDisguises extends JavaPlugin { private final UpdateChecker updateChecker = new UpdateChecker(); @Getter private PlayerSkinHandler skinHandler; + private DisguiseCommandConfig commandConfig; @Override public void onLoad() { @@ -95,6 +97,12 @@ public class LibsDisguises extends JavaPlugin { "plugin will continue to load, but it will look like a mugging victim"); } + commandConfig = new DisguiseCommandConfig(); + + if (!reloaded) { + commandConfig.load(); + } + WatcherSanitizer.init(); } @@ -239,7 +247,7 @@ public class LibsDisguises extends JavaPlugin { for (String command : getDescription().getCommands().keySet()) { PluginCommand cmd = getCommand("libsdisguises:" + command); - if (cmd.getExecutor() != this && !force) { + if (cmd == null || (cmd.getExecutor() != this && !force)) { continue; } @@ -292,7 +300,17 @@ public class LibsDisguises extends JavaPlugin { } private void registerCommand(String commandName, CommandExecutor executioner) { - PluginCommand command = getCommand("libsdisguises:" + commandName); + String name = commandConfig.getCommand(commandName); + + if (name == null) { + return; + } + + PluginCommand command = getCommand("libsdisguises:" + name); + + if (command == null) { + return; + } command.setExecutor(executioner); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/config/DisguiseCommandConfig.java b/src/main/java/me/libraryaddict/disguise/utilities/config/DisguiseCommandConfig.java new file mode 100644 index 00000000..ff2a062b --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/utilities/config/DisguiseCommandConfig.java @@ -0,0 +1,190 @@ +package me.libraryaddict.disguise.utilities.config; + +import com.google.common.base.Strings; +import lombok.Getter; +import lombok.Setter; +import me.libraryaddict.disguise.LibsDisguises; +import me.libraryaddict.disguise.utilities.DisguiseUtilities; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.PluginDescriptionFile; + +import java.io.*; +import java.lang.reflect.Field; +import java.util.*; + +/** + * Created by libraryaddict on 28/01/2021. + */ +public class DisguiseCommandConfig { + @Getter + @Setter + public class DisguiseCommand { + private String name; + private String description; + private String permission; + private List aliases = new ArrayList<>(); + private boolean enabled; + } + + private File commandConfig = new File(LibsDisguises.getInstance().getDataFolder(), "commands.yml"); + private HashMap commands = new HashMap<>(); + private boolean modifyCommands = false; + + private void loadConfig() { + if (!commandConfig.exists()) { + return; + } + + YamlConfiguration config = YamlConfiguration.loadConfiguration(commandConfig); + + for (String name : config.getKeys(false)) { + DisguiseCommand command = commands.get(name); + + if (command == null) { + DisguiseUtilities.getLogger().warning("Unable to find a command '" + name + "'"); + continue; + } + + if (!config.isConfigurationSection(name)) { + DisguiseUtilities.getLogger().warning("Improper config for " + name); + continue; + } + + ConfigurationSection section = config.getConfigurationSection(name); + + if (!name.equals("libsdisguises")) { + command.setEnabled(section.getBoolean("enabled", true)); + command.setName(section.getString("name", name)); + command.setPermission(section.getString("permission", command.getPermission())); + } + + command.setDescription(section.getString("description", command.getDescription())); + + if (section.contains("aliases")) { + command.setAliases(new ArrayList<>(section.getStringList("aliases"))); + } + } + + modifyCommands = config.getBoolean("ModifyCommands", false); + } + + private void saveConfig() { + YamlConfiguration config = new YamlConfiguration(); + + for (Map.Entry entry : commands.entrySet()) { + ConfigurationSection section = config.createSection(entry.getKey()); + + DisguiseCommand command = entry.getValue(); + + if (!command.getName().equals("libsdisguises")) { + section.set("name", command.getName()); + section.set("permission", command.getPermission()); + section.set("enabled", command.isEnabled()); + } + + section.set("description", command.getDescription()); + section.set("aliases", command.getAliases()); + } + + String s = + "# The following can be changed to modify how the disguise commands are registered\n# This will only work on server startup\nModifyCommands: " + + modifyCommands + "\n\n" + config.saveToString(); + + commandConfig.delete(); + + try { + commandConfig.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + + try (PrintWriter writer = new PrintWriter(commandConfig, "UTF-8")) { + writer.write(s); + } catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + + public void load() { + loadPlugin(); + loadConfig(); + saveConfig(); + + if (!modifyCommands) { + return; + } + + registerCommands(); + } + + private void registerCommands() { + PluginDescriptionFile desc = LibsDisguises.getInstance().getDescription(); + Map> newMap = new HashMap<>(); + + for (DisguiseCommand command : commands.values()) { + if (!command.isEnabled()) { + continue; + } + + Map map = new HashMap<>(); + newMap.put(command.getName(), map); + + if (!command.getAliases().isEmpty()) { + map.put("aliases", command.getAliases()); + } + + if (!Strings.isNullOrEmpty(command.getPermission())) { + map.put("permission", command.getPermission()); + } + + if (!Strings.isNullOrEmpty(command.getDescription())) { + map.put("description", command.getDescription()); + } + } + + try { + Field f = PluginDescriptionFile.class.getDeclaredField("commands"); + f.setAccessible(true); + f.set(desc, newMap); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public String getCommand(String name) { + if (!modifyCommands) { + return name; + } + + DisguiseCommand command = commands.get(name); + + if (command == null || !command.isEnabled()) { + return null; + } + + return command.getName(); + } + + private void loadPlugin() { + PluginDescriptionFile desc = LibsDisguises.getInstance().getDescription(); + + for (Map.Entry> entry : desc.getCommands().entrySet()) { + DisguiseCommand command = new DisguiseCommand(); + command.setName(entry.getKey()); + + Map map = entry.getValue(); + + command.setPermission((String) map.get("permission")); + + if (map.containsKey("aliases")) { + command.setAliases(new ArrayList<>((Collection) map.get("aliases"))); + } + + command.setDescription((String) map.getOrDefault("description", "No description set")); + command.setEnabled(true); + + commands.put(entry.getKey(), command); + } + } +}