Remove more code duplication.

This commit is contained in:
GJ 2013-06-12 15:41:26 -04:00
parent b15f1dde28
commit db637f0bc9
2 changed files with 11 additions and 27 deletions

View File

@ -1,7 +1,5 @@
package com.gmail.nossr50.listeners; package com.gmail.nossr50.listeners;
import java.util.List;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.block.Furnace; import org.bukkit.block.Furnace;
@ -19,7 +17,6 @@ import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
@ -77,20 +74,13 @@ public class InventoryListener implements Listener {
return; return;
} }
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
if (metadata.isEmpty()) {
return;
}
ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting(); ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting();
if (smelting == null || !ItemUtils.isSmeltable(smelting)) { if (smelting == null || !ItemUtils.isSmeltable(smelting)) {
return; return;
} }
// We can make this assumption because we (should) be the only ones using this exact metadata Player player = Misc.getPlayerFromFurnace(furnaceBlock);
Player player = plugin.getServer().getPlayer(metadata.get(0).asString());
if (Misc.isNPCEntity(player) || !Permissions.fuelEfficiency(player)) { if (Misc.isNPCEntity(player) || !Permissions.fuelEfficiency(player)) {
return; return;
@ -108,20 +98,13 @@ public class InventoryListener implements Listener {
return; return;
} }
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
if (metadata.isEmpty()) {
return;
}
ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting(); ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting();
if (smelting == null || !ItemUtils.isSmeltable(smelting)) { if (smelting == null || !ItemUtils.isSmeltable(smelting)) {
return; return;
} }
// We can make this assumption because we (should) be the only ones using this exact metadata Player player = Misc.getPlayerFromFurnace(furnaceBlock);
Player player = plugin.getServer().getPlayer(metadata.get(0).asString());
if (Misc.isNPCEntity(player) || !Permissions.skillEnabled(player, SkillType.SMELTING)) { if (Misc.isNPCEntity(player) || !Permissions.skillEnabled(player, SkillType.SMELTING)) {
return; return;
@ -139,20 +122,13 @@ public class InventoryListener implements Listener {
return; return;
} }
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
if (metadata.isEmpty()) {
return;
}
ItemStack result = ((Furnace) furnaceState).getInventory().getResult(); ItemStack result = ((Furnace) furnaceState).getInventory().getResult();
if (result == null || !ItemUtils.isSmelted(result)) { if (result == null || !ItemUtils.isSmelted(result)) {
return; return;
} }
// We can make this assumption because we (should) be the only ones using this exact metadata Player player = Misc.getPlayerFromFurnace(furnaceBlock);
Player player = plugin.getServer().getPlayer(metadata.get(0).asString());
if (Misc.isNPCEntity(player)) { if (Misc.isNPCEntity(player)) {
return; return;

View File

@ -1,6 +1,7 @@
package com.gmail.nossr50.util; package com.gmail.nossr50.util;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Random; import java.util.Random;
import org.bukkit.Chunk; import org.bukkit.Chunk;
@ -15,6 +16,7 @@ import org.bukkit.event.inventory.InventoryEvent;
import org.bukkit.inventory.FurnaceInventory; import org.bukkit.inventory.FurnaceInventory;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.MetadataValue;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
@ -225,6 +227,12 @@ public final class Misc {
return furnace.getBlock(); return furnace.getBlock();
} }
public static Player getPlayerFromFurnace(Block furnaceBlock) {
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
return metadata.isEmpty() ? null : mcMMO.p.getServer().getPlayerExact(metadata.get(0).asString());
}
public static Random getRandom() { public static Random getRandom() {
return random; return random;
} }