From 805f7654344ddc11fcde401c786496d2c9778733 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Tue, 10 May 2022 23:46:37 -0500 Subject: [PATCH] Alpha 1.0 RC02 - Adjusted things for QoL purposes. - Added TabCompletion to the tool assist command. --- .../github/simplex/toolassist/ToolAssist.java | 15 ++++-- .../toolassist/data/BlockIdentifier.java | 20 +++++++- .../toolassist/play/Command_toolassist.java | 48 +++++++++++++++++-- .../simplex/toolassist/play/MineListener.java | 8 ++-- 4 files changed, 78 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/github/simplex/toolassist/ToolAssist.java b/src/main/java/io/github/simplex/toolassist/ToolAssist.java index bf9d37f..2e446d5 100644 --- a/src/main/java/io/github/simplex/toolassist/ToolAssist.java +++ b/src/main/java/io/github/simplex/toolassist/ToolAssist.java @@ -9,24 +9,29 @@ import org.jetbrains.annotations.NotNull; public final class ToolAssist extends JavaPlugin { private Config config; + private Command_toolassist command; @Override public void onEnable() { // Plugin startup logic getLogger().info("Initializing configuration..."); - this.config = new Config(this); + config = new Config(this); getLogger().info("Configuration loaded! Registering listener..."); new MineListener(this); getLogger().info("Listener registered successfully! Loading command..."); - new Command_toolassist(this); + command = new Command_toolassist(this); getLogger().info("Initialization complete!"); } @Override public void onDisable() { - this.config.osave(); - this.config = null; - getLogger().info("Goodbye!"); + getLogger().info("Saving configuration..."); + config.osave(); + config = null; + getLogger().info("Configuration saved successfully. Unregistering the command..."); + command.unregister(); + command = null; + getLogger().info("Termination complete. Goodbye!"); } @Override diff --git a/src/main/java/io/github/simplex/toolassist/data/BlockIdentifier.java b/src/main/java/io/github/simplex/toolassist/data/BlockIdentifier.java index 494b663..f2db5da 100644 --- a/src/main/java/io/github/simplex/toolassist/data/BlockIdentifier.java +++ b/src/main/java/io/github/simplex/toolassist/data/BlockIdentifier.java @@ -2,6 +2,8 @@ package io.github.simplex.toolassist.data; import io.github.simplex.toolassist.ToolAssist; import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.Tag; import org.bukkit.block.Block; import org.bukkit.inventory.ItemStack; @@ -35,7 +37,7 @@ public class BlockIdentifier { public boolean checkBlock(Block block, ItemStack targetItem) { if (plugin.getConfig().getSettings().noConfig()) { - return block.isValidTool(targetItem); + return block.isValidTool(targetItem) && all.stream().anyMatch(a -> a.isTagged(block.getType())); } checkBlockConfig(block, targetItem); @@ -71,4 +73,20 @@ public class BlockIdentifier { } isValid = false; } + + public List> all = List.of(Tag.COAL_ORES, + Tag.COPPER_ORES, + Tag.GOLD_ORES, + Tag.IRON_ORES, + Tag.DIAMOND_ORES, + Tag.LAPIS_ORES, + Tag.EMERALD_ORES, + Tag.REDSTONE_ORES, + Tag.SAND, + Tag.CAVE_VINES, + Tag.WART_BLOCKS, + Tag.LOGS, + Tag.LOGS_THAT_BURN, + Tag.LEAVES, + Tag.WARPED_STEMS); } 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 index 234e524..1dec400 100644 --- a/src/main/java/io/github/simplex/toolassist/play/Command_toolassist.java +++ b/src/main/java/io/github/simplex/toolassist/play/Command_toolassist.java @@ -7,12 +7,16 @@ 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.bukkit.command.TabCompleter; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.stream.Stream; -public class Command_toolassist extends Command implements PluginIdentifiableCommand { +public class Command_toolassist extends Command implements PluginIdentifiableCommand, TabCompleter { private final ToolAssist plugin; public Command_toolassist(ToolAssist plugin) { @@ -56,7 +60,7 @@ public class Command_toolassist extends Command implements PluginIdentifiableCom } } - if (args[0].equalsIgnoreCase("tool")) { + if (args[0].equalsIgnoreCase("tools")) { if (args.length != 4) return false; if (args[1].equalsIgnoreCase("add")) { @@ -93,4 +97,42 @@ public class Command_toolassist extends Command implements PluginIdentifiableCom public @NotNull ToolAssist getPlugin() { return plugin; } + + @Override + public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { + List completions = new ArrayList<>(); + + switch (args.length) { + case 1 -> { + completions = List.of("tools", "plugin"); + } + case 2 -> { + if (args[0].equalsIgnoreCase("tools")) { + completions = List.of("add", "remove"); + } + if (args[0].equalsIgnoreCase("plugin")) { + completions = List.of("permission", "radius", "sneak", "use_config"); + } + } + case 3 -> { + if (args[0].equalsIgnoreCase("tools")) { + if (args[1].equalsIgnoreCase("add") || args[1].equalsIgnoreCase("remove")) { + completions = Stream.of("pickaxe", "axe", "sword", "shears", "shovel", "hoe").sorted().toList(); + } + } + + if (args[0].equalsIgnoreCase("plugin")) { + if (args[1].equalsIgnoreCase("use_config") || args[1].equalsIgnoreCase("sneak")) { + completions = List.of("true", "false"); + } + } + } + } + + return completions.stream().filter(p -> p.startsWith(args[args.length - 1])).toList(); + } + + public void unregister() { + unregister(plugin.getServer().getCommandMap()); + } } diff --git a/src/main/java/io/github/simplex/toolassist/play/MineListener.java b/src/main/java/io/github/simplex/toolassist/play/MineListener.java index d1cb528..6a0cda1 100644 --- a/src/main/java/io/github/simplex/toolassist/play/MineListener.java +++ b/src/main/java/io/github/simplex/toolassist/play/MineListener.java @@ -25,15 +25,15 @@ public class MineListener implements Listener { @EventHandler public void activate(BlockBreakEvent event) { Player player = event.getPlayer(); - ItemStack stack = player.getInventory().getItemInMainHand(); - Block block = event.getBlock(); String permission = settings.permission(); - List blocks = identifier.populateAndRetrieve(block, stack); if (!player.hasPermission(permission)) return; - if (!player.isSneaking() && settings.useSneak()) return; + ItemStack stack = player.getInventory().getItemInMainHand(); + Block block = event.getBlock(); + List blocks = identifier.populateAndRetrieve(block, stack); + blocks.forEach(Block::breakNaturally); } }