Clean up our inventory event handling.

This commit is contained in:
GJ 2014-01-03 10:41:38 -05:00
parent e0358d42d6
commit 05b21eae5f
2 changed files with 52 additions and 68 deletions

View File

@ -1,6 +1,10 @@
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.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;
@ -13,7 +17,10 @@ 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.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.skills.SecondaryAbility; import com.gmail.nossr50.datatypes.skills.SecondaryAbility;
@ -34,44 +41,49 @@ public class InventoryListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryOpen(InventoryOpenEvent event) { public void onInventoryOpen(InventoryOpenEvent event) {
Block furnaceBlock = processInventoryOpenorCloseEvent(event.getInventory());
if (furnaceBlock == null || furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) {
return;
}
HumanEntity player = event.getPlayer(); HumanEntity player = event.getPlayer();
if (Misc.isNPCEntity(player)) { if (Misc.isNPCEntity(player)) {
return; return;
} }
Block furnaceBlock = Misc.processInventoryOpenorCloseEvent(event); furnaceBlock.setMetadata(mcMMO.furnaceMetadataKey, UserManager.getPlayer((Player) player).getPlayerMetadata());
if (furnaceBlock != null && !furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) {
furnaceBlock.setMetadata(mcMMO.furnaceMetadataKey, UserManager.getPlayer((Player) player).getPlayerMetadata());
}
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryClose(InventoryCloseEvent event) { public void onInventoryClose(InventoryCloseEvent event) {
Block furnaceBlock = processInventoryOpenorCloseEvent(event.getInventory());
if (furnaceBlock == null || furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) {
return;
}
HumanEntity player = event.getPlayer(); HumanEntity player = event.getPlayer();
if (Misc.isNPCEntity(player)) { if (Misc.isNPCEntity(player)) {
return; return;
} }
Block furnaceBlock = Misc.processInventoryOpenorCloseEvent(event); furnaceBlock.removeMetadata(mcMMO.furnaceMetadataKey, plugin);
if (furnaceBlock != null && furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) {
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();
ItemStack smelting = Misc.getSmeltingFromFurnace(furnaceBlock); BlockState furnaceState = furnaceBlock.getState();
ItemStack smelting = furnaceState instanceof Furnace ? ((Furnace) furnaceState).getInventory().getSmelting() : null;
if (!ItemUtils.isSmeltable(smelting)) { if (!ItemUtils.isSmeltable(smelting)) {
return; return;
} }
Player player = Misc.getPlayerFromFurnace(furnaceBlock); Player player = getPlayerFromFurnace(furnaceBlock);
if (Misc.isNPCEntity(player) || !Permissions.secondaryAbilityEnabled(player, SecondaryAbility.FUEL_EFFICIENCY)) { if (Misc.isNPCEntity(player) || !Permissions.secondaryAbilityEnabled(player, SecondaryAbility.FUEL_EFFICIENCY)) {
return; return;
@ -89,7 +101,7 @@ public class InventoryListener implements Listener {
return; return;
} }
Player player = Misc.getPlayerFromFurnace(furnaceBlock); Player player = getPlayerFromFurnace(furnaceBlock);
if (Misc.isNPCEntity(player) || !SkillType.SMELTING.getPermissions(player)) { if (Misc.isNPCEntity(player) || !SkillType.SMELTING.getPermissions(player)) {
return; return;
@ -101,13 +113,14 @@ public class InventoryListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onFurnaceExtractEvent(FurnaceExtractEvent event) { public void onFurnaceExtractEvent(FurnaceExtractEvent event) {
Block furnaceBlock = event.getBlock(); Block furnaceBlock = event.getBlock();
ItemStack result = Misc.getResultFromFurnace(furnaceBlock); BlockState furnaceState = furnaceBlock.getState();
ItemStack result = furnaceState instanceof Furnace ? ((Furnace) furnaceState).getInventory().getResult() : null;
if (!ItemUtils.isSmelted(result)) { if (!ItemUtils.isSmelted(result)) {
return; return;
} }
Player player = Misc.getPlayerFromFurnace(furnaceBlock); Player player = getPlayerFromFurnace(furnaceBlock);
if (Misc.isNPCEntity(player) || !Permissions.vanillaXpBoost(player, SkillType.SMELTING)) { if (Misc.isNPCEntity(player) || !Permissions.vanillaXpBoost(player, SkillType.SMELTING)) {
return; return;
@ -137,4 +150,28 @@ public class InventoryListener implements Listener {
new PlayerUpdateInventoryTask((Player) whoClicked).runTaskLater(plugin, 0); new PlayerUpdateInventoryTask((Player) whoClicked).runTaskLater(plugin, 0);
} }
private Block processInventoryOpenorCloseEvent(Inventory inventory) {
if (!(inventory instanceof FurnaceInventory)) {
return null;
}
Furnace furnace = (Furnace) inventory.getHolder();
if (furnace == null || furnace.getBurnTime() != 0) {
return null;
}
return furnace.getBlock();
}
private Player getPlayerFromFurnace(Block furnaceBlock) {
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
if (metadata.isEmpty()) {
return null;
}
return plugin.getServer().getPlayerExact(metadata.get(0).asString());
}
} }

View File

@ -9,19 +9,12 @@ import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Furnace;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
import org.bukkit.entity.NPC; import org.bukkit.entity.NPC;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryEvent;
import org.bukkit.inventory.FurnaceInventory;
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.config.Config; import com.gmail.nossr50.config.Config;
@ -219,52 +212,6 @@ public final class Misc {
} }
} }
public static Block processInventoryOpenorCloseEvent(InventoryEvent event) {
Inventory inventory = event.getInventory();
if (!(inventory instanceof FurnaceInventory)) {
return null;
}
Furnace furnace = (Furnace) inventory.getHolder();
if (furnace == null || furnace.getBurnTime() != 0) {
return null;
}
return furnace.getBlock();
}
public static Player getPlayerFromFurnace(Block furnaceBlock) {
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
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();
}
/** /**
* Attempts to match any player names with the given name, and returns a list of all possibly matches. * Attempts to match any player names with the given name, and returns a list of all possibly matches.
* *