From 555c6e22844cae62d4b2492aab03f263a2a3c8d6 Mon Sep 17 00:00:00 2001 From: Indyuce Date: Thu, 3 Nov 2022 19:12:00 +0100 Subject: [PATCH] Cleaned up custom block breaking --- .../mmoitems/api/block/CustomBlock.java | 92 ++++----- .../stat/block/RequirePowerToBreak.java | 12 +- .../net/Indyuce/mmoitems/util/MMOUtils.java | 52 +++-- .../listener/CustomBlockListener.java | 177 ++++++++---------- 4 files changed, 164 insertions(+), 169 deletions(-) diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/block/CustomBlock.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/block/CustomBlock.java index e7cf1c8e..1cfe65d6 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/block/CustomBlock.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/block/CustomBlock.java @@ -7,67 +7,71 @@ import net.Indyuce.mmoitems.api.util.MushroomState; import net.Indyuce.mmoitems.stat.data.BooleanData; import net.Indyuce.mmoitems.stat.data.DoubleData; import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.Nullable; + +import java.util.Random; public class CustomBlock { - private final int id; - private final MushroomState state; + private final int id; + private final MushroomState state; - private final MMOItem mmoitem; + private final MMOItem mmoitem; - private final WorldGenTemplate template; - private final int minExp, maxExp, requiredPower; - private final boolean requirePowerToBreak; + @Nullable + private final WorldGenTemplate template; + private final int minExp, maxExp, requiredPower; + private final boolean requirePowerToBreak; - public CustomBlock(MushroomState state, MMOItem mmoitem) { - this.mmoitem = mmoitem; + private static final Random RANDOM = new Random(); - this.id = (mmoitem.hasData(ItemStats.BLOCK_ID)) ? (int) ((DoubleData) mmoitem.getData(ItemStats.BLOCK_ID)).getValue() : 0; - this.state = state; + public CustomBlock(MushroomState state, MMOItem mmoitem) { + this.mmoitem = mmoitem; - this.minExp = (mmoitem.hasData(ItemStats.MIN_XP)) ? (int) ((DoubleData) mmoitem.getData(ItemStats.MIN_XP)).getValue() : 0; - this.maxExp = (mmoitem.hasData(ItemStats.MAX_XP)) ? (int) ((DoubleData) mmoitem.getData(ItemStats.MAX_XP)).getValue() : 0; - this.requiredPower = (mmoitem.hasData(ItemStats.REQUIRED_POWER)) ? (int) ((DoubleData) mmoitem.getData(ItemStats.REQUIRED_POWER)).getValue() - : 0; - - this.requirePowerToBreak = (mmoitem.hasData(ItemStats.REQUIRE_POWER_TO_BREAK)) ? (boolean) ((BooleanData) mmoitem.getData(ItemStats.REQUIRE_POWER_TO_BREAK)).isEnabled() : false; + this.id = mmoitem.hasData(ItemStats.BLOCK_ID) ? (int) ((DoubleData) mmoitem.getData(ItemStats.BLOCK_ID)).getValue() : 0; + this.state = state; - this.template = (mmoitem.hasData(ItemStats.GEN_TEMPLATE)) - ? MMOItems.plugin.getWorldGen().getOrThrow((mmoitem.getData(ItemStats.GEN_TEMPLATE)).toString()) - : null; - } + this.minExp = mmoitem.hasData(ItemStats.MIN_XP) ? (int) ((DoubleData) mmoitem.getData(ItemStats.MIN_XP)).getValue() : 0; + this.maxExp = mmoitem.hasData(ItemStats.MAX_XP) ? (int) ((DoubleData) mmoitem.getData(ItemStats.MAX_XP)).getValue() : 0; + this.requiredPower = mmoitem.hasData(ItemStats.REQUIRED_POWER) ? (int) ((DoubleData) mmoitem.getData(ItemStats.REQUIRED_POWER)).getValue() : 0; + this.requirePowerToBreak = mmoitem.hasData(ItemStats.REQUIRE_POWER_TO_BREAK) && ((BooleanData) mmoitem.getData(ItemStats.REQUIRE_POWER_TO_BREAK)).isEnabled(); + this.template = mmoitem.hasData(ItemStats.GEN_TEMPLATE) ? MMOItems.plugin.getWorldGen().getOrThrow((mmoitem.getData(ItemStats.GEN_TEMPLATE)).toString()) : null; + } - public int getId() { - return id; - } + public int getId() { + return id; + } + public MushroomState getState() { + return state; + } - public MushroomState getState() { - return state; - } + public boolean hasGenTemplate() { + return template != null; + } - public boolean hasGenTemplate() { - return template != null; - } + public WorldGenTemplate getGenTemplate() { + return template; + } - public WorldGenTemplate getGenTemplate() { - return template; - } + public int getMinExpDrop() { + return minExp; + } - public int getMinExpDrop() { - return minExp; - } + public int getMaxExpDrop() { + return maxExp; + } - public int getMaxExpDrop() { - return maxExp; - } + public int rollExperience() { + return Math.max(0, maxExp < minExp ? minExp : RANDOM.nextInt(maxExp - minExp + 1) + minExp); + } - public int getRequiredPower() { - return requiredPower; - } + public int getRequiredPower() { + return requiredPower; + } - public ItemStack getItem() { - return mmoitem.newBuilder().build(); - } + public ItemStack getItem() { + return mmoitem.newBuilder().build(); + } public boolean requirePowerToBreak() { return requirePowerToBreak; diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/block/RequirePowerToBreak.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/block/RequirePowerToBreak.java index cd8e1ebe..fa5e6631 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/block/RequirePowerToBreak.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/block/RequirePowerToBreak.java @@ -1,11 +1,19 @@ package net.Indyuce.mmoitems.stat.block; import net.Indyuce.mmoitems.stat.type.BooleanStat; -import net.Indyuce.mmoitems.stat.type.DoubleStat; import org.bukkit.Material; +/** + * Vanilla behaviour: + * When a player tries to break a block but doesn't + * have enough pickaxe power, the block is broken but + * does NOT drop anything. + *

+ * This option: + * When toggled on, the block simply won't break/drop + */ public class RequirePowerToBreak extends BooleanStat { public RequirePowerToBreak() { - super("REQUIRE_POWER_TO_BREAK", Material.BEDROCK, "Require Power to Break", new String[] { "If you need the required pickaxe", "power to break this custom block." }, new String[] { "block" }); + super("REQUIRE_POWER_TO_BREAK", Material.BEDROCK, "Require Power to Break", new String[]{"When enabled, the block will NOT break", "if the player doesn't have enough pickaxe", "power, unlike vanilla block behaviour."}, new String[]{"block"}); } } diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/MMOUtils.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/MMOUtils.java index fbe275b9..513fdbcf 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/MMOUtils.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/util/MMOUtils.java @@ -82,34 +82,32 @@ public class MMOUtils { } public static int getPickaxePower(Player player) { - ItemStack item = player.getInventory().getItemInMainHand(); - if (item != null && item.getType() != Material.AIR) { - NBTItem nbt = NBTItem.get(item); - if (nbt.hasType()) - return nbt.getInteger("MMOITEMS_PICKAXE_POWER"); - else { - switch (item.getType().name()) { - case "WOODEN_PICKAXE": - case "WOOD_PICKAXE": - return 5; - case "STONE_PICKAXE": - return 10; - case "GOLDEN_PICKAXE": - case "GOLD_PICKAXE": - return 15; - case "IRON_PICKAXE": - return 20; - case "DIAMOND_PICKAXE": - return 25; - case "NETHERITE_PICKAXE": - return 30; - default: - return 0; - } - } - } + final ItemStack item = player.getInventory().getItemInMainHand(); + if (item == null || item.getType() == Material.AIR) + return 0; - return 0; + final NBTItem nbt = NBTItem.get(item); + if (nbt.hasTag("MMOITEMS_PICKAXE_POWER")) + return nbt.getInteger("MMOITEMS_PICKAXE_POWER"); + + switch (item.getType().name()) { + case "WOODEN_PICKAXE": + case "WOOD_PICKAXE": + return 5; + case "STONE_PICKAXE": + return 10; + case "GOLDEN_PICKAXE": + case "GOLD_PICKAXE": + return 15; + case "IRON_PICKAXE": + return 20; + case "DIAMOND_PICKAXE": + return 25; + case "NETHERITE_PICKAXE": + return 30; + default: + return 0; + } } /** diff --git a/MMOItems-Dist/src/main/java/net/Indyuce/mmoitems/listener/CustomBlockListener.java b/MMOItems-Dist/src/main/java/net/Indyuce/mmoitems/listener/CustomBlockListener.java index 4772ae2f..15c6d960 100644 --- a/MMOItems-Dist/src/main/java/net/Indyuce/mmoitems/listener/CustomBlockListener.java +++ b/MMOItems-Dist/src/main/java/net/Indyuce/mmoitems/listener/CustomBlockListener.java @@ -1,10 +1,10 @@ package net.Indyuce.mmoitems.listener; -import net.Indyuce.mmoitems.MMOItems; -import net.Indyuce.mmoitems.util.MMOUtils; -import net.Indyuce.mmoitems.api.block.CustomBlock; import io.lumine.mythic.lib.MythicLib; import io.lumine.mythic.lib.api.item.NBTItem; +import net.Indyuce.mmoitems.MMOItems; +import net.Indyuce.mmoitems.api.block.CustomBlock; +import net.Indyuce.mmoitems.util.MMOUtils; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Material; @@ -22,113 +22,98 @@ import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.inventory.EquipmentSlot; import java.util.Optional; -import java.util.Random; import java.util.logging.Level; public class CustomBlockListener implements Listener { - private static final Random RANDOM = new Random(); - public CustomBlockListener() { - if (MMOItems.plugin.getLanguage().replaceMushroomDrops) - Bukkit.getPluginManager().registerEvents(new MushroomReplacer(), MMOItems.plugin); - } + public CustomBlockListener() { + if (MMOItems.plugin.getLanguage().replaceMushroomDrops) + Bukkit.getPluginManager().registerEvents(new MushroomReplacer(), MMOItems.plugin); + } - @EventHandler(ignoreCancelled = true) - public void a(BlockPhysicsEvent event) { - if (MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getChangedType())) { - event.setCancelled(true); - event.getBlock().getState().update(true, false); - } - } + @EventHandler(ignoreCancelled = true) + public void a(BlockPhysicsEvent event) { + if (MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getChangedType())) { + event.setCancelled(true); + event.getBlock().getState().update(true, false); + } + } - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void b(BlockBreakEvent event) { - Optional opt = MMOItems.plugin.getCustomBlocks().getFromBlock(event.getBlock().getBlockData()); - if (!opt.isPresent()) { - return; - } + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public void b(BlockBreakEvent event) { + if (event.getPlayer().getGameMode() == GameMode.CREATIVE) + return; - CustomBlock block = opt.get(); - - final int power = MMOUtils.getPickaxePower(event.getPlayer()); + Optional opt = MMOItems.plugin.getCustomBlocks().getFromBlock(event.getBlock().getBlockData()); + if (!opt.isPresent()) + return; - if ( block.requirePowerToBreak() && block.getRequiredPower()>0 ){ - if (power < block.getRequiredPower()) { - event.setCancelled(true); - return; - } - else if (power >= block.getRequiredPower()) { - event.setDropItems(false); - event.setExpToDrop(event.getPlayer().getGameMode() == GameMode.CREATIVE ? 0 - : MMOUtils.getPickaxePower(event.getPlayer()) >= block.getRequiredPower() - ? block.getMaxExpDrop() == 0 && block.getMinExpDrop() == 0 ? 0 - : RANDOM.nextInt((block.getMaxExpDrop() - block.getMinExpDrop()) + 1) + block.getMinExpDrop() - : 0); - return; - } - event.setCancelled(true); - return; - } + final CustomBlock block = opt.get(); + final int power = MMOUtils.getPickaxePower(event.getPlayer()); + if (power < block.getRequiredPower()) { + if (block.requirePowerToBreak()) { + event.setCancelled(true); + } else { + event.setDropItems(false); + event.setExpToDrop(0); + } + return; + } - event.setDropItems(false); - event.setExpToDrop(event.getPlayer().getGameMode() == GameMode.CREATIVE ? 0 - : MMOUtils.getPickaxePower(event.getPlayer()) >= block.getRequiredPower() - ? block.getMaxExpDrop() == 0 && block.getMinExpDrop() == 0 ? 0 - : RANDOM.nextInt((block.getMaxExpDrop() - block.getMinExpDrop()) + 1) + block.getMinExpDrop() - : 0); - } + event.setDropItems(false); + event.setExpToDrop(event.getPlayer().getGameMode() == GameMode.CREATIVE ? 0 : block.rollExperience()); + } + @Deprecated + private static int getPickaxePower(Player player) { + return MMOUtils.getPickaxePower(player); + } - @Deprecated - private static int getPickaxePower(Player player) { - return MMOUtils.getPickaxePower(player); - } + @EventHandler(priority = EventPriority.HIGHEST) + public void c(BlockPlaceEvent event) { + if (!event.isCancelled() && !isMushroomBlock(event.getBlockPlaced().getType())) { + NBTItem nbtItem = MythicLib.plugin.getVersion().getWrapper().getNBTItem(event.getItemInHand()); + int blockId = nbtItem.getInteger("MMOITEMS_BLOCK_ID"); + if (blockId > 160 || blockId < 1 || blockId == 54) // checks if block is a custom block + return; + if (MMOItems.plugin.getCustomBlocks().getBlock(blockId) == null) { + MMOItems.plugin.getLogger().log(Level.SEVERE, "Could not load custom block '" + blockId + "': " + " Block is not registered."); + MMOItems.plugin.getLogger().log(Level.SEVERE, "Try reloading the plugin to solve the issue."); + event.setCancelled(true); + return; + } - @EventHandler(priority = EventPriority.HIGHEST) - public void c(BlockPlaceEvent event) { - if (!event.isCancelled() && !isMushroomBlock(event.getBlockPlaced().getType())) { - NBTItem nbtItem = MythicLib.plugin.getVersion().getWrapper().getNBTItem(event.getItemInHand()); - int blockId = nbtItem.getInteger("MMOITEMS_BLOCK_ID"); - if (blockId > 160 || blockId < 1 || blockId == 54) // checks if block is a custom block - return; - if (MMOItems.plugin.getCustomBlocks().getBlock(blockId) == null) { - MMOItems.plugin.getLogger().log(Level.SEVERE, "Could not load custom block '" + blockId + "': " + " Block is not registered."); - MMOItems.plugin.getLogger().log(Level.SEVERE, "Try reloading the plugin to solve the issue."); - event.setCancelled(true); - return; - } + CustomBlock block = MMOItems.plugin.getCustomBlocks().getBlock(blockId); // stores the custom block + Block newBlock = event.getBlockPlaced(); + newBlock.setType(block.getState().getType(), false); + newBlock.setBlockData(block.getState().getBlockData(), false); - CustomBlock block = MMOItems.plugin.getCustomBlocks().getBlock(blockId); // stores the custom block - Block newBlock = event.getBlockPlaced(); - newBlock.setType(block.getState().getType(), false); - newBlock.setBlockData(block.getState().getBlockData(), false); + BlockPlaceEvent bpe = new BlockPlaceEvent(newBlock, newBlock.getState(), event.getBlockAgainst(), event.getItemInHand(), event.getPlayer(), true, + EquipmentSlot.HAND); + Bukkit.getServer().getPluginManager().callEvent(bpe); + } + } - BlockPlaceEvent bpe = new BlockPlaceEvent(newBlock, newBlock.getState(), event.getBlockAgainst(), event.getItemInHand(), event.getPlayer(), true, - EquipmentSlot.HAND); - Bukkit.getServer().getPluginManager().callEvent(bpe); - } - } + @EventHandler(ignoreCancelled = true) + public void d(BlockIgniteEvent event) { + if (event.getCause() == IgniteCause.LAVA || event.getCause() == IgniteCause.SPREAD) { + BlockFace[] faces = {BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.WEST, BlockFace.EAST}; + for (BlockFace face : faces) + if (MMOItems.plugin.getCustomBlocks().getFromBlock(event.getBlock().getRelative(face).getBlockData()).isPresent()) + event.setCancelled(true); + } + } - @EventHandler(ignoreCancelled = true) - public void d(BlockIgniteEvent event) { - if (event.getCause() == IgniteCause.LAVA || event.getCause() == IgniteCause.SPREAD) { - BlockFace[] faces = { BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.WEST, BlockFace.EAST }; - for (BlockFace face : faces) - if (MMOItems.plugin.getCustomBlocks().getFromBlock(event.getBlock().getRelative(face).getBlockData()).isPresent()) - event.setCancelled(true); - } - } + private boolean isMushroomBlock(Material material) { + return (material == Material.BROWN_MUSHROOM_BLOCK || material == Material.MUSHROOM_STEM || material == Material.RED_MUSHROOM_BLOCK); + } - private boolean isMushroomBlock(Material material) { - return (material == Material.BROWN_MUSHROOM_BLOCK || material == Material.MUSHROOM_STEM || material == Material.RED_MUSHROOM_BLOCK); - } - - public static class MushroomReplacer implements Listener { - @EventHandler(ignoreCancelled = true) - public void d(BlockBreakEvent event) { - if (MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getBlock().getType()) - && MMOItems.plugin.getDropTables().hasSilkTouchTool(event.getPlayer())) - event.setDropItems(false); - } - } + public static class MushroomReplacer implements Listener { + @EventHandler(ignoreCancelled = true) + public void d(BlockBreakEvent event) { + if (MMOItems.plugin.getCustomBlocks().isMushroomBlock(event.getBlock().getType()) + && MMOItems.plugin.getDropTables().hasSilkTouchTool(event.getPlayer())) + event.setDropItems(false); + } + } }