Added a way to modify the config using a command

This commit is contained in:
Paldiu 2022-05-10 12:44:47 -05:00
parent 086a310479
commit f1989e1bbe
4 changed files with 130 additions and 2 deletions

View File

@ -1,6 +1,8 @@
package io.github.simplex.toolassist; package io.github.simplex.toolassist;
import io.github.simplex.toolassist.data.Config; 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.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -13,8 +15,10 @@ public final class ToolAssist extends JavaPlugin {
// Plugin startup logic // Plugin startup logic
getLogger().info("Initializing configuration..."); getLogger().info("Initializing configuration...");
this.config = new Config(this); this.config = new Config(this);
getLogger().info("Initialization complete! Registering listener..."); getLogger().info("Configuration loaded! Registering listener...");
new MineListener(this); new MineListener(this);
getLogger().info("Listener registered successfully! Loading command...");
new Command_toolassist(this);
getLogger().info("Initialization complete!"); getLogger().info("Initialization complete!");
} }

View File

@ -8,6 +8,8 @@ import org.bukkit.configuration.file.YamlConfiguration;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -85,8 +87,10 @@ public class Config extends YamlConfiguration {
public static class Settings { public static class Settings {
private final ConfigurationSection plugin_settings; private final ConfigurationSection plugin_settings;
private final ConfigurationSection tool_settings; private final ConfigurationSection tool_settings;
private final Config config;
public Settings(Config config) { public Settings(Config config) {
this.config = config;
this.plugin_settings = config.getConfigurationSection("plugin_settings"); this.plugin_settings = config.getConfigurationSection("plugin_settings");
this.tool_settings = config.getConfigurationSection("tool_settings"); this.tool_settings = config.getConfigurationSection("tool_settings");
} }
@ -148,5 +152,28 @@ public class Config extends YamlConfiguration {
.map(Material::matchMaterial) .map(Material::matchMaterial)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
public final void modifyToolEntry(String name, String value, boolean addOrRemove) {
List<String> 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<String> 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();
}
} }
} }

View File

@ -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 <plugin | (tool <add | remove>)> <setting> <value>");
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;
}
}

View File

@ -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.BlockIdentifier;
import io.github.simplex.toolassist.data.Config; import io.github.simplex.toolassist.data.Config;
import org.bukkit.block.Block; import org.bukkit.block.Block;