diff --git a/src/main/java/io/github/itzispyder/ogredupealias/OgreDupeAlias.java b/src/main/java/io/github/itzispyder/ogredupealias/OgreDupeAlias.java index b78c1f3..7ef6a75 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/OgreDupeAlias.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/OgreDupeAlias.java @@ -1,19 +1,44 @@ package io.github.itzispyder.ogredupealias; +import io.github.itzispyder.ogredupealias.commands.commands.ConfigCommand; +import org.bukkit.Bukkit; +import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitScheduler; + +import java.util.logging.Logger; public final class OgreDupeAlias extends JavaPlugin { + public static final PluginManager pm = Bukkit.getPluginManager(); + public static final BukkitScheduler sch = Bukkit.getScheduler(); + public static final Logger log = Bukkit.getLogger(); public static final String prefix = ""; + public static OgreDupeAlias instance; @Override public void onEnable() { // Plugin startup logic - + instance = this; + this.init(); + this.initConfig(); } @Override public void onDisable() { // Plugin shutdown logic } + + public void init() { + // Events + + // Commands + getCommand("config").setExecutor(new ConfigCommand()); + getCommand("config").setTabCompleter(new ConfigCommand()); + } + + public void initConfig() { + this.getConfig().options().copyDefaults(); + this.saveDefaultConfig(); + } } diff --git a/src/main/java/io/github/itzispyder/ogredupealias/commands/commands/ConfigCommand.java b/src/main/java/io/github/itzispyder/ogredupealias/commands/commands/ConfigCommand.java new file mode 100644 index 0000000..3b7e217 --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/commands/commands/ConfigCommand.java @@ -0,0 +1,56 @@ +package io.github.itzispyder.ogredupealias.commands.commands; + +import io.github.itzispyder.ogredupealias.commands.CmdExHandler; +import io.github.itzispyder.ogredupealias.commands.TabComplBuilder; +import io.github.itzispyder.ogredupealias.data.Config; +import io.github.itzispyder.ogredupealias.data.ConfigDataType; +import io.github.itzispyder.ogredupealias.utils.ArrayUtils; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; + +import java.util.Arrays; +import java.util.List; + +public class ConfigCommand implements TabExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + try { + final String path = args[1]; + final ConfigDataType type = ConfigDataType.valueOf(args[2]); + + switch (args[0]) { + case "set" -> { + String value = args[3]; + Config.get().set(path,ConfigDataType.parse(value,type)); + Config.save(); + sender.sendMessage("'" + path + "' has updated: \n" + ConfigDataType.parse(value,type)); + } + case "get" -> { + sender.sendMessage("'" + path + "' has the following data: \n" + ConfigDataType.parseConfig(path,type)); + } + } + } + catch (Exception ex) { + CmdExHandler handler = new CmdExHandler(ex,command); + sender.sendMessage(handler.getHelp()); + } + return true; + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + return new TabComplBuilder(sender, command, alias, args) + .add(1, new String[] { + "set", + "get" + }) + .add(2, Config.getSections()) + .add(3, ArrayUtils.toNewList(ConfigDataType.values(), type -> type.name().toLowerCase())) + .add(4, new String[] { + "" + },args[0].equals("set")) + .build(); + } +} diff --git a/src/main/java/io/github/itzispyder/ogredupealias/data/Config.java b/src/main/java/io/github/itzispyder/ogredupealias/data/Config.java new file mode 100644 index 0000000..c78fc93 --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/data/Config.java @@ -0,0 +1,40 @@ +package io.github.itzispyder.ogredupealias.data; + +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static io.github.itzispyder.ogredupealias.OgreDupeAlias.instance; +import static io.github.itzispyder.ogredupealias.OgreDupeAlias.log; + +public abstract class Config { + + public static final File DATA_FOLDER = instance.getDataFolder(); + public static final File CONFIG_FILE = new File(DATA_FOLDER,"config.yml"); + + public static FileConfiguration get() { + return YamlConfiguration.loadConfiguration(CONFIG_FILE); + } + + public static void save() { + try { + get().save(CONFIG_FILE); + } + catch (Exception ex) { + log.warning("unable to save plugin config!"); + } + } + + public static List getSections() { + ConfigurationSection mainSection = get().getConfigurationSection(""); + Set keys = mainSection != null ? mainSection.getKeys(true) : new HashSet<>(); + List sections = new ArrayList<>(keys); + return sections; + } +} diff --git a/src/main/java/io/github/itzispyder/ogredupealias/data/ConfigDataType.java b/src/main/java/io/github/itzispyder/ogredupealias/data/ConfigDataType.java new file mode 100644 index 0000000..cf1ec05 --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/data/ConfigDataType.java @@ -0,0 +1,95 @@ +package io.github.itzispyder.ogredupealias.data; + +import org.bukkit.Location; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class ConfigDataType { + + private static final Set> values = new HashSet<>(); + + public static final ConfigDataType BOOLEAN = register(Boolean.class); + public static final ConfigDataType STRING = register(String.class); + public static final ConfigDataType INTEGER = register(Integer.class); + public static final ConfigDataType DOUBLE = register(Double.class); + public static final ConfigDataType FLOAT = register(Float.class); + public static final ConfigDataType LONG = register(Long.class); + public static final ConfigDataType SHORT = register(Short.class); + public static final ConfigDataType BYTE = register(Byte.class); + public static final ConfigDataType LOCATION = register(Location.class); + public static final ConfigDataType STRING_ARRAY = register(String[].class); + public static final ConfigDataType BYTE_ARRAY = register(Byte[].class); + + private static ConfigDataType register(Class type) { + ConfigDataType dataType = new ConfigDataType<>(type); + values.add(dataType); + return dataType; + } + + private final Class klass; + private final String name; + + public ConfigDataType(Class klass) { + this.klass = klass; + this.name = klass.getSimpleName().toUpperCase(); + } + + public static Set> values() { + return values; + } + + public static ConfigDataType valueOf(String name) { + for (ConfigDataType type : values()) { + if (name.equalsIgnoreCase(type.name())) return type; + } + return null; + } + + public Class getClassType() { + return klass; + } + + public String name() { + return name; + } + + public static T parseConfig(String path, ConfigDataType type) { + return Config.get().getObject(path,type.getClassType()); + } + + public static T parse(String value, ConfigDataType type) { + Object returnable; + + switch (type.name()) { + case "BOOLEAN" -> returnable = Boolean.parseBoolean(value); + case "STRING" -> returnable = value; + case "INTEGER" -> returnable = Integer.parseInt(value); + case "FLOAT" -> returnable = Float.parseFloat(value); + case "DOUBLE" -> returnable = Double.parseDouble(value); + case "SHORT" -> returnable = Short.parseShort(value); + case "LONG" -> returnable = Long.parseLong(value); + case "BYTE" -> returnable = Byte.parseByte(value); + case "STRING_ARRAY" -> { + List list = new ArrayList<>(); + for (String s : value.split(",")) list.add(s); + returnable = list.toArray(new String[0]); + } + case "BYTE_ARRAY" -> { + List list = new ArrayList<>(); + for (String s : value.split(",")) list.add(Byte.parseByte(s)); + returnable = list.toArray(new Byte[0]); + } + default -> returnable = new Object(); + } + + try { + return (T) returnable; + } + catch (Exception ex) { + return null; + } + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 92f10e1..6407aa3 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -6,3 +6,7 @@ prefix: ODA authors: [ ImproperIssues, TheTrouper ] description: Server utilities for OgreDupe.minehut.gg website: https://itzispyder.github.io/ +commands: + config: + description: Config management + usage: /config [get|set] \ No newline at end of file