3 Commits

Author SHA1 Message Date
805f765434 Alpha 1.0 RC02
- Adjusted things for QoL purposes.
- Added TabCompletion to the tool assist command.
2022-05-10 23:46:37 -05:00
f1989e1bbe Added a way to modify the config using a command 2022-05-10 12:44:47 -05:00
086a310479 Critical Patch
Fixed a critical issue where a class level block list was populated and never cleared.
2022-05-09 23:18:02 -05:00
9 changed files with 296 additions and 16 deletions

View File

@ -0,0 +1,82 @@
# The overall settings for the plugin itself
plugin_settings:
# The permission required to use the assist feature.
permission: "toolassist.activate"
# How many blocks outwards should be scanned
search_radius: 15
# Whether a user must be sneaking to activate
sneak_activation: true
# Whether to use the respective tool to trigger activation, or to use the groupings listed below.
no_config: false
# The block material that can be targeted by the respective tool grouping
tool_settings:
# Blocks which are affected by pickaxes
pickaxe:
- COAL_ORE
- COPPER_ORE
- IRON_ORE
- GOLD_ORE
- REDSTONE_ORE
- LAPIS_LAZULI_ORE
- EMERALD_ORE
- DIAMOND_ORE
- DEEPSLATE_COAL_ORE
- DEEPSLATE_IRON_ORE
- DEEPSLATE_GOLD_ORE
- DEEPSLATE_REDSTONE_ORE
- DEEPSLATE_LAPIS_ORE
- DEEPSLATE_EMERALD_ORE
- DEEPSLATE_DIAMOND_ORE
- DEEPSLATE_COPPER_ORE
- NETHER_QUARTZ_ORE
- NETHER_GOLD_ORE
# Blocks which are affected by axes
axe:
- OAK_LOG
- DARK_OAK_LOG
- BIRCH_LOG
- SPRUCE_LOG
- JUNGLE_LOG
- ACACIA_LOG
- STRIPPED_OAK_LOG
- STRIPPED_DARK_OAK_LOG
- STRIPPED_BIRCH_LOG
- STRIPPED_SPRUCE_LOG
- STRIPPED_JUNGLE_LOG
- STRIPPED_ACACIA_LOG
# Blocks which are affected by shovels
shovel:
- SAND
- GRAVEL
# Blocks which are affected by hoes
hoe:
- WARPED_WART_BLOCK
- NETHER_WART_BLOCK
- SPONGE
- WET_SPONGE
# Blocks which are affected by swords
sword:
- COBWEB
# Blocks which are affected by shears
shears:
- OAK_LEAVES
- DARK_OAK_LEAVES
- BIRCH_LEAVES
- JUNGLE_LEAVES
- ACACIA_LEAVES
- SPRUCE_LEAVES
- VINE
- CAVE_VINES_PLANT
- WEEPING_VINES_PLANT
- TWISTING_VINES_PLANT

View File

@ -0,0 +1,6 @@
name: ToolAssist
version: 'v1.0-Alpha'
main: io.github.simplex.toolassist.ToolAssist
api-version: 1.18
authors: [ SimplexDevelopment ]
description: A lightweight vein-mining plugin.

Binary file not shown.

View File

@ -0,0 +1,2 @@
Manifest-Version: 1.0

View File

@ -1,28 +1,37 @@
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;
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);
getLogger().info("Initialization complete! Registering listener...");
config = new Config(this);
getLogger().info("Configuration loaded! Registering listener...");
new MineListener(this);
getLogger().info("Listener registered successfully! Loading command...");
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

View File

@ -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;
@ -9,7 +11,6 @@ import java.util.ArrayList;
import java.util.List;
public class BlockIdentifier {
private final List<Block> blockList = new ArrayList<>();
private final ToolAssist plugin;
boolean isValid = false;
@ -18,9 +19,9 @@ public class BlockIdentifier {
}
public List<Block> populateAndRetrieve(Block block, ItemStack requiredItem) {
List<Block> surroundingBlocks = new ArrayList<>();
Location start = block.getLocation().clone();
int radius = plugin.getConfig().getSettings().radius();
List<Block> surroundingBlocks = new ArrayList<>();
for (double x = start.getX() - radius; x <= start.getX() + radius; x++) {
for (double y = start.getY() - radius; y <= start.getY() + radius; y++) {
for (double z = start.getZ() - radius; z <= start.getZ() + radius; z++) {
@ -31,14 +32,12 @@ public class BlockIdentifier {
}
}
}
blockList.addAll(surroundingBlocks);
return blockList;
return surroundingBlocks;
}
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);
@ -74,4 +73,20 @@ public class BlockIdentifier {
}
isValid = false;
}
public List<Tag<Material>> 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);
}

View File

@ -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<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,138 @@
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.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, TabCompleter {
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("tools")) {
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;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
List<String> 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());
}
}

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.Config;
import org.bukkit.block.Block;
@ -24,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<Block> 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<Block> blocks = identifier.populateAndRetrieve(block, stack);
blocks.forEach(Block::breakNaturally);
}
}