From 3a78e1b487d8c8d166b0c061910095ebb999742a Mon Sep 17 00:00:00 2001 From: GJ Date: Wed, 12 Jun 2013 15:56:42 -0400 Subject: [PATCH] More code duplication removal. --- .../nossr50/listeners/InventoryListener.java | 43 ++++--------------- .../skills/smelting/SmeltingManager.java | 6 +-- .../com/gmail/nossr50/util/ItemUtils.java | 8 ++++ .../java/com/gmail/nossr50/util/Misc.java | 27 +++++++++++- 4 files changed, 44 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java b/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java index 4dd5cd300..9b782cf32 100644 --- a/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java @@ -1,8 +1,6 @@ package com.gmail.nossr50.listeners; -import org.bukkit.block.Block; -import org.bukkit.block.BlockState; -import org.bukkit.block.Furnace; +import org.bukkit.block.Block; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -21,7 +19,6 @@ import org.bukkit.metadata.FixedMetadataValue; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.runnables.PlayerUpdateInventoryTask; -import com.gmail.nossr50.skills.smelting.SmeltingManager; import com.gmail.nossr50.util.ItemUtils; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; @@ -68,15 +65,9 @@ public class InventoryListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onFurnaceBurnEvent(FurnaceBurnEvent event) { Block furnaceBlock = event.getBlock(); - BlockState furnaceState = furnaceBlock.getState(); + ItemStack smelting = Misc.getSmeltingFromFurnace(furnaceBlock); - if (!(furnaceState instanceof Furnace)) { - return; - } - - ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting(); - - if (smelting == null || !ItemUtils.isSmeltable(smelting)) { + if (!ItemUtils.isSmeltable(smelting)) { return; } @@ -92,15 +83,9 @@ public class InventoryListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onFurnaceSmeltEvent(FurnaceSmeltEvent event) { Block furnaceBlock = event.getBlock(); - BlockState furnaceState = furnaceBlock.getState(); + ItemStack smelting = Misc.getSmeltingFromFurnace(furnaceBlock); - if (!(furnaceState instanceof Furnace)) { - return; - } - - ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting(); - - if (smelting == null || !ItemUtils.isSmeltable(smelting)) { + if (!ItemUtils.isSmeltable(smelting)) { return; } @@ -116,29 +101,19 @@ public class InventoryListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onFurnaceExtractEvent(FurnaceExtractEvent event) { Block furnaceBlock = event.getBlock(); - BlockState furnaceState = furnaceBlock.getState(); + ItemStack result = Misc.getResultFromFurnace(furnaceBlock); - if (!(furnaceState instanceof Furnace)) { - return; - } - - ItemStack result = ((Furnace) furnaceState).getInventory().getResult(); - - if (result == null || !ItemUtils.isSmelted(result)) { + if (!ItemUtils.isSmelted(result)) { return; } Player player = Misc.getPlayerFromFurnace(furnaceBlock); - if (Misc.isNPCEntity(player)) { + if (Misc.isNPCEntity(player) || !Permissions.vanillaXpBoost(player, SkillType.SMELTING)) { return; } - SmeltingManager smeltingManager = UserManager.getPlayer(player).getSmeltingManager(); - - if (smeltingManager.canUseVanillaXpBoost()) { - event.setExpToDrop(smeltingManager.vanillaXPBoost(event.getExpToDrop())); - } + event.setExpToDrop(UserManager.getPlayer(player).getSmeltingManager().vanillaXPBoost(event.getExpToDrop())); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) diff --git a/src/main/java/com/gmail/nossr50/skills/smelting/SmeltingManager.java b/src/main/java/com/gmail/nossr50/skills/smelting/SmeltingManager.java index d3b91dad6..7f74ae280 100644 --- a/src/main/java/com/gmail/nossr50/skills/smelting/SmeltingManager.java +++ b/src/main/java/com/gmail/nossr50/skills/smelting/SmeltingManager.java @@ -28,10 +28,6 @@ public class SmeltingManager extends SkillManager { return getSkillLevel() >= Smelting.fluxMiningUnlockLevel && BlockUtils.affectedByFluxMining(blockState) && Permissions.fluxMining(getPlayer()) && !mcMMO.getPlaceStore().isTrue(blockState); } - public boolean canUseVanillaXpBoost() { - return getSkillLevel() >= Smelting.Tier.ONE.getLevel() && Permissions.vanillaXpBoost(getPlayer(), skill); - } - /** * Process the Flux Mining ability. * @@ -122,6 +118,6 @@ public class SmeltingManager extends SkillManager { } } - return 0; + return 1; } } diff --git a/src/main/java/com/gmail/nossr50/util/ItemUtils.java b/src/main/java/com/gmail/nossr50/util/ItemUtils.java index fdc62e045..217692658 100644 --- a/src/main/java/com/gmail/nossr50/util/ItemUtils.java +++ b/src/main/java/com/gmail/nossr50/util/ItemUtils.java @@ -461,6 +461,10 @@ public class ItemUtils { } public static boolean isSmeltable(ItemStack item) { + if (item == null) { + return false; + } + switch (item.getType()) { case COAL_ORE: case DIAMOND_ORE: @@ -479,6 +483,10 @@ public class ItemUtils { } public static boolean isSmelted(ItemStack item) { + if (item == null) { + return false; + } + switch (item.getType()) { case COAL: case DIAMOND: diff --git a/src/main/java/com/gmail/nossr50/util/Misc.java b/src/main/java/com/gmail/nossr50/util/Misc.java index 4a9c03063..6602de8bc 100644 --- a/src/main/java/com/gmail/nossr50/util/Misc.java +++ b/src/main/java/com/gmail/nossr50/util/Misc.java @@ -8,6 +8,7 @@ import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.block.Furnace; import org.bukkit.entity.Entity; import org.bukkit.entity.HumanEntity; @@ -230,7 +231,31 @@ public final class Misc { public static Player getPlayerFromFurnace(Block furnaceBlock) { List metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey); - return metadata.isEmpty() ? null : mcMMO.p.getServer().getPlayerExact(metadata.get(0).asString()); + if (metadata.isEmpty()) { + return null; + } + + return mcMMO.p.getServer().getPlayerExact(metadata.get(0).asString()); + } + + public static ItemStack getSmeltingFromFurnace(Block furnaceBlock) { + BlockState furnaceState = furnaceBlock.getState(); + + if (!(furnaceState instanceof Furnace)) { + return null; + } + + return ((Furnace) furnaceState).getInventory().getSmelting(); + } + + public static ItemStack getResultFromFurnace(Block furnaceBlock) { + BlockState furnaceState = furnaceBlock.getState(); + + if (!(furnaceState instanceof Furnace)) { + return null; + } + + return ((Furnace) furnaceState).getInventory().getResult(); } public static Random getRandom() {