From f1989e1bbe08f678546495def6ce6bdc97a7989f Mon Sep 17 00:00:00 2001 From: Paldiu Date: Tue, 10 May 2022 12:44:47 -0500 Subject: [PATCH] Added a way to modify the config using a command --- .../github/simplex/toolassist/ToolAssist.java | 6 +- .../simplex/toolassist/data/Config.java | 27 ++++++ .../toolassist/play/Command_toolassist.java | 96 +++++++++++++++++++ .../toolassist/{ => play}/MineListener.java | 3 +- 4 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/github/simplex/toolassist/play/Command_toolassist.java rename src/main/java/io/github/simplex/toolassist/{ => play}/MineListener.java (93%) diff --git a/src/main/java/io/github/simplex/toolassist/ToolAssist.java b/src/main/java/io/github/simplex/toolassist/ToolAssist.java index fff3f4a..bf9d37f 100644 --- a/src/main/java/io/github/simplex/toolassist/ToolAssist.java +++ b/src/main/java/io/github/simplex/toolassist/ToolAssist.java @@ -1,6 +1,8 @@ package io.github.simplex.toolassist; import io.github.simplex.toolassist.data.Config; +import io.github.simplex.toolassist.play.Command_toolassist; +import io.github.simplex.toolassist.play.MineListener; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; @@ -13,8 +15,10 @@ public final class ToolAssist extends JavaPlugin { // Plugin startup logic getLogger().info("Initializing configuration..."); this.config = new Config(this); - getLogger().info("Initialization complete! Registering listener..."); + getLogger().info("Configuration loaded! Registering listener..."); new MineListener(this); + getLogger().info("Listener registered successfully! Loading command..."); + new Command_toolassist(this); getLogger().info("Initialization complete!"); } diff --git a/src/main/java/io/github/simplex/toolassist/data/Config.java b/src/main/java/io/github/simplex/toolassist/data/Config.java index 79c70f0..6b15102 100644 --- a/src/main/java/io/github/simplex/toolassist/data/Config.java +++ b/src/main/java/io/github/simplex/toolassist/data/Config.java @@ -8,6 +8,8 @@ import org.bukkit.configuration.file.YamlConfiguration; import java.io.*; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -85,8 +87,10 @@ public class Config extends YamlConfiguration { public static class Settings { private final ConfigurationSection plugin_settings; private final ConfigurationSection tool_settings; + private final Config config; public Settings(Config config) { + this.config = config; this.plugin_settings = config.getConfigurationSection("plugin_settings"); this.tool_settings = config.getConfigurationSection("tool_settings"); } @@ -148,5 +152,28 @@ public class Config extends YamlConfiguration { .map(Material::matchMaterial) .collect(Collectors.toSet()); } + + public final void modifyToolEntry(String name, String value, boolean addOrRemove) { + List materialList = tool_settings.getStringList(name); + + // This is to use a tertiary statement instead of an if-else. + // The respective method will be called and then the result will be set to the ignored boolean, which can be safely ignored. + boolean ignored = addOrRemove ? materialList.add(value) : materialList.remove(value); + + tool_settings.set(name, materialList); + config.osave(); + } + + public final void removeToolEntry(String name, String value) { + List materialList = tool_settings.getStringList(name); + materialList.remove(value); + tool_settings.set(name, materialList); + config.osave(); + } + + public final void setPluginEntry(String name, Object value) { + plugin_settings.set(name, value); + config.osave(); + } } } diff --git a/src/main/java/io/github/simplex/toolassist/play/Command_toolassist.java b/src/main/java/io/github/simplex/toolassist/play/Command_toolassist.java new file mode 100644 index 0000000..234e524 --- /dev/null +++ b/src/main/java/io/github/simplex/toolassist/play/Command_toolassist.java @@ -0,0 +1,96 @@ +package io.github.simplex.toolassist.play; + +import io.github.simplex.toolassist.ToolAssist; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextColor; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginIdentifiableCommand; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class Command_toolassist extends Command implements PluginIdentifiableCommand { + private final ToolAssist plugin; + + public Command_toolassist(ToolAssist plugin) { + super("toolassist"); + super.setLabel("toolassist"); + super.setPermission("toolassist.admin"); + super.setDescription("A way to modify the configuration in game."); + super.setUsage("/toolassist )> "); + super.setAliases(List.of("ta", "miner", "assist", "vm")); + super.permissionMessage(Component.empty().content("You must have the toolassist.admin permission to use this command.") + .color(TextColor.color(ChatColor.RED.asBungee().getColor().getRGB()))); + super.register(plugin.getServer().getCommandMap()); + this.plugin = plugin; + } + + @Override + public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) { + if (args[0].equalsIgnoreCase("plugin")) { + if (args.length != 3) return false; + + switch (args[1]) { + case "radius" -> { + plugin.getConfig().getSettings().setPluginEntry("search_radius", args[2]); + return true; + } + case "sneak" -> { + plugin.getConfig().getSettings().setPluginEntry("sneak_activation", args[2]); + return true; + } + case "use_config" -> { + plugin.getConfig().getSettings().setPluginEntry("no_config", args[2]); + return true; + } + case "permission" -> { + plugin.getConfig().getSettings().setPluginEntry("permission", args[2]); + return true; + } + default -> { + return false; + } + } + } + + if (args[0].equalsIgnoreCase("tool")) { + if (args.length != 4) return false; + + if (args[1].equalsIgnoreCase("add")) { + switch (args[2]) { + case "pickaxe", "axe", "sword", "shears", "shovel", "hoe" -> { + plugin.getConfig().getSettings().modifyToolEntry(args[2], args[3], true); + return true; + } + default -> { + return false; + } + } + } + + if (args[1].equalsIgnoreCase("remove")) { + switch (args[2]) { + case "pickaxe", "axe", "sword", "shears", "shovel", "hoe" -> { + plugin.getConfig().getSettings().modifyToolEntry(args[2], args[3], false); + return true; + } + default -> { + return false; + } + } + } + + return false; + } + + return false; + } + + @Override + public @NotNull ToolAssist getPlugin() { + return plugin; + } +} diff --git a/src/main/java/io/github/simplex/toolassist/MineListener.java b/src/main/java/io/github/simplex/toolassist/play/MineListener.java similarity index 93% rename from src/main/java/io/github/simplex/toolassist/MineListener.java rename to src/main/java/io/github/simplex/toolassist/play/MineListener.java index 6714068..d1cb528 100644 --- a/src/main/java/io/github/simplex/toolassist/MineListener.java +++ b/src/main/java/io/github/simplex/toolassist/play/MineListener.java @@ -1,5 +1,6 @@ -package io.github.simplex.toolassist; +package io.github.simplex.toolassist.play; +import io.github.simplex.toolassist.ToolAssist; import io.github.simplex.toolassist.data.BlockIdentifier; import io.github.simplex.toolassist.data.Config; import org.bukkit.block.Block;