From 443f0b053a990f9933638ca898bf76ade5cb3ccb Mon Sep 17 00:00:00 2001 From: BitByLogic <33465177+BitByLogics@users.noreply.github.com> Date: Tue, 21 May 2024 04:36:12 -0400 Subject: [PATCH] Code overhaul/cleanup - Downsize classes - Overhaul config.yml - Take advantage of BlockDropItemEvent for drop logic - Allow configuration of drops to remove - Add /dropsoverhaul command - Add /dropsoverhaul reload to reload configuration - Rename /killdrops to /dropsoverhaul killdrops - Allow messages to be configured in config.yml - Remove default-everyone-killdrops option, permission can just be negated - Add BitByLogic to authors --- .../dropsoverhaul/DropsOverhaul.java | 43 +++++++ .../infotoast/dropsoverhaul/ItemLists.java | 100 ---------------- .../org/infotoast/dropsoverhaul/Listener.java | 110 ------------------ .../org/infotoast/dropsoverhaul/Plugin.java | 61 ---------- .../command/DropsOverhaulCommand.java | 82 +++++++++++++ src/main/resources/config.yml | 43 +++++-- src/main/resources/plugin.yml | 17 ++- 7 files changed, 170 insertions(+), 286 deletions(-) create mode 100644 src/main/java/org/infotoast/dropsoverhaul/DropsOverhaul.java delete mode 100644 src/main/java/org/infotoast/dropsoverhaul/ItemLists.java delete mode 100644 src/main/java/org/infotoast/dropsoverhaul/Listener.java delete mode 100644 src/main/java/org/infotoast/dropsoverhaul/Plugin.java create mode 100644 src/main/java/org/infotoast/dropsoverhaul/command/DropsOverhaulCommand.java diff --git a/src/main/java/org/infotoast/dropsoverhaul/DropsOverhaul.java b/src/main/java/org/infotoast/dropsoverhaul/DropsOverhaul.java new file mode 100644 index 0000000..e4072c0 --- /dev/null +++ b/src/main/java/org/infotoast/dropsoverhaul/DropsOverhaul.java @@ -0,0 +1,43 @@ +package org.infotoast.dropsoverhaul; + +import org.bukkit.entity.Item; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockDropItemEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.java.JavaPlugin; +import org.infotoast.dropsoverhaul.command.DropsOverhaulCommand; + +import java.util.HashMap; + +public class DropsOverhaul extends JavaPlugin implements Listener { + + @Override + public void onEnable() { + saveDefaultConfig(); + + getCommand("dropsoverhaul").setExecutor(new DropsOverhaulCommand(this)); + getServer().getPluginManager().registerEvents(this, this); + + getLogger().info("DropsOverhaul has been enabled!"); + } + + @EventHandler + public void onBlockDrop(BlockDropItemEvent event) { + if (!getConfig().getStringList("Settings.Drops-To-Remove").contains(event.getBlockState().getType().name())) { + return; + } + + event.setCancelled(true); + + ItemStack[] items = event.getItems().stream().map(Item::getItemStack).toArray(ItemStack[]::new); + HashMap leftOver = event.getPlayer().getInventory().addItem(items); + + if (leftOver.isEmpty() || getConfig().getBoolean("Settings.Full-Inventory-Delete")) { + return; + } + + leftOver.values().forEach(item -> event.getPlayer().getWorld().dropItemNaturally(event.getPlayer().getLocation(), item)); + } + +} diff --git a/src/main/java/org/infotoast/dropsoverhaul/ItemLists.java b/src/main/java/org/infotoast/dropsoverhaul/ItemLists.java deleted file mode 100644 index 8b679c2..0000000 --- a/src/main/java/org/infotoast/dropsoverhaul/ItemLists.java +++ /dev/null @@ -1,100 +0,0 @@ -package org.infotoast.dropsoverhaul; - -import org.bukkit.Material; - -import java.util.ArrayList; -import java.util.List; - -public class ItemLists { - private List deletables = new ArrayList<>(); - private List tools = new ArrayList<>(); - - private List canBreakStone = new ArrayList<>(); - - private List requiresPickaxe = new ArrayList<>(); - public ItemLists() { - deletables.add(Material.DIRT); - deletables.add(Material.STONE); - deletables.add(Material.SAND); - deletables.add(Material.SANDSTONE); - deletables.add(Material.DIORITE); - deletables.add(Material.GRANITE); - deletables.add(Material.ANDESITE); - deletables.add(Material.GRAVEL); - deletables.add(Material.NETHERRACK); - deletables.add(Material.BLACKSTONE); - deletables.add(Material.BASALT); - deletables.add(Material.GRASS_BLOCK); - deletables.add(Material.TUFF); - deletables.add(Material.CALCITE); - deletables.add(Material.COBBLED_DEEPSLATE); - deletables.add(Material.DEEPSLATE); - deletables.add(Material.SMOOTH_BASALT); - tools.add(Material.WOODEN_AXE); - tools.add(Material.WOODEN_HOE); - tools.add(Material.WOODEN_PICKAXE); - tools.add(Material.WOODEN_SHOVEL); - tools.add(Material.WOODEN_SWORD); - tools.add(Material.STONE_AXE); - tools.add(Material.STONE_HOE); - tools.add(Material.STONE_PICKAXE); - tools.add(Material.STONE_SHOVEL); - tools.add(Material.STONE_SWORD); - tools.add(Material.GOLDEN_AXE); - tools.add(Material.GOLDEN_HOE); - tools.add(Material.GOLDEN_PICKAXE); - tools.add(Material.GOLDEN_SHOVEL); - tools.add(Material.GOLDEN_SWORD); - tools.add(Material.IRON_AXE); - tools.add(Material.IRON_HOE); - tools.add(Material.IRON_PICKAXE); - tools.add(Material.IRON_SHOVEL); - tools.add(Material.IRON_SWORD); - tools.add(Material.DIAMOND_AXE); - tools.add(Material.DIAMOND_HOE); - tools.add(Material.DIAMOND_PICKAXE); - tools.add(Material.DIAMOND_SHOVEL); - tools.add(Material.DIAMOND_SWORD); - tools.add(Material.NETHERITE_AXE); - tools.add(Material.NETHERITE_HOE); - tools.add(Material.NETHERITE_PICKAXE); - tools.add(Material.NETHERITE_SHOVEL); - tools.add(Material.NETHERITE_SWORD); - tools.add(Material.TRIDENT); - canBreakStone.add(Material.WOODEN_PICKAXE); - canBreakStone.add(Material.STONE_PICKAXE); - canBreakStone.add(Material.GOLDEN_PICKAXE); - canBreakStone.add(Material.IRON_PICKAXE); - canBreakStone.add(Material.DIAMOND_PICKAXE); - canBreakStone.add(Material.NETHERITE_PICKAXE); - requiresPickaxe.add(Material.STONE); - requiresPickaxe.add(Material.SANDSTONE); - requiresPickaxe.add(Material.ANDESITE); - requiresPickaxe.add(Material.GRANITE); - requiresPickaxe.add(Material.DIORITE); - requiresPickaxe.add(Material.NETHERRACK); - requiresPickaxe.add(Material.BLACKSTONE); - requiresPickaxe.add(Material.BASALT); - requiresPickaxe.add(Material.TUFF); - requiresPickaxe.add(Material.CALCITE); - requiresPickaxe.add(Material.COBBLED_DEEPSLATE); - requiresPickaxe.add(Material.DEEPSLATE); - requiresPickaxe.add(Material.SMOOTH_BASALT); - } - - public boolean isDeletable(Material mat) { - return deletables.contains(mat); - } - - public boolean isTool(Material mat) { - return tools.contains(mat); - } - - public boolean toolBreaksStone(Material mat) { - return canBreakStone.contains(mat); - } - - public boolean requiresPickaxe(Material mat) { - return requiresPickaxe.contains(mat); - } -} diff --git a/src/main/java/org/infotoast/dropsoverhaul/Listener.java b/src/main/java/org/infotoast/dropsoverhaul/Listener.java deleted file mode 100644 index 798fd32..0000000 --- a/src/main/java/org/infotoast/dropsoverhaul/Listener.java +++ /dev/null @@ -1,110 +0,0 @@ -package org.infotoast.dropsoverhaul; - -import org.bukkit.GameMode; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.entity.Item; -import org.bukkit.inventory.meta.Damageable; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.inventory.ItemStack; - -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - -public class Listener implements org.bukkit.event.Listener { - private Plugin pl; - public Listener(Plugin pl) { - this.pl = pl; - } - private Random rand = new Random(); - ItemLists lists = new ItemLists(); - - private ItemStack getResultItemStack(Player breaker, Material mat) { - ItemStack item; - item = new ItemStack(mat); - switch (mat) { - case GRASS_BLOCK: - if (breaker.getInventory().getItemInMainHand().containsEnchantment(Enchantment.SILK_TOUCH)) - item = new ItemStack(Material.GRASS_BLOCK); - else - item = new ItemStack(Material.DIRT); - break; - case STONE: - if (breaker.getInventory().getItemInMainHand().containsEnchantment(Enchantment.SILK_TOUCH)) - item = new ItemStack(Material.STONE); - else - item = new ItemStack(Material.COBBLESTONE); - break; - case GRAVEL: - if (new Random().nextInt(8) == 0) - item = new ItemStack(Material.FLINT); - else - item = new ItemStack(Material.GRAVEL); - break; - } - return item; - } - - private void processToolDamage(Player breaker) { - if (lists.isTool(breaker.getInventory().getItemInMainHand().getType())) - if (breaker.getInventory().getItemInMainHand().getItemMeta() instanceof Damageable) { - if (!(breaker.getInventory().getItemInMainHand().containsEnchantment(Enchantment.UNBREAKING) && rand.nextInt(100) > (100 / (breaker.getInventory().getItemInMainHand().getEnchantmentLevel(Enchantment.UNBREAKING) + 1)))) - breaker.getInventory().getItemInMainHand().setDurability((short)(((Damageable)breaker.getInventory().getItemInMainHand().getItemMeta()).getDamage() + 1)); - if (breaker.getInventory().getItemInMainHand().getDurability() >= breaker.getInventory().getItemInMainHand().getType().getMaxDurability() && breaker.getInventory().getItemInMainHand().getType().getMaxDurability() > 0) - breaker.getInventory().setItemInMainHand(new ItemStack(Material.AIR)); - } - } - @EventHandler - public void onBlockBreak(BlockBreakEvent evt) { - Player breaker = evt.getPlayer(); - Block blocc = evt.getBlock(); - Material mat = blocc.getType(); - if (lists.isDeletable(mat)) { - if (breaker.getGameMode() == GameMode.SURVIVAL) { - ItemStack item; - if (lists.requiresPickaxe(mat)) - if (lists.toolBreaksStone(breaker.getInventory().getItemInMainHand().getType())) - item = getResultItemStack(breaker, mat); - else { evt.setCancelled(true); blocc.setType(Material.AIR, true); return; } - else - item = getResultItemStack(breaker, mat); - - if (breaker.getInventory().firstEmpty() == -1) { - System.out.println("Inventory is full."); - Material resultMat = item.getType(); - if (breaker.getInventory().contains(resultMat)) { - System.out.println("Inventory contains valid item."); - ItemStack[] storageContents = breaker.getInventory().getStorageContents(); - for (ItemStack stack : storageContents) { - if (stack.getType() == resultMat) { - if (stack.getAmount() < resultMat.getMaxStackSize()) { - stack.add(); - blocc.setType(Material.AIR, true); - processToolDamage(breaker); - evt.setCancelled(true); - return; - } - } - } - } - FileConfiguration conf = pl.getCustomConfig(); - if (!conf.getBoolean("full-inventory-delete")) return; - blocc.setType(Material.AIR, true); - processToolDamage(breaker); - evt.setCancelled(true); - return; - } - - breaker.getInventory().addItem(item); - blocc.setType(Material.AIR, true); - processToolDamage(breaker); - evt.setCancelled(true); - } - } - } -} diff --git a/src/main/java/org/infotoast/dropsoverhaul/Plugin.java b/src/main/java/org/infotoast/dropsoverhaul/Plugin.java deleted file mode 100644 index 7a8705c..0000000 --- a/src/main/java/org/infotoast/dropsoverhaul/Plugin.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.infotoast.dropsoverhaul; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.plugin.java.JavaPlugin; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.logging.Logger; - -public class Plugin extends JavaPlugin { - private File customConfigFile; - private FileConfiguration customConfig; - public static Logger logger; - @Override - public void onEnable() { - logger = getLogger(); - createCustomConfig(); - getServer().getPluginManager().registerEvents(new Listener(this), this); - logger.info("DropsOverhaul has been enabled!"); - } - - public FileConfiguration getCustomConfig() { - return customConfig; - } - - private void createCustomConfig() { - customConfigFile = new File(getDataFolder(), "config.yml"); - if (!customConfigFile.exists()) { - customConfigFile.getParentFile().mkdirs(); - saveResource("config.yml", false); - } - - customConfig = new YamlConfiguration(); - try { - customConfig.load(customConfigFile); - } catch (InvalidConfigurationException | IOException e) { - e.printStackTrace(); - } - } - - @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (label.equalsIgnoreCase("killdrops")) { - boolean permDefault = customConfig.getBoolean("default-everyone-killdrops"); - if (permDefault || sender.hasPermission("dropsoverhaul.killdrops")) { - int radius = customConfig.getInt("killdrops-radius"); - String cmd = "execute as " + sender.getName() + " at " + sender.getName() + " run kill @e[type=item,distance=.." + radius + "]"; - getServer().dispatchCommand(getServer().getConsoleSender(), cmd); - return true; - } - sender.sendMessage("§cAccess denied.§r"); - return false; - } - return false; - } -} diff --git a/src/main/java/org/infotoast/dropsoverhaul/command/DropsOverhaulCommand.java b/src/main/java/org/infotoast/dropsoverhaul/command/DropsOverhaulCommand.java new file mode 100644 index 0000000..5ac3d6a --- /dev/null +++ b/src/main/java/org/infotoast/dropsoverhaul/command/DropsOverhaulCommand.java @@ -0,0 +1,82 @@ +package org.infotoast.dropsoverhaul.command; + +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Item; +import org.bukkit.entity.Player; +import org.infotoast.dropsoverhaul.DropsOverhaul; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; + +public class DropsOverhaulCommand implements CommandExecutor { + + private final DropsOverhaul plugin; + + public DropsOverhaulCommand(DropsOverhaul plugin) { + this.plugin = plugin; + } + + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) { + if (args.length == 0) { + sendHelp(sender); + return true; + } + + if (args[0].equalsIgnoreCase("reload")) { + if (!sender.hasPermission("dropsoverhaul.reload")) { + sender.sendMessage(colorize(plugin.getConfig().getString("Messages.No-Permission", "&cAccess denied."))); + return true; + } + + plugin.reloadConfig(); + sender.sendMessage(colorize(plugin.getConfig().getString("Messages.Command.Reloaded", "&aSuccessfully reloaded configuration."))); + return true; + } + + if (args[0].equalsIgnoreCase("killdrops")) { + if (!sender.hasPermission("dropsoverhaul.killdrops")) { + sender.sendMessage(colorize(plugin.getConfig().getString("Messages.No-Permission", "&cAccess denied."))); + return true; + } + + if (!(sender instanceof Player player)) { + sender.sendMessage(colorize(plugin.getConfig().getString("Messages.Players-Only", "&cOnly players can use this command."))); + return true; + } + + int radius = plugin.getConfig().getInt("Settings.Kill-Drops-Radius"); + + Collection nearbyItems = player.getWorld().getNearbyEntitiesByType(Item.class, player.getLocation(), radius); + int itemAmount = nearbyItems.size(); + nearbyItems.forEach(Item::remove); + + player.sendMessage(colorize(plugin.getConfig().getString("Messages.Item-Delete", "&aSuccessfully removed %amount% items.")) + .replace("%amount%", itemAmount + "")); + return true; + } + + sendHelp(sender); + return true; + } + + private void sendHelp(CommandSender sender) { + sender.sendMessage(colorize(plugin.getConfig().getString("Messages.Command.Help-Header", "&aDrops Overhaul Commands"))); + + if (sender.hasPermission("dropsoverhaul.reload")) { + sender.sendMessage(colorize(plugin.getConfig().getString("Messages.Command.Reload-Command", "&7/do reload &8- &eReloads the configuration."))); + } + + if (sender.hasPermission("dropsoverhaul.killdrops")) { + sender.sendMessage(colorize(plugin.getConfig().getString("Messages.Command.Kill-Drops-Command", "&7/do killdrops &8- &eKills nearby items."))); + } + } + + private String colorize(String message) { + return ChatColor.translateAlternateColorCodes('&', message); + } + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 74d730e..1503e3f 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,10 +1,35 @@ -# DropsOverhaul configuration +# DropsOverhaul Configuration # +Settings: + # When /killdrops is used, what radius should the player be allowed to kill drops around them? + Kill-Drops-Radius: 20 + # If the player's inventory is full, should the plugin delete junk blocks altogether, or should it drop the junk blocks and do the normal behavior + Full-Inventory-Delete: true + # List of materials to remove when mined + Drops-To-Remove: + - "DIRT" + - "STONE" + - "SAND" + - "SANDSTONE" + - "DIORITE" + - "GRANITE" + - "ANDESITE" + - "GRAVEL" + - "NETHERRACK" + - "BLACKSTONE" + - "BASALT" + - "GRASS_BLOCK" + - "TUFF" + - "CALCITE" + - "COBBLED_DEEPSLATE" + - "DEEPSLATE" + - "SMOOTH_BASALT" -# When /killdrops is used, what radius should the player be allowed to kill drops around them? -killdrops-radius: 20 - -# Should everyone be allowed to use /killdrops? If false, the permission dropsoverhaul.killdrops will be used. -default-everyone-killdrops: true - -# If the player's inventory is full, should the plugin delete junk blocks altogether, or should it drop the junk blocks and do the normal behavior -full-inventory-delete: true \ No newline at end of file +Messages: + Player-Only: "&cOnly players can use this command." + No-Permission: "&cAccess denied." + Item-Delete: "&aSuccessfully removed %amount% items." + Command: + Help-Header: "&aDrops Overhaul Commands" + Reload-Command: "&7/do reload &8- &eReloads the configuration." + Kill-Drops-Command: "&7/do killdrops &8- &eKills nearby items." + Reloaded: "&aSuccessfully reloaded configuration." diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index d9d347a..6cdb49f 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,16 +1,21 @@ name: DropsOverhaul version: '${version}' -main: org.infotoast.dropsoverhaul.Plugin +main: org.infotoast.dropsoverhaul.DropsOverhaul api-version: 1.20 prefix: DropsOverhaul -authors: [ Frank ] +authors: [ Frank, BitByLogic ] description: Innovative lag prevention plugin stopping drops before they happen! website: infotoast.org + commands: - killdrops: - description: This command will allow players to kill all drops in the radius specified in the configuration, default 20 + dropsoverhaul: + aliases: [ "do" ] + description: Reload the configuration or kill all nearby item entities. + permissions: + dropsoverhaul.reload: + description: Players who have this permission can use /do reload + default: false dropsoverhaul.killdrops: - descrption: An optional permission that can be enabled in the config that allows you to determine who is allowed to run /killdrops + description: Players who have this permission can use /do killdrops default: true -