Introduce ability to customize command names, add aliases, even disable them individially

This commit is contained in:
libraryaddict 2021-01-28 21:51:46 +13:00
parent 6c2cd6ac7a
commit 87e3defcab
2 changed files with 210 additions and 2 deletions

View File

@ -18,6 +18,7 @@ import me.libraryaddict.disguise.commands.undisguise.UndisguiseRadiusCommand;
import me.libraryaddict.disguise.commands.utils.*; import me.libraryaddict.disguise.commands.utils.*;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsPremium; 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.DisguiseListener;
import me.libraryaddict.disguise.utilities.listeners.PaperDisguiseListener; import me.libraryaddict.disguise.utilities.listeners.PaperDisguiseListener;
import me.libraryaddict.disguise.utilities.listeners.PlayerSkinHandler; import me.libraryaddict.disguise.utilities.listeners.PlayerSkinHandler;
@ -54,6 +55,7 @@ public class LibsDisguises extends JavaPlugin {
private final UpdateChecker updateChecker = new UpdateChecker(); private final UpdateChecker updateChecker = new UpdateChecker();
@Getter @Getter
private PlayerSkinHandler skinHandler; private PlayerSkinHandler skinHandler;
private DisguiseCommandConfig commandConfig;
@Override @Override
public void onLoad() { 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"); "plugin will continue to load, but it will look like a mugging victim");
} }
commandConfig = new DisguiseCommandConfig();
if (!reloaded) {
commandConfig.load();
}
WatcherSanitizer.init(); WatcherSanitizer.init();
} }
@ -239,7 +247,7 @@ public class LibsDisguises extends JavaPlugin {
for (String command : getDescription().getCommands().keySet()) { for (String command : getDescription().getCommands().keySet()) {
PluginCommand cmd = getCommand("libsdisguises:" + command); PluginCommand cmd = getCommand("libsdisguises:" + command);
if (cmd.getExecutor() != this && !force) { if (cmd == null || (cmd.getExecutor() != this && !force)) {
continue; continue;
} }
@ -292,7 +300,17 @@ public class LibsDisguises extends JavaPlugin {
} }
private void registerCommand(String commandName, CommandExecutor executioner) { 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); command.setExecutor(executioner);

View File

@ -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<String> aliases = new ArrayList<>();
private boolean enabled;
}
private File commandConfig = new File(LibsDisguises.getInstance().getDataFolder(), "commands.yml");
private HashMap<String, DisguiseCommand> 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<String, DisguiseCommand> 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<String, Map<String, Object>> newMap = new HashMap<>();
for (DisguiseCommand command : commands.values()) {
if (!command.isEnabled()) {
continue;
}
Map<String, Object> 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<String, Map<String, Object>> entry : desc.getCommands().entrySet()) {
DisguiseCommand command = new DisguiseCommand();
command.setName(entry.getKey());
Map<String, Object> 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);
}
}
}