From 95afbbd069371f8a036b9509bfec977b5ab8b52d Mon Sep 17 00:00:00 2001 From: WildInterloper <156627888+WildInterloper@users.noreply.github.com> Date: Tue, 5 Mar 2024 23:43:48 -0500 Subject: [PATCH] v0.6.0 - Added AutoSwitch option in config.yml. - Added auto_switch.yml, when AutoSwitch is set to true in the main config it will check what tools server admins want prisoners to autoswitch to and they can even customize what blocks will automatically trigger switching to certain tools. --- .idea/.gitignore | 2 + NVus_Prison_Setup.iml | 5 ++ pom.xml | 2 +- .../Listeners/ToolSwitchListener.java | 86 +++++++++++++++++++ .../nvus/nvus_prison_setup/PrisonSetup.java | 10 ++- src/main/resources/auto_switch.yml | 65 ++++++++++++++ src/main/resources/config.yml | 3 + 7 files changed, 168 insertions(+), 5 deletions(-) create mode 100644 src/main/java/me/nvus/nvus_prison_setup/Listeners/ToolSwitchListener.java create mode 100644 src/main/resources/auto_switch.yml diff --git a/.idea/.gitignore b/.idea/.gitignore index 26d3352..8f00030 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -1,3 +1,5 @@ # Default ignored files /shelf/ /workspace.xml +# GitHub Copilot persisted chat sessions +/copilot/chatSessions diff --git a/NVus_Prison_Setup.iml b/NVus_Prison_Setup.iml index bbeeb3e..c974104 100644 --- a/NVus_Prison_Setup.iml +++ b/NVus_Prison_Setup.iml @@ -1,5 +1,10 @@ + + + + + diff --git a/pom.xml b/pom.xml index 439a7cc..d191cb3 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.NVus NVus_Prison_Setup - 0.5.9 + 0.6.0 jar NVus_PrisonSetup diff --git a/src/main/java/me/nvus/nvus_prison_setup/Listeners/ToolSwitchListener.java b/src/main/java/me/nvus/nvus_prison_setup/Listeners/ToolSwitchListener.java new file mode 100644 index 0000000..bce9f5e --- /dev/null +++ b/src/main/java/me/nvus/nvus_prison_setup/Listeners/ToolSwitchListener.java @@ -0,0 +1,86 @@ +package me.nvus.nvus_prison_setup.Listeners; + +import me.nvus.nvus_prison_setup.Configs.ConfigManager; +import org.bukkit.Material; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; +import java.util.List; +import java.util.stream.Collectors; + +public class ToolSwitchListener implements Listener { + private ConfigManager configManager; + + public ToolSwitchListener(ConfigManager configManager) { + this.configManager = configManager; + } + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { + if (event.getAction() != Action.LEFT_CLICK_BLOCK) return; + + Player player = event.getPlayer(); + FileConfiguration config = configManager.getConfig("config.yml"); + if (!config.getBoolean("AutoSwitch", true)) return; + if (!player.hasPermission("nvus.prisoner")) return; + + FileConfiguration autoSwitchConfig = configManager.getConfig("auto_switch.yml"); + Material blockType = event.getClickedBlock().getType(); + + // Determine the correct tool based on block type, pulled from auto_switch.yml + Material bestTool = determineBestTool(blockType, player, autoSwitchConfig); + if (bestTool != null) { + switchToTool(player, bestTool); + } + } + + private Material determineBestTool(Material blockType, Player player, FileConfiguration autoSwitchConfig) { + List autoSwitchTools = autoSwitchConfig.getStringList("AutoSwitchTools"); + List pickaxeMaterials = convertStringListToMaterial(autoSwitchConfig.getStringList("PickaxeMaterials")); + List axeMaterials = convertStringListToMaterial(autoSwitchConfig.getStringList("AxeMaterials")); + List shovelMaterials = convertStringListToMaterial(autoSwitchConfig.getStringList("ShovelMaterials")); + + Material requiredTool = null; + if (pickaxeMaterials.contains(blockType)) { + requiredTool = Material.valueOf("IRON_PICKAXE"); + } else if (axeMaterials.contains(blockType)) { + requiredTool = Material.valueOf("IRON_AXE"); + } else if (shovelMaterials.contains(blockType)) { + requiredTool = Material.valueOf("IRON_SHOVEL"); + } + + // Ensure the tool is in the list of tools that should auto switch + if (requiredTool != null && autoSwitchTools.contains(requiredTool.name())) { + return findBestToolInInventory(requiredTool, player); + } + + return null; + } + + private Material findBestToolInInventory(Material toolType, Player player) { + PlayerInventory inventory = player.getInventory(); + for (ItemStack item : inventory.getContents()) { + if (item != null && item.getType() == toolType) { + return toolType; // Further logic can be added to select the best tool if multiple are found + } + } + return null; + } + + private List convertStringListToMaterial(List stringList) { + return stringList.stream().map(Material::valueOf).collect(Collectors.toList()); + } + + private void switchToTool(Player player, Material tool) { + PlayerInventory inventory = player.getInventory(); + int toolSlot = inventory.first(tool); + if (toolSlot >= 0 && toolSlot < 9) { // If the tool is in the quickbar + inventory.setHeldItemSlot(toolSlot); + } + } +} diff --git a/src/main/java/me/nvus/nvus_prison_setup/PrisonSetup.java b/src/main/java/me/nvus/nvus_prison_setup/PrisonSetup.java index 40de988..8f42abf 100644 --- a/src/main/java/me/nvus/nvus_prison_setup/PrisonSetup.java +++ b/src/main/java/me/nvus/nvus_prison_setup/PrisonSetup.java @@ -5,6 +5,7 @@ import me.nvus.nvus_prison_setup.Listeners.PlayerArmor; import me.nvus.nvus_prison_setup.Listeners.PlayerItems; import me.nvus.nvus_prison_setup.Listeners.PlayerSpawn; import me.nvus.nvus_prison_setup.Listeners.BlockListener; +import me.nvus.nvus_prison_setup.Listeners.ToolSwitchListener; import org.bukkit.ChatColor; import org.bukkit.plugin.java.JavaPlugin; @@ -17,18 +18,18 @@ public final class PrisonSetup extends JavaPlugin { // Initialize the ConfigManager configManager = new ConfigManager(this); - // Save the default config if it doesn't exist + // Save the default configs, if they doesn't exist + configManager.saveDefaultConfig("config.yml"); configManager.saveDefaultConfig("banned_items.yml"); - configManager.saveDefaultConfig("config.yml"); // Add this line + configManager.saveDefaultConfig("auto_switch.yml"); // Register event listeners getServer().getPluginManager().registerEvents(new PlayerSpawn(), this); getServer().getPluginManager().registerEvents(new PlayerArmor(), this); getServer().getPluginManager().registerEvents(new PlayerItems(configManager), this); getServer().getPluginManager().registerEvents(new BlockListener(this), this); + getServer().getPluginManager().registerEvents(new ToolSwitchListener(configManager), this); - - // Log a success message getLogger().info(ChatColor.translateAlternateColorCodes('&',"&a&lNVus Prison Setup has been successfully enabled!")); } @@ -38,6 +39,7 @@ public final class PrisonSetup extends JavaPlugin { // Save the config when disabling the plugin configManager.saveConfig("config.yml"); configManager.saveConfig("banned_items.yml"); + configManager.saveConfig("auto_switch.yml"); // Log a success message getLogger().info(ChatColor.translateAlternateColorCodes('&',"&c&lNVus Prison Setup has been successfully disabled!")); diff --git a/src/main/resources/auto_switch.yml b/src/main/resources/auto_switch.yml new file mode 100644 index 0000000..f15f916 --- /dev/null +++ b/src/main/resources/auto_switch.yml @@ -0,0 +1,65 @@ +#======================================================================================# +# NVus PRISON SETUP # +# by never2nv # +# www.FNGnation.net # +# Discord: FNGnation.net/discord # +#======================================================================================# + +# If AutoSwitch = true in the config.yml, what tools should the prisoner auto switch to? +# I'd always keep some kind of pickaxe, axe and shovel in here even if you don't want to auto switch to them. +# Just eliminate the Materials list for that tool(s) to disable that particular tool. Just to prevent conflicts/errors. +AutoSwitchTools: + - IRON_PICKAXE + - IRON_AXE + - IRON_SHOVEL + +# What blocks/ores should cause the prisoner to auto switch to a pickaxe? +PickaxeMaterials: + - STONE + - COBBLESTONE + - ANDESITE + - DIORITE + - GRANITE + - NETHERRACK + - END_STONE + - SANDSTONE + - RED_SANDSTONE + - OBSIDIAN +# Here's all the ORES that will trigger switching to a piacke: + - COAL_ORE + - IRON_ORE + - GOLD_ORE + - DIAMOND_ORE + - LAPIS_ORE + - REDSTONE_ORE + - EMERALD_ORE + - NETHER_QUARTZ_ORE + - ANCIENT_DEBRIS + +# What blocks should cause the prisoner to auto switch to an axe? +AxeMaterials: + - OAK_LOG + - OAK_PLANKS + - BIRCH_LOG + - BIRCH_PLANKS + - SPRUCE_LOG + - SPRUCE_PLANKS + - JUNGLE_LOG + - JUNGLE_PLANKS + - ACACIA_LOG + - ACACIA_PLANKS + - DARK_OAK_LOG + - DARK_OAK_PLANKS + +# What blocks should cause the prisoner to auto switch to a shovel? +ShovelMaterials: + - GRASS_BLOCK + - DIRT + - COARSE_DIRT + - PODZOL + - SAND + - RED_SAND + - GRAVEL + - CLAY + - SNOW + - SNOW_BLOCK \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 4a31f68..0560f88 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -9,3 +9,6 @@ # Should prisoners auto pickup blocks they have mined? AutoPickup: true +# Should prisoners be able to auto switch to the correct tool for whatever they are mining? +#i.e. If hitting dirt/clay with a pickaxe, they will auto switch to a shovel (if in their quickbar) +AutoSwitch: true