Fix errors in furnace listener.

Bad GJ!
This commit is contained in:
md-5 2013-03-09 08:12:34 +11:00
parent a07f14e326
commit 1063ad5c68

View File

@ -1,157 +1,157 @@
package com.gmail.nossr50.listeners; package com.gmail.nossr50.listeners;
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;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.inventory.FurnaceBurnEvent; import org.bukkit.event.inventory.FurnaceBurnEvent;
import org.bukkit.event.inventory.FurnaceExtractEvent; import org.bukkit.event.inventory.FurnaceExtractEvent;
import org.bukkit.event.inventory.FurnaceSmeltEvent; import org.bukkit.event.inventory.FurnaceSmeltEvent;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryOpenEvent;
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.FixedMetadataValue; import org.bukkit.metadata.FixedMetadataValue;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.skills.smelting.SmeltingManager; import com.gmail.nossr50.skills.smelting.SmeltingManager;
import com.gmail.nossr50.util.ItemUtils; import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.skills.SkillUtils; import com.gmail.nossr50.util.skills.SkillUtils;
public class InventoryListener implements Listener { public class InventoryListener implements Listener {
private final mcMMO plugin; private final mcMMO plugin;
public InventoryListener(final mcMMO plugin) { public InventoryListener(final mcMMO plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onInventoryOpen(InventoryOpenEvent event) { public void onInventoryOpen(InventoryOpenEvent event) {
HumanEntity player = event.getPlayer(); HumanEntity player = event.getPlayer();
if (Misc.isNPCEntity(player)) { if (Misc.isNPCEntity(player)) {
return; return;
} }
Inventory inventory = event.getInventory(); Inventory inventory = event.getInventory();
if (inventory instanceof FurnaceInventory) { if (inventory instanceof FurnaceInventory) {
Furnace furnace = (Furnace) inventory.getHolder(); Furnace furnace = (Furnace) inventory.getHolder();
if (furnace == null) { if (furnace == null) {
return; return;
} }
if (furnace.getBurnTime() == 0) { if (furnace.getBurnTime() == 0) {
Block furnaceBlock = furnace.getBlock(); Block furnaceBlock = furnace.getBlock();
if (!furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) { if (!furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) {
furnaceBlock.setMetadata(mcMMO.furnaceMetadataKey, new FixedMetadataValue(plugin, player.getName())); furnaceBlock.setMetadata(mcMMO.furnaceMetadataKey, new FixedMetadataValue(plugin, player.getName()));
} }
} }
} }
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onInventoryClose(InventoryCloseEvent event) { public void onInventoryClose(InventoryCloseEvent event) {
HumanEntity player = event.getPlayer(); HumanEntity player = event.getPlayer();
if (Misc.isNPCEntity(player)) { if (Misc.isNPCEntity(player)) {
return; return;
} }
Inventory inventory = event.getInventory(); Inventory inventory = event.getInventory();
if (inventory instanceof FurnaceInventory) { if (inventory instanceof FurnaceInventory) {
Furnace furnace = (Furnace) inventory.getHolder(); Furnace furnace = (Furnace) inventory.getHolder();
if (furnace == null) { if (furnace == null) {
return; return;
} }
if (furnace.getBurnTime() == 0) { if (furnace.getBurnTime() == 0) {
Block furnaceBlock = furnace.getBlock(); Block furnaceBlock = furnace.getBlock();
if (furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) { if (furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) {
furnaceBlock.removeMetadata(mcMMO.furnaceMetadataKey, plugin); furnaceBlock.removeMetadata(mcMMO.furnaceMetadataKey, plugin);
} }
} }
} }
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onFurnaceBurnEvent(FurnaceBurnEvent event) { public void onFurnaceBurnEvent(FurnaceBurnEvent event) {
Block furnaceBlock = event.getBlock(); Block furnaceBlock = event.getBlock();
BlockState furnaceState = furnaceBlock.getState(); BlockState furnaceState = furnaceBlock.getState();
if (furnaceState instanceof Furnace) { if (furnaceState instanceof Furnace) {
ItemStack smelting = ((Furnace) furnaceBlock).getInventory().getSmelting(); ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting();
if (furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey) && smelting != null && ItemUtils.isSmeltable(smelting)) { if (furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey) && smelting != null && ItemUtils.isSmeltable(smelting)) {
// We can make this assumption because we (should) be the only ones using this exact metadata // We can make this assumption because we (should) be the only ones using this exact metadata
Player player = plugin.getServer().getPlayer(furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey).get(0).asString()); Player player = plugin.getServer().getPlayer(furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey).get(0).asString());
if (Misc.isNPCEntity(player) || !Permissions.fuelEfficiency(player)) { if (Misc.isNPCEntity(player) || !Permissions.fuelEfficiency(player)) {
return; return;
} }
event.setBurnTime(UserManager.getPlayer(player).getSmeltingManager().fuelEfficiency(event.getBurnTime())); event.setBurnTime(UserManager.getPlayer(player).getSmeltingManager().fuelEfficiency(event.getBurnTime()));
} }
} }
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onFurnaceSmeltEvent(FurnaceSmeltEvent event) { public void onFurnaceSmeltEvent(FurnaceSmeltEvent event) {
Block furnaceBlock = event.getBlock(); Block furnaceBlock = event.getBlock();
BlockState furnaceState = furnaceBlock.getState(); BlockState furnaceState = furnaceBlock.getState();
if (furnaceState instanceof Furnace) { if (furnaceState instanceof Furnace) {
ItemStack smelting = ((Furnace) furnaceBlock).getInventory().getSmelting(); ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting();
if (furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey) && smelting != null && ItemUtils.isSmeltable(smelting)) { if (furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey) && smelting != null && ItemUtils.isSmeltable(smelting)) {
// We can make this assumption because we (should) be the only ones using this exact metadata // We can make this assumption because we (should) be the only ones using this exact metadata
Player player = plugin.getServer().getPlayer(furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey).get(0).asString()); Player player = plugin.getServer().getPlayer(furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey).get(0).asString());
if (Misc.isNPCEntity(player) || !Permissions.skillEnabled(player, SkillType.SMELTING)) { if (Misc.isNPCEntity(player) || !Permissions.skillEnabled(player, SkillType.SMELTING)) {
return; return;
} }
event.setResult(UserManager.getPlayer(player).getSmeltingManager().smeltProcessing(event.getSource().getType(), event.getResult())); event.setResult(UserManager.getPlayer(player).getSmeltingManager().smeltProcessing(event.getSource().getType(), event.getResult()));
} }
} }
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onFurnaceExtractEvent(FurnaceExtractEvent event) { public void onFurnaceExtractEvent(FurnaceExtractEvent event) {
BlockState furnaceBlock = event.getBlock().getState(); BlockState furnaceBlock = event.getBlock().getState();
if (furnaceBlock instanceof Furnace) { if (furnaceBlock instanceof Furnace) {
ItemStack result = ((Furnace) furnaceBlock).getInventory().getResult(); ItemStack result = ((Furnace) furnaceState).getInventory().getResult();
if (furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey) && result != null && ItemUtils.isSmelted(result)) { if (furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey) && result != null && ItemUtils.isSmelted(result)) {
// We can make this assumption because we (should) be the only ones using this exact metadata // We can make this assumption because we (should) be the only ones using this exact metadata
Player player = plugin.getServer().getPlayer(furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey).get(0).asString()); Player player = plugin.getServer().getPlayer(furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey).get(0).asString());
SmeltingManager smeltingManager = UserManager.getPlayer(player).getSmeltingManager(); SmeltingManager smeltingManager = UserManager.getPlayer(player).getSmeltingManager();
if (smeltingManager.canUseVanillaXpBoost()) { if (smeltingManager.canUseVanillaXpBoost()) {
event.setExpToDrop(smeltingManager.vanillaXPBoost(event.getExpToDrop())); event.setExpToDrop(smeltingManager.vanillaXPBoost(event.getExpToDrop()));
} }
} }
} }
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryClickEvent(InventoryClickEvent event) { public void onInventoryClickEvent(InventoryClickEvent event) {
SkillUtils.removeAbilityBuff(event.getCurrentItem()); SkillUtils.removeAbilityBuff(event.getCurrentItem());
} }
} }