config command
This commit is contained in:
@@ -1,19 +1,44 @@
|
|||||||
package io.github.itzispyder.ogredupealias;
|
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.plugin.java.JavaPlugin;
|
||||||
|
import org.bukkit.scheduler.BukkitScheduler;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public final class OgreDupeAlias extends JavaPlugin {
|
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 final String prefix = "";
|
||||||
|
public static OgreDupeAlias instance;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
// Plugin startup logic
|
// Plugin startup logic
|
||||||
|
instance = this;
|
||||||
|
this.init();
|
||||||
|
this.initConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
// Plugin shutdown logic
|
// 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<String> getSections() {
|
||||||
|
ConfigurationSection mainSection = get().getConfigurationSection("");
|
||||||
|
Set<String> keys = mainSection != null ? mainSection.getKeys(true) : new HashSet<>();
|
||||||
|
List<String> sections = new ArrayList<>(keys);
|
||||||
|
return sections;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<T> {
|
||||||
|
|
||||||
|
private static final Set<ConfigDataType<?>> values = new HashSet<>();
|
||||||
|
|
||||||
|
public static final ConfigDataType<Boolean> BOOLEAN = register(Boolean.class);
|
||||||
|
public static final ConfigDataType<String> STRING = register(String.class);
|
||||||
|
public static final ConfigDataType<Integer> INTEGER = register(Integer.class);
|
||||||
|
public static final ConfigDataType<Double> DOUBLE = register(Double.class);
|
||||||
|
public static final ConfigDataType<Float> FLOAT = register(Float.class);
|
||||||
|
public static final ConfigDataType<Long> LONG = register(Long.class);
|
||||||
|
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);
|
||||||
|
|
||||||
|
private static <T> ConfigDataType<T> register(Class<T> type) {
|
||||||
|
ConfigDataType<T> dataType = new ConfigDataType<>(type);
|
||||||
|
values.add(dataType);
|
||||||
|
return dataType;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Class<T> klass;
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public ConfigDataType(Class<T> klass) {
|
||||||
|
this.klass = klass;
|
||||||
|
this.name = klass.getSimpleName().toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Set<ConfigDataType<?>> values() {
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConfigDataType<?> valueOf(String name) {
|
||||||
|
for (ConfigDataType<?> type : values()) {
|
||||||
|
if (name.equalsIgnoreCase(type.name())) return type;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<T> getClassType() {
|
||||||
|
return klass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String name() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T parseConfig(String path, ConfigDataType<T> type) {
|
||||||
|
return Config.get().getObject(path,type.getClassType());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T parse(String value, ConfigDataType<T> 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<String> list = new ArrayList<>();
|
||||||
|
for (String s : value.split(",")) list.add(s);
|
||||||
|
returnable = list.toArray(new String[0]);
|
||||||
|
}
|
||||||
|
case "BYTE_ARRAY" -> {
|
||||||
|
List<Byte> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
0
src/main/resources/config.yml
Normal file
0
src/main/resources/config.yml
Normal file
@@ -6,3 +6,7 @@ prefix: ODA
|
|||||||
authors: [ ImproperIssues, TheTrouper ]
|
authors: [ ImproperIssues, TheTrouper ]
|
||||||
description: Server utilities for OgreDupe.minehut.gg
|
description: Server utilities for OgreDupe.minehut.gg
|
||||||
website: https://itzispyder.github.io/
|
website: https://itzispyder.github.io/
|
||||||
|
commands:
|
||||||
|
config:
|
||||||
|
description: Config management
|
||||||
|
usage: /config [get|set] <path> <datatype> <value>
|
||||||
Reference in New Issue
Block a user