diff --git a/build.gradle b/build.gradle index 0dcc823..06cba06 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group = 'io.github.simplex' -version = '1.0.0' +version = 'v1.0-Alpha' repositories { mavenCentral() diff --git a/src/main/java/io/github/simplex/toolassist/MineListener.java b/src/main/java/io/github/simplex/toolassist/MineListener.java new file mode 100644 index 0000000..6714068 --- /dev/null +++ b/src/main/java/io/github/simplex/toolassist/MineListener.java @@ -0,0 +1,38 @@ +package io.github.simplex.toolassist; + +import io.github.simplex.toolassist.data.BlockIdentifier; +import io.github.simplex.toolassist.data.Config; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.List; + +public class MineListener implements Listener { + private final BlockIdentifier identifier; + private final Config.Settings settings; + + public MineListener(ToolAssist plugin) { + this.settings = plugin.getConfig().getSettings(); + this.identifier = new BlockIdentifier(plugin); + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @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; + + blocks.forEach(Block::breakNaturally); + } +} diff --git a/src/main/java/io/github/simplex/toolassist/ToolAssist.java b/src/main/java/io/github/simplex/toolassist/ToolAssist.java index b867e3e..fff3f4a 100644 --- a/src/main/java/io/github/simplex/toolassist/ToolAssist.java +++ b/src/main/java/io/github/simplex/toolassist/ToolAssist.java @@ -11,12 +11,18 @@ public final class ToolAssist extends JavaPlugin { @Override public void onEnable() { // Plugin startup logic + getLogger().info("Initializing configuration..."); this.config = new Config(this); + getLogger().info("Initialization complete! Registering listener..."); + new MineListener(this); + getLogger().info("Initialization complete!"); } @Override public void onDisable() { - // Plugin shutdown logic + this.config.osave(); + this.config = null; + getLogger().info("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 5d28e37..b60365f 100644 --- a/src/main/java/io/github/simplex/toolassist/data/BlockIdentifier.java +++ b/src/main/java/io/github/simplex/toolassist/data/BlockIdentifier.java @@ -2,46 +2,76 @@ package io.github.simplex.toolassist.data; import io.github.simplex.toolassist.ToolAssist; import org.bukkit.Location; -import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; import org.bukkit.inventory.ItemStack; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.atomic.AtomicBoolean; public class BlockIdentifier { private final List blockList = new ArrayList<>(); - private final List blockLocations = new ArrayList<>(); private final ToolAssist plugin; + boolean isValid = false; public BlockIdentifier(ToolAssist plugin) { this.plugin = plugin; } - public List populateAndRetrieve(Block block) { + public List populateAndRetrieve(Block block, ItemStack requiredItem) { Location start = block.getLocation().clone(); - + int radius = plugin.getConfig().getSettings().radius(); + List 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++) { + Location location = new Location(start.getWorld(), x, y, z); + if (checkBlock(location.getBlock(), requiredItem)) { + surroundingBlocks.add(location.getBlock()); + } + } + } + } + blockList.addAll(surroundingBlocks); return blockList; } public boolean checkBlock(Block block, ItemStack targetItem) { - AtomicBoolean isValid = new AtomicBoolean(false); - - if (plugin.getConfig().getSettings().useTags()) { - TagBox.oreTagList().forEach(tag -> { - if (tag.isTagged(block.getType())) isValid.set(true); - }); - return isValid.get(); + if (plugin.getConfig().getSettings().noConfig()) { + return block.isValidTool(targetItem); } - Material m = block.getType(); - Material item = targetItem.getType(); + checkBlockConfig(block, targetItem); + return isValid; + } - // TODO: Tag or Material checks for the item, then the respective block type. - - return isValid.get(); + private void checkBlockConfig(Block block, ItemStack targetItem) { + String itemName = targetItem.getType().name(); + Config.Settings settings = plugin.getConfig().getSettings(); + if (itemName.endsWith("pickaxe")) { + if (settings.pickaxeMaterials().contains(block.getType())) isValid = true; + return; + } + if (!itemName.endsWith("pickaxe") && itemName.endsWith("axe")) { + if (settings.axeMaterials().contains(block.getType())) isValid = true; + return; + } + if (itemName.endsWith("shears")) { + if (settings.shearMaterials().contains(block.getType())) isValid = true; + return; + } + if (itemName.endsWith("shovel")) { + if (settings.shovelMaterials().contains(block.getType())) isValid = true; + return; + } + if (itemName.endsWith("hoe")) { + if (settings.hoeMaterials().contains(block.getType())) isValid = true; + return; + } + if (itemName.endsWith("sword")) { + if (settings.swordMaterials().contains(block.getType())) isValid = true; + return; + } + isValid = false; } } 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 fb38344..79c70f0 100644 --- a/src/main/java/io/github/simplex/toolassist/data/Config.java +++ b/src/main/java/io/github/simplex/toolassist/data/Config.java @@ -8,11 +8,15 @@ import org.bukkit.configuration.file.YamlConfiguration; import java.io.*; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +@SuppressWarnings("ResultOfMethodCallIgnored") public class Config extends YamlConfiguration { private final Settings settings; + private final File cf; public Config(ToolAssist plugin) { this.settings = new Settings(this); @@ -23,7 +27,7 @@ public class Config extends YamlConfiguration { if (!dataFolder.exists()) dataFolder.mkdirs(); - File cf = new File(dataFolder, fileName); + cf = new File(dataFolder, fileName); InputStream stream = plugin.getResource(fileName); assert stream != null; @@ -34,7 +38,7 @@ public class Config extends YamlConfiguration { plugin.saveResource(fileName, true); } - oload(cf); + oload(); reader.lines().filter(s -> s.contains(":")) .map(s -> s.split(":")[0]) @@ -42,11 +46,12 @@ public class Config extends YamlConfiguration { .forEach(s -> { plugin.getLogger().severe("Configuration is missing an entry, attempting to replace..."); Optional stringStream = reader.lines().filter(c -> c.contains(s)).findFirst(); - assert stringStream.isPresent(); + if (stringStream.isEmpty()) + throw new RuntimeException("Unable to fix your configuration file. Please delete the config.yml in the data folder and restart your server."); String key = stringStream.get().split(":")[0].trim(); String value = stringStream.get().split(":")[1].trim(); super.addDefault(key, value); - osave(cf); + osave(); }); @@ -54,10 +59,10 @@ public class Config extends YamlConfiguration { plugin.getLogger().severe(ex.getMessage()); } - oload(cf); + oload(); } - public void osave(File cf) { + public void osave() { try { super.save(cf); } catch (IOException e) { @@ -65,7 +70,7 @@ public class Config extends YamlConfiguration { } } - public void oload(File cf) { + public void oload() { try { super.load(cf); } catch (IOException | InvalidConfigurationException e) { @@ -86,8 +91,12 @@ public class Config extends YamlConfiguration { this.tool_settings = config.getConfigurationSection("tool_settings"); } - public final boolean useTags() { - return plugin_settings.getBoolean("use_tags_no_config", false); + public final String permission() { + return plugin_settings.getString("permission", "toolassist.activate"); + } + + public final boolean noConfig() { + return plugin_settings.getBoolean("no_config", false); } public final boolean useSneak() { @@ -99,57 +108,45 @@ public class Config extends YamlConfiguration { } public final Set pickaxeMaterials() { - Set materials = new HashSet<>(); - tool_settings.getStringList("pickaxe") + return tool_settings.getStringList("pickaxe") .stream() .map(Material::matchMaterial) - .forEach(materials::add); - return materials; + .collect(Collectors.toSet()); } public final Set axeMaterials() { - Set materials = new HashSet<>(); - tool_settings.getStringList("axe") + return tool_settings.getStringList("axe") .stream() .map(Material::matchMaterial) - .forEach(materials::add); - return materials; + .collect(Collectors.toSet()); } public final Set shovelMaterials() { - Set materials = new HashSet<>(); - tool_settings.getStringList("shovel") + return tool_settings.getStringList("shovel") .stream() .map(Material::matchMaterial) - .forEach(materials::add); - return materials; + .collect(Collectors.toSet()); } public final Set hoeMaterials() { - Set materials = new HashSet<>(); - tool_settings.getStringList("hoe") + return tool_settings.getStringList("hoe") .stream() .map(Material::matchMaterial) - .forEach(materials::add); - return materials; + .collect(Collectors.toSet()); } public final Set swordMaterials() { - Set materials = new HashSet<>(); - tool_settings.getStringList("sword") + return tool_settings.getStringList("sword") .stream() .map(Material::matchMaterial) - .forEach(materials::add); - return materials; + .collect(Collectors.toSet()); } public final Set shearMaterials() { - Set materials = new HashSet<>(); - tool_settings.getStringList("shears") + return tool_settings.getStringList("shears") .stream() .map(Material::matchMaterial) - .forEach(materials::add); - return materials; + .collect(Collectors.toSet()); } } } diff --git a/src/main/java/io/github/simplex/toolassist/data/TagBox.java b/src/main/java/io/github/simplex/toolassist/data/TagBox.java deleted file mode 100644 index b18a2e1..0000000 --- a/src/main/java/io/github/simplex/toolassist/data/TagBox.java +++ /dev/null @@ -1,39 +0,0 @@ -package io.github.simplex.toolassist.data; - -import org.bukkit.Material; -import org.bukkit.Tag; - -import java.util.List; - -public class TagBox { - public static List> oreTagList() { - return List.of(Tag.COAL_ORES, - Tag.IRON_ORES, - Tag.COPPER_ORES, - Tag.GOLD_ORES, - Tag.DIAMOND_ORES, - Tag.REDSTONE_ORES, - Tag.EMERALD_ORES, - Tag.LAPIS_ORES); - } - - public static Tag leaves() { - return Tag.LEAVES; - } - - public static Tag sandType() { - return Tag.SAND; - } - - public static Tag wart() { - return Tag.WART_BLOCKS; - } - - public static Tag logs() { - return Tag.LOGS; - } - - public static Tag vines() { - return Tag.CAVE_VINES; - } -} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index c4436e1..aaf9df9 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,14 +1,17 @@ # 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 Tag to get the appropriate materials, or to use the groupings listed below. - use_tags_no_config: false + # 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: diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 7b5465b..5268e2a 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,4 +3,4 @@ version: '${version}' main: io.github.simplex.toolassist.ToolAssist api-version: 1.18 authors: [ SimplexDevelopment ] -description: A better vein miner plugin. +description: A lightweight vein-mining plugin.