v0.8.1 - Added ToolDamage, moved and renamed PrisonerTools from auto_switch.yml to config.yml. Added ToolDamage and reload config toggles to /nvus menu

This commit is contained in:
WildInterloper 2024-03-10 12:33:30 -04:00
parent 900e2ca0ed
commit a9f4e80094
8 changed files with 129 additions and 73 deletions

View File

@ -1,16 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module version="4"> <module version="4">
<component name="AdditionalModuleElements">
<content url="file://$MODULE_DIR$" dumb="true">
<excludeFolder url="file://$MODULE_DIR$/.idea/copilot/chatSessions" />
</content>
</component>
<component name="FacetManager"> <component name="FacetManager">
<facet type="minecraft" name="Minecraft"> <facet type="minecraft" name="Minecraft">
<configuration> <configuration>
<autoDetectTypes> <autoDetectTypes>
<platformType>PAPER</platformType> <platformType>PAPER</platformType>
<platformType>ADVENTURE</platformType> <platformType>ADVENTURE</platformType>
<platformType>SPIGOT</platformType>
</autoDetectTypes> </autoDetectTypes>
<projectReimportVersion>1</projectReimportVersion> <projectReimportVersion>1</projectReimportVersion>
</configuration> </configuration>

View File

@ -6,7 +6,7 @@
<groupId>me.NVus</groupId> <groupId>me.NVus</groupId>
<artifactId>NVus_Prison_Setup</artifactId> <artifactId>NVus_Prison_Setup</artifactId>
<version>0.7.5</version> <version>0.8.1</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>NVus_PrisonSetup</name> <name>NVus_PrisonSetup</name>

View File

@ -31,14 +31,17 @@ public class SettingsMenu implements Listener {
} }
public void openSettingsMenu(Player player) { public void openSettingsMenu(Player player) {
Inventory inv = Bukkit.createInventory(null, 9, ChatColor.DARK_GREEN + "Settings Menu"); Inventory inv = Bukkit.createInventory(null, 9, ChatColor.DARK_GREEN + "NVus Prison Settings");
FileConfiguration config = configManager.getConfig("config.yml"); FileConfiguration config = configManager.getConfig("config.yml");
inv.setItem(0, createToggleItem(Material.LEATHER_CHESTPLATE, "Toggle PrisonerArmor", config.getBoolean("PrisonerArmor", false))); inv.setItem(0, createToggleItem(Material.LEATHER_CHESTPLATE, "Toggle PrisonerArmor", config.getBoolean("PrisonerArmor", true)));
inv.setItem(1, createToggleItem(Material.IRON_DOOR, "Toggle RestrictArmor", config.getBoolean("RestrictArmor", false))); inv.setItem(1, createToggleItem(Material.IRON_CHESTPLATE, "Toggle RestrictArmor", config.getBoolean("RestrictArmor", true)));
inv.setItem(3, createToggleItem(Material.HOPPER, "Toggle AutoPickup", config.getBoolean("AutoPickup", false)));
inv.setItem(3, createToggleItem(Material.HOPPER, "Toggle AutoPickup", config.getBoolean("AutoPickup", true)));
inv.setItem(4, createToggleItem(Material.LEVER, "Toggle AutoSwitch", config.getBoolean("AutoSwitch", false))); inv.setItem(4, createToggleItem(Material.LEVER, "Toggle AutoSwitch", config.getBoolean("AutoSwitch", false)));
inv.setItem(7, createItem(Material.BOOK, ChatColor.GREEN + "Reload Configs"));
inv.setItem(6, createToggleItem(Material.IRON_PICKAXE, "Toggle ToolDamage", config.getBoolean("ToolDamage", false)));
inv.setItem(7, createToggleItem(Material.BOOK, "Reload Configs", false));
player.openInventory(inv); player.openInventory(inv);
} }
@ -46,22 +49,30 @@ public class SettingsMenu implements Listener {
private ItemStack createToggleItem(Material material, String name, boolean isEnabled) { private ItemStack createToggleItem(Material material, String name, boolean isEnabled) {
ItemStack item = new ItemStack(material); ItemStack item = new ItemStack(material);
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
meta.setDisplayName(ChatColor.GREEN + name + ": " + (isEnabled ? ChatColor.BLUE + "Enabled" : ChatColor.RED + "Disabled")); // Non-Toggable Items
if (name.equals("Reload Configs")) {
meta.setDisplayName(ChatColor.GREEN + name);
}
// Toggable Items
else {
meta.setDisplayName(ChatColor.GREEN + name + ": " + (isEnabled ? ChatColor.BLUE + "Enabled" : ChatColor.RED + "Disabled"));
}
item.setItemMeta(meta); item.setItemMeta(meta);
return item; return item;
} }
private ItemStack createItem(Material material, String name) { // private ItemStack createItem(Material material, String name) {
ItemStack item = new ItemStack(material); // ItemStack item = new ItemStack(material);
ItemMeta meta = item.getItemMeta(); // ItemMeta meta = item.getItemMeta();
meta.setDisplayName(name); // meta.setDisplayName(name);
item.setItemMeta(meta); // item.setItemMeta(meta);
return item; // return item;
} // }
@EventHandler @EventHandler
public void onInventoryClick(InventoryClickEvent event) { public void onInventoryClick(InventoryClickEvent event) {
if (!event.getView().getTitle().equals(ChatColor.DARK_GREEN + "Settings Menu")) return; if (!event.getView().getTitle().equals(ChatColor.DARK_GREEN + "NVus Prison Settings")) return;
event.setCancelled(true); event.setCancelled(true);
Player player = (Player) event.getWhoClicked(); Player player = (Player) event.getWhoClicked();
@ -95,6 +106,7 @@ public class SettingsMenu implements Listener {
} else if (displayName.contains("Reload Configs")) { } else if (displayName.contains("Reload Configs")) {
reloadConfigs(player); reloadConfigs(player);
} }
}; };
playerTasks.put(playerUUID, task); playerTasks.put(playerUUID, task);
@ -125,5 +137,6 @@ public class SettingsMenu implements Listener {
configManager.reloadConfig("banned_items.yml"); configManager.reloadConfig("banned_items.yml");
configManager.saveConfig("config.yml"); configManager.saveConfig("config.yml");
player.sendMessage(ChatColor.GREEN + "Configuration files reloaded."); player.sendMessage(ChatColor.GREEN + "Configuration files reloaded.");
player.closeInventory();
} }
} }

View File

@ -6,15 +6,17 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.PlayerInventory;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ToolSwitchListener implements Listener { public class ToolSwitchListener implements Listener {
private ConfigManager configManager; private final ConfigManager configManager;
public ToolSwitchListener(ConfigManager configManager) { public ToolSwitchListener(ConfigManager configManager) {
this.configManager = configManager; this.configManager = configManager;
@ -22,59 +24,49 @@ public class ToolSwitchListener implements Listener {
@EventHandler @EventHandler
public void onPlayerInteract(PlayerInteractEvent event) { public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() != Action.LEFT_CLICK_BLOCK) return; if (event.getAction() != org.bukkit.event.block.Action.LEFT_CLICK_BLOCK || event.getClickedBlock() == null) return;
Player player = event.getPlayer(); Player player = event.getPlayer();
FileConfiguration config = configManager.getConfig("config.yml"); 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"); 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 if (!player.hasPermission("nvus.prisoner") || !config.getBoolean("AutoSwitch", true)) return;
Material bestTool = determineBestTool(blockType, player, autoSwitchConfig);
Material blockType = event.getClickedBlock().getType();
Material bestTool = determineBestToolForBlock(blockType, player, config, autoSwitchConfig);
if (bestTool != null) { if (bestTool != null) {
switchToTool(player, bestTool); switchToTool(player, bestTool);
} }
} }
private Material determineBestTool(Material blockType, Player player, FileConfiguration autoSwitchConfig) { private Material determineBestToolForBlock(Material blockType, Player player, FileConfiguration config, FileConfiguration autoSwitchConfig) {
List<String> autoSwitchTools = autoSwitchConfig.getStringList("AutoSwitchTools"); List<Material> prisonerTools = config.getStringList("PrisonerTools").stream()
List<Material> pickaxeMaterials = convertStringListToMaterial(autoSwitchConfig.getStringList("PickaxeMaterials")); .map(Material::valueOf)
List<Material> axeMaterials = convertStringListToMaterial(autoSwitchConfig.getStringList("AxeMaterials")); .collect(Collectors.toList());
List<Material> 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 Map<Material, List<Material>> toolEffectivenessMap = new HashMap<>();
if (requiredTool != null && autoSwitchTools.contains(requiredTool.name())) { for (Material tool : prisonerTools) {
return findBestToolInInventory(requiredTool, player);
}
return null; if (tool.toString().endsWith("_PICKAXE")) {
} toolEffectivenessMap.put(tool, convertStringListToMaterial(autoSwitchConfig.getStringList("PickaxeMaterials")));
} else if (tool.toString().endsWith("_AXE")) {
private Material findBestToolInInventory(Material toolType, Player player) { toolEffectivenessMap.put(tool, convertStringListToMaterial(autoSwitchConfig.getStringList("AxeMaterials")));
PlayerInventory inventory = player.getInventory(); } else if (tool.toString().endsWith("_SHOVEL")) {
for (ItemStack item : inventory.getContents()) { toolEffectivenessMap.put(tool, convertStringListToMaterial(autoSwitchConfig.getStringList("ShovelMaterials")));
if (item != null && item.getType() == toolType) {
return toolType; // Further logic can be added to select the best tool if multiple are found
} }
} }
return null;
for (Material tool : prisonerTools) {
List<Material> effectiveBlocks = toolEffectivenessMap.getOrDefault(tool, List.of());
if (effectiveBlocks.contains(blockType) && player.getInventory().contains(tool)) {
return tool;
}
}
return null; // No suitable tool found
} }
private List<Material> convertStringListToMaterial(List<String> stringList) {
return stringList.stream().map(Material::valueOf).collect(Collectors.toList());
}
private void switchToTool(Player player, Material tool) { private void switchToTool(Player player, Material tool) {
PlayerInventory inventory = player.getInventory(); PlayerInventory inventory = player.getInventory();
@ -83,4 +75,8 @@ public class ToolSwitchListener implements Listener {
inventory.setHeldItemSlot(toolSlot); inventory.setHeldItemSlot(toolSlot);
} }
} }
private List<Material> convertStringListToMaterial(List<String> stringList) {
return stringList.stream().map(Material::valueOf).collect(Collectors.toList());
}
} }

View File

@ -1,18 +1,20 @@
package me.nvus.nvus_prison_setup; package me.nvus.nvus_prison_setup;
import me.nvus.nvus_prison_setup.Configs.ConfigManager; import me.nvus.nvus_prison_setup.Configs.ConfigManager;
import me.nvus.nvus_prison_setup.Listeners.CommandListener; import me.nvus.nvus_prison_setup.Listeners.*;
import me.nvus.nvus_prison_setup.Configs.SettingsMenu; import me.nvus.nvus_prison_setup.Configs.SettingsMenu;
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 me.nvus.nvus_prison_setup.Updater.UpdateChecker; import me.nvus.nvus_prison_setup.Updater.UpdateChecker;
import me.nvus.nvus_prison_setup.Listeners.ToolDamageListener;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public final class PrisonSetup extends JavaPlugin { public final class PrisonSetup extends JavaPlugin {
@ -28,6 +30,9 @@ public final class PrisonSetup extends JavaPlugin {
configManager.saveDefaultConfig("banned_items.yml"); configManager.saveDefaultConfig("banned_items.yml");
configManager.saveDefaultConfig("auto_switch.yml"); configManager.saveDefaultConfig("auto_switch.yml");
// Update config file(s) when new updates come out and they need replaced
checkAndUpdateConfigs();
// Register event listeners // Register event listeners
getServer().getPluginManager().registerEvents(new PlayerSpawn(configManager), this); getServer().getPluginManager().registerEvents(new PlayerSpawn(configManager), this);
getServer().getPluginManager().registerEvents(new PlayerArmor(configManager), this); getServer().getPluginManager().registerEvents(new PlayerArmor(configManager), this);
@ -38,6 +43,10 @@ public final class PrisonSetup extends JavaPlugin {
//new SettingsMenu(this, configManager); //new SettingsMenu(this, configManager);
getServer().getPluginManager().registerEvents(new SettingsMenu(this, configManager), this); getServer().getPluginManager().registerEvents(new SettingsMenu(this, configManager), this);
// Tool Damage
ToolDamageListener toolDamageListener = new ToolDamageListener(configManager);
getServer().getPluginManager().registerEvents(toolDamageListener, this);
getLogger().info(ChatColor.translateAlternateColorCodes('&',"&a&lNVus Prison Setup has been successfully enabled!")); getLogger().info(ChatColor.translateAlternateColorCodes('&',"&a&lNVus Prison Setup has been successfully enabled!"));
new UpdateChecker(this, 12345).getVersion(version -> { new UpdateChecker(this, 12345).getVersion(version -> {
@ -73,4 +82,42 @@ public final class PrisonSetup extends JavaPlugin {
public ConfigManager getConfigManager() { public ConfigManager getConfigManager() {
return configManager; return configManager;
} }
private void checkAndUpdateConfigs() {
File configFile = new File(getDataFolder(), "config.yml");
if (!configFile.exists()) {
saveResource("config.yml", false);
saveResource("auto_switch.yml", false);
return;
}
FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
String configVersion = config.getString("ConfigVersion", "0"); // Default to "0" if not found
if (!"1.0".equals(configVersion)) {
backupAndReplaceConfig("config.yml");
backupAndReplaceConfig("auto_switch.yml");
// After backing up and copying new ones, you might want to reload these configs into your ConfigManager
// assuming you have such a method in your ConfigManager
getConfigManager().reloadConfig("config.yml");
getConfigManager().reloadConfig("auto_switch.yml");
}
}
private void backupAndReplaceConfig(String fileName) {
File configFile = new File(getDataFolder(), fileName);
File backupFile = new File(getDataFolder(), fileName + ".BACKUP");
// Backup the old file
try {
Files.move(configFile.toPath(), backupFile.toPath());
} catch (IOException e) {
getLogger().warning("Could not backup " + fileName + ": " + e.getMessage());
}
// Copy new file from resources
saveResource(fileName, false);
}
} }

View File

@ -5,14 +5,6 @@
# Discord: FNGnation.net/discord # # 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? # What blocks/ores should cause the prisoner to auto switch to a pickaxe?
PickaxeMaterials: PickaxeMaterials:
- STONE - STONE

View File

@ -20,3 +20,15 @@ AutoSwitch: true
PrisonerArmor: true PrisonerArmor: true
# Should prisoners be able to remove/change their default prisoner armor? # Should prisoners be able to remove/change their default prisoner armor?
RestrictArmor: true RestrictArmor: true
# What tools are considered Prisoner Tools? These are used for the ToolDamage toggle and for auto switching feature!
# See auto_switch.yml to set up which blocks trigger the auto switch to these tools.
PrisonerTools:
- IRON_PICKAXE
- IRON_AXE
- IRON_SHOVEL
# When set to FALSE it will prevent Prisoner Tools from receiving damage when mining etc.
ToolDamage: false
ConfigVersion: 1.0 # Do not change this value!

View File

@ -2,7 +2,7 @@ name: NVus_PrisonSetup
version: '${project.version}' version: '${project.version}'
main: me.nvus.nvus_prison_setup.PrisonSetup main: me.nvus.nvus_prison_setup.PrisonSetup
api-version: '1.20' api-version: '1.20'
prefix: NVus Prison prefix: NVus Prison Lite
authors: [never2nv] authors: [never2nv]
website: https://FNGnation.net website: https://FNGnation.net