From caa1b0d14c8d9333607bd6cb815a696b15ad925d Mon Sep 17 00:00:00 2001 From: GJ Date: Thu, 14 Feb 2013 19:02:50 -0500 Subject: [PATCH] Duplicate code is bad, m'kay? --- .../nossr50/listeners/InventoryListener.java | 28 +----------- .../nossr50/listeners/PlayerListener.java | 32 +------------ .../nossr50/skills/utilities/SkillTools.java | 45 +++++++++++-------- 3 files changed, 30 insertions(+), 75 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java b/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java index 38679d0fe..ecbad99e5 100644 --- a/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java @@ -1,11 +1,8 @@ package com.gmail.nossr50.listeners; -import java.util.List; - import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.Furnace; -import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -19,11 +16,11 @@ import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.FurnaceInventory; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.McMMOPlayer; import com.gmail.nossr50.skills.smelting.SmeltingManager; +import com.gmail.nossr50.skills.utilities.SkillTools; import com.gmail.nossr50.util.ItemChecks; import com.gmail.nossr50.util.Users; @@ -137,27 +134,6 @@ public class InventoryListener implements Listener{ @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onInventoryClickEvent(InventoryClickEvent event) { ItemStack item = event.getCurrentItem(); - - if (item != null && item.containsEnchantment(Enchantment.DIG_SPEED)) { - ItemMeta itemMeta = item.getItemMeta(); - - if (itemMeta.hasLore()) { - int efficiencyLevel = item.getEnchantmentLevel(Enchantment.DIG_SPEED); - List itemLore = itemMeta.getLore(); - - if (itemLore.remove("mcMMO Ability Tool")) { - if (efficiencyLevel <= 5) { - itemMeta.removeEnchant(Enchantment.DIG_SPEED); - } - else { - itemMeta.addEnchant(Enchantment.DIG_SPEED, efficiencyLevel - 5, true); - } - - itemMeta.setLore(itemLore); - item.setItemMeta(itemMeta); - return; - } - } - } + SkillTools.removeAbilityBuff(item); //Remove enchants if they try to move them in an inventory } } diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index 85c4e3cb4..0e7c4882e 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -1,10 +1,7 @@ package com.gmail.nossr50.listeners; -import java.util.List; - import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; import org.bukkit.entity.Item; import org.bukkit.entity.LivingEntity; @@ -24,7 +21,6 @@ import org.bukkit.event.player.PlayerPickupItemEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.chat.ChatManager; @@ -49,8 +45,8 @@ import com.gmail.nossr50.skills.utilities.SkillType; import com.gmail.nossr50.util.BlockChecks; import com.gmail.nossr50.util.Hardcore; import com.gmail.nossr50.util.ItemChecks; -import com.gmail.nossr50.util.Motd; import com.gmail.nossr50.util.Misc; +import com.gmail.nossr50.util.Motd; import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.StringUtils; import com.gmail.nossr50.util.Users; @@ -79,31 +75,7 @@ public class PlayerListener implements Listener { if (playerProfile.getAbilityMode(AbilityType.GIGA_DRILL_BREAKER) || playerProfile.getAbilityMode(AbilityType.SUPER_BREAKER)) { for (ItemStack item : event.getDrops()) { - if (item == null || item.getType() == Material.AIR ) { - continue; - } - - if (item.containsEnchantment(Enchantment.DIG_SPEED)) { - ItemMeta itemMeta = item.getItemMeta(); - - if (itemMeta.hasLore()) { - int efficiencyLevel = item.getEnchantmentLevel(Enchantment.DIG_SPEED); - List itemLore = itemMeta.getLore(); - - if (itemLore.remove("mcMMO Ability Tool")) { - if (efficiencyLevel <= 5) { - itemMeta.removeEnchant(Enchantment.DIG_SPEED); - } - else { - itemMeta.addEnchant(Enchantment.DIG_SPEED, efficiencyLevel - 5, true); - } - - itemMeta.setLore(itemLore); - item.setItemMeta(itemMeta); - return; - } - } - } + SkillTools.removeAbilityBuff(item); } } } diff --git a/src/main/java/com/gmail/nossr50/skills/utilities/SkillTools.java b/src/main/java/com/gmail/nossr50/skills/utilities/SkillTools.java index e4ad6186e..8a3b882ad 100644 --- a/src/main/java/com/gmail/nossr50/skills/utilities/SkillTools.java +++ b/src/main/java/com/gmail/nossr50/skills/utilities/SkillTools.java @@ -545,32 +545,39 @@ public class SkillTools { public static void handleAbilitySpeedDecrease(Player player) { PlayerInventory playerInventory = player.getInventory(); - for (ItemStack item : playerInventory.getContents()) { - if (item == null || item.getType() == Material.AIR ) { - continue; - } + for (int i = 0; i < playerInventory.getContents().length; i++) { + ItemStack item = playerInventory.getItem(i); + playerInventory.setItem(i, removeAbilityBuff(item)); + } + } - if (item.containsEnchantment(Enchantment.DIG_SPEED)) { - ItemMeta itemMeta = item.getItemMeta(); + public static ItemStack removeAbilityBuff(ItemStack item) { + if (item == null || item.getType() == Material.AIR ) { + return item; + } - if (itemMeta.hasLore()) { + if (item.containsEnchantment(Enchantment.DIG_SPEED)) { + ItemMeta itemMeta = item.getItemMeta(); + + if (itemMeta.hasLore()) { + List itemLore = itemMeta.getLore(); + + if (itemLore.remove("mcMMO Ability Tool")) { int efficiencyLevel = item.getEnchantmentLevel(Enchantment.DIG_SPEED); - List itemLore = itemMeta.getLore(); - if (itemLore.remove("mcMMO Ability Tool")) { - if (efficiencyLevel <= 5) { - itemMeta.removeEnchant(Enchantment.DIG_SPEED); - } - else { - itemMeta.addEnchant(Enchantment.DIG_SPEED, efficiencyLevel - 5, true); - } - - itemMeta.setLore(itemLore); - item.setItemMeta(itemMeta); - return; + if (efficiencyLevel <= 5) { + itemMeta.removeEnchant(Enchantment.DIG_SPEED); } + else { + itemMeta.addEnchant(Enchantment.DIG_SPEED, efficiencyLevel - 5, true); + } + + itemMeta.setLore(itemLore); + item.setItemMeta(itemMeta); } } } + + return item; } }