diff --git a/src/main/java/io/github/itzispyder/ogredupealias/OgreDupeAlias.java b/src/main/java/io/github/itzispyder/ogredupealias/OgreDupeAlias.java index 7ef6a75..01fb3f5 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/OgreDupeAlias.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/OgreDupeAlias.java @@ -1,6 +1,7 @@ package io.github.itzispyder.ogredupealias; import io.github.itzispyder.ogredupealias.commands.commands.ConfigCommand; +import io.github.itzispyder.ogredupealias.data.Config; import org.bukkit.Bukkit; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; @@ -13,7 +14,6 @@ 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 @@ -41,4 +41,8 @@ public final class OgreDupeAlias extends JavaPlugin { this.getConfig().options().copyDefaults(); this.saveDefaultConfig(); } + + public static String prefix() { + return Config.Plugin.prefix(); + } } 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 index 3b7e217..39ff77a 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/commands/commands/ConfigCommand.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/commands/commands/ConfigCommand.java @@ -1,17 +1,21 @@ 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 io.github.itzispyder.ogredupealias.utils.Text; +import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; +import org.bukkit.configuration.file.FileConfiguration; -import java.util.Arrays; +import java.util.ArrayList; import java.util.List; +import static io.github.itzispyder.ogredupealias.OgreDupeAlias.prefix; + public class ConfigCommand implements TabExecutor { @Override @@ -22,13 +26,17 @@ public class ConfigCommand implements TabExecutor { 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)); + List bd = new ArrayList<>(); + for (int i = 3; i < args.length; i ++) bd.add(args[i]); + String value = Text.color(String.join(" ",bd)); + + FileConfiguration config = Config.get(); + config.set(path,ConfigDataType.parse(value,type)); + Config.save(config); + sender.sendMessage(Text.builder("&3Config path '&b" + path + "&3' has updated: \n&3Value: &7" + ConfigDataType.parse(value,type)).color().prefix().build()); } case "get" -> { - sender.sendMessage("'" + path + "' has the following data: \n" + ConfigDataType.parseConfig(path,type)); + sender.sendMessage(Text.builder("&3Config path '&b" + path + "&3' has the following data: \n&3Value: &7" + ConfigDataType.parseConfig(path,type)).color().prefix().build()); } } } @@ -41,16 +49,44 @@ public class ConfigCommand implements TabExecutor { @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(); + List list = new ArrayList<>(); + + switch (args.length) { + case 1 -> { + list.add("get"); + list.add("set"); + } + case 2 -> { + list.addAll(Config.getSections()); + } + case 3 -> { + list.addAll(ArrayUtils.toNewList(ConfigDataType.values(), type -> type.name().toLowerCase())); + } + case 4 -> { + switch (args[0]) { + case "set" -> { + switch (args[2]) { + case "location" -> { + if (!args[3].contains(",")) list.addAll(ArrayUtils.toNewList(Bukkit.getWorlds(),w -> w.getName() + ",")); + else if (args[3].charAt(args[3].length() - 1) != ',' && args[3].split(",").length <= 3) { + list.add(args[3] + ","); + } + } + case "byte[]", "string[]" -> { + if (args[3].length() >= 1 && args[3].charAt(args[3].length() - 1) != ',') { + list.add(args[3] + ","); + } + } + default -> { + list.add(""); + } + } + } + } + } + } + + list.removeIf(s -> !s.toLowerCase().contains(args[args.length - 1].toLowerCase())); + return list; } } diff --git a/src/main/java/io/github/itzispyder/ogredupealias/data/Config.java b/src/main/java/io/github/itzispyder/ogredupealias/data/Config.java index c78fc93..318cdeb 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/data/Config.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/data/Config.java @@ -1,5 +1,6 @@ package io.github.itzispyder.ogredupealias.data; +import io.github.itzispyder.ogredupealias.utils.Text; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -17,14 +18,15 @@ 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 final String path = "config."; public static FileConfiguration get() { return YamlConfiguration.loadConfiguration(CONFIG_FILE); } - public static void save() { + public static void save(FileConfiguration config) { try { - get().save(CONFIG_FILE); + config.save(CONFIG_FILE); } catch (Exception ex) { log.warning("unable to save plugin config!"); @@ -37,4 +39,11 @@ public abstract class Config { List sections = new ArrayList<>(keys); return sections; } + + public static class Plugin { + private static final String path = "config.plugin."; + public static String prefix() { + return get().getString(path + "prefix") != null ? get().getString(path + "prefix") : Text.color("&7[&5Ogre&3Dupe&7] &r"); + } + } } diff --git a/src/main/java/io/github/itzispyder/ogredupealias/data/ConfigDataType.java b/src/main/java/io/github/itzispyder/ogredupealias/data/ConfigDataType.java index cf1ec05..ec66171 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/data/ConfigDataType.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/data/ConfigDataType.java @@ -1,6 +1,8 @@ package io.github.itzispyder.ogredupealias.data; +import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.World; import java.util.ArrayList; import java.util.HashSet; @@ -20,8 +22,9 @@ public class ConfigDataType { 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); + public static final ConfigDataType STRING_LIST = register(String[].class); + public static final ConfigDataType BYTE_LIST = register(Byte[].class); + public static final ConfigDataType NULL = register(Null.class); private static ConfigDataType register(Class type) { ConfigDataType dataType = new ConfigDataType<>(type); @@ -72,15 +75,26 @@ public class ConfigDataType { case "SHORT" -> returnable = Short.parseShort(value); case "LONG" -> returnable = Long.parseLong(value); case "BYTE" -> returnable = Byte.parseByte(value); - case "STRING_ARRAY" -> { + case "STRING[]" -> { List list = new ArrayList<>(); for (String s : value.split(",")) list.add(s); - returnable = list.toArray(new String[0]); + returnable = list; } - case "BYTE_ARRAY" -> { + case "BYTE[]" -> { List list = new ArrayList<>(); for (String s : value.split(",")) list.add(Byte.parseByte(s)); - returnable = list.toArray(new Byte[0]); + returnable = list; + } + case "LOCATION" -> { + String[] parts = value.split(","); + World world = Bukkit.getWorld(parts[0]); + double x = Double.parseDouble(parts[1]); + double y = Double.parseDouble(parts[2]); + double z = Double.parseDouble(parts[3]); + returnable = new Location(world,x,y,z); + } + case "NULL" -> { + returnable = null; } default -> returnable = new Object(); } diff --git a/src/main/java/io/github/itzispyder/ogredupealias/data/Null.java b/src/main/java/io/github/itzispyder/ogredupealias/data/Null.java new file mode 100644 index 0000000..134ef93 --- /dev/null +++ b/src/main/java/io/github/itzispyder/ogredupealias/data/Null.java @@ -0,0 +1,6 @@ +package io.github.itzispyder.ogredupealias.data; + +public class Null { + + public static final Null NULL = null; +} diff --git a/src/main/java/io/github/itzispyder/ogredupealias/utils/Text.java b/src/main/java/io/github/itzispyder/ogredupealias/utils/Text.java index ad9d7ad..d039ef0 100644 --- a/src/main/java/io/github/itzispyder/ogredupealias/utils/Text.java +++ b/src/main/java/io/github/itzispyder/ogredupealias/utils/Text.java @@ -32,7 +32,7 @@ public class Text { } public static String prefixed(String s) { - return prefix + s; + return prefix() + s; } public static TextBuilder builder(String s) {