Introduce ability to customize command names, add aliases, even disable them individially
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user