From db637f0bc9bec4f37c14140cb7daacb6f8ac87a3 Mon Sep 17 00:00:00 2001 From: GJ Date: Wed, 12 Jun 2013 15:41:26 -0400 Subject: [PATCH] Remove more code duplication. --- .../nossr50/listeners/InventoryListener.java | 30 ++----------------- .../java/com/gmail/nossr50/util/Misc.java | 8 +++++ 2 files changed, 11 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java b/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java index 2f414f4c6..4dd5cd300 100644 --- a/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java @@ -1,7 +1,5 @@ package com.gmail.nossr50.listeners; -import java.util.List; - import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.Furnace; @@ -19,7 +17,6 @@ import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.metadata.FixedMetadataValue; -import org.bukkit.metadata.MetadataValue; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.skills.SkillType; @@ -77,20 +74,13 @@ public class InventoryListener implements Listener { return; } - List metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey); - - if (metadata.isEmpty()) { - return; - } - ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting(); if (smelting == null || !ItemUtils.isSmeltable(smelting)) { return; } - // We can make this assumption because we (should) be the only ones using this exact metadata - Player player = plugin.getServer().getPlayer(metadata.get(0).asString()); + Player player = Misc.getPlayerFromFurnace(furnaceBlock); if (Misc.isNPCEntity(player) || !Permissions.fuelEfficiency(player)) { return; @@ -108,20 +98,13 @@ public class InventoryListener implements Listener { return; } - List metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey); - - if (metadata.isEmpty()) { - return; - } - ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting(); if (smelting == null || !ItemUtils.isSmeltable(smelting)) { return; } - // We can make this assumption because we (should) be the only ones using this exact metadata - Player player = plugin.getServer().getPlayer(metadata.get(0).asString()); + Player player = Misc.getPlayerFromFurnace(furnaceBlock); if (Misc.isNPCEntity(player) || !Permissions.skillEnabled(player, SkillType.SMELTING)) { return; @@ -139,20 +122,13 @@ public class InventoryListener implements Listener { return; } - List metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey); - - if (metadata.isEmpty()) { - return; - } - ItemStack result = ((Furnace) furnaceState).getInventory().getResult(); if (result == null || !ItemUtils.isSmelted(result)) { return; } - // We can make this assumption because we (should) be the only ones using this exact metadata - Player player = plugin.getServer().getPlayer(metadata.get(0).asString()); + Player player = Misc.getPlayerFromFurnace(furnaceBlock); if (Misc.isNPCEntity(player)) { return; diff --git a/src/main/java/com/gmail/nossr50/util/Misc.java b/src/main/java/com/gmail/nossr50/util/Misc.java index f0f672c56..4a9c03063 100644 --- a/src/main/java/com/gmail/nossr50/util/Misc.java +++ b/src/main/java/com/gmail/nossr50/util/Misc.java @@ -1,6 +1,7 @@ package com.gmail.nossr50.util; import java.util.Collection; +import java.util.List; import java.util.Random; import org.bukkit.Chunk; @@ -15,6 +16,7 @@ import org.bukkit.event.inventory.InventoryEvent; import org.bukkit.inventory.FurnaceInventory; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import org.bukkit.metadata.MetadataValue; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.player.McMMOPlayer; @@ -225,6 +227,12 @@ public final class Misc { return furnace.getBlock(); } + public static Player getPlayerFromFurnace(Block furnaceBlock) { + List metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey); + + return metadata.isEmpty() ? null : mcMMO.p.getServer().getPlayerExact(metadata.get(0).asString()); + } + public static Random getRandom() { return random; }