From 96f657f6e7147f9c0e6531952d9b8196e7fe6fb6 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Mon, 5 Apr 2021 13:16:31 +0200 Subject: [PATCH] Small performance improvements --- CHANGELOG.md | 5 +++-- .../implementation/listeners/BlockListener.java | 16 ++++++++++++---- .../listeners/TalismanListener.java | 11 +++++++++-- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5f059704..ffcf852f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,10 +38,11 @@ * Block Placers can now place down cake #### Changes +* Changed item order in guide for the Villager Rune and Nether Goo (All runes are now grouped together) +* Ancient Pedestals can now be found under "Magical Gadgets" * Removed all functionality from the old Automated Crafting Chamber * Changed Cargo Motor texture -* Ancient Pedestals can now be found under "Magical Gadgets" -* Changed item order in guide for the Villager Rune and Nether Goo (All runes are now grouped together) +* Small performance improvements #### Fixes * Fixed #1161 diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java index 79e71dd01..909e44d93 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java @@ -21,6 +21,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction; import io.github.thebusybiscuit.slimefun4.core.attributes.NotPlaceable; @@ -238,7 +239,7 @@ public class BlockListener implements Listener { if (!dummyEvent.isCancelled() && dummyEvent.isDropItems()) { for (ItemStack drop : drops) { - if (drop != null && drop.getType() != Material.AIR) { + if (drop != null && !drop.getType().isAir()) { blockAbove.getWorld().dropItemNaturally(blockAbove.getLocation(), drop); } } @@ -251,10 +252,17 @@ public class BlockListener implements Listener { private int getBonusDropsWithFortune(@Nullable ItemStack item, @Nonnull Block b) { int amount = 1; - if (item != null) { - int fortuneLevel = item.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS); + if (item != null && !item.getType().isAir() && item.hasItemMeta()) { + /* + * Small performance optimization: + * ItemStack#getEnchantmentLevel() calls ItemStack#getItemMeta(), so if + * we are handling more than one Enchantment, we should access the ItemMeta + * directly and re use it. + */ + ItemMeta meta = item.getItemMeta(); + int fortuneLevel = meta.getEnchantLevel(Enchantment.LOOT_BONUS_BLOCKS); - if (fortuneLevel > 0 && !item.containsEnchantment(Enchantment.SILK_TOUCH)) { + if (fortuneLevel > 0 && !meta.hasEnchant(Enchantment.SILK_TOUCH)) { Random random = ThreadLocalRandom.current(); amount = Math.max(1, random.nextInt(fortuneLevel + 2) - 1); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TalismanListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TalismanListener.java index ce21c0f35..9cd331b05 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TalismanListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TalismanListener.java @@ -278,7 +278,14 @@ public class TalismanListener implements Listener { ItemStack item = e.getPlayer().getInventory().getItemInMainHand(); // We are going to ignore Silk Touch here - if (item.getType() != Material.AIR && item.getAmount() > 0 && !item.containsEnchantment(Enchantment.SILK_TOUCH)) { + if (item.getType() != Material.AIR && item.getAmount() > 0) { + ItemMeta meta = item.getItemMeta(); + + // Ignore Silk Touch Enchantment + if (meta.hasEnchant(Enchantment.SILK_TOUCH)) { + return; + } + Material type = e.getBlockState().getType(); // We only want to double ores @@ -286,7 +293,7 @@ public class TalismanListener implements Listener { Collection drops = e.getItems(); if (Talisman.trigger(e, SlimefunItems.TALISMAN_MINER, false)) { - int dropAmount = getAmountWithFortune(type, item.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS)); + int dropAmount = getAmountWithFortune(type, meta.getEnchantLevel(Enchantment.LOOT_BONUS_BLOCKS)); // Keep track of whether we actually doubled the drops or not boolean doubledDrops = false;