config command
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String> 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<String> 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[] {
|
||||
"<value>"
|
||||
},args[0].equals("set"))
|
||||
.build();
|
||||
List<String> 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("<value>");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
list.removeIf(s -> !s.toLowerCase().contains(args[args.length - 1].toLowerCase()));
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T> {
|
||||
public static final ConfigDataType<Short> SHORT = register(Short.class);
|
||||
public static final ConfigDataType<Byte> BYTE = register(Byte.class);
|
||||
public static final ConfigDataType<Location> LOCATION = register(Location.class);
|
||||
public static final ConfigDataType<String[]> STRING_ARRAY = register(String[].class);
|
||||
public static final ConfigDataType<Byte[]> BYTE_ARRAY = register(Byte[].class);
|
||||
public static final ConfigDataType<String[]> STRING_LIST = register(String[].class);
|
||||
public static final ConfigDataType<Byte[]> BYTE_LIST = register(Byte[].class);
|
||||
public static final ConfigDataType<Null> NULL = register(Null.class);
|
||||
|
||||
private static <T> ConfigDataType<T> register(Class<T> type) {
|
||||
ConfigDataType<T> dataType = new ConfigDataType<>(type);
|
||||
@@ -72,15 +75,26 @@ public class ConfigDataType<T> {
|
||||
case "SHORT" -> returnable = Short.parseShort(value);
|
||||
case "LONG" -> returnable = Long.parseLong(value);
|
||||
case "BYTE" -> returnable = Byte.parseByte(value);
|
||||
case "STRING_ARRAY" -> {
|
||||
case "STRING[]" -> {
|
||||
List<String> 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<Byte> 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();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package io.github.itzispyder.ogredupealias.data;
|
||||
|
||||
public class Null {
|
||||
|
||||
public static final Null NULL = null;
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user