Tidy up our InventoryListener.

This commit is contained in:
GJ 2013-06-12 15:10:43 -04:00
parent a1792fe125
commit 28a78d820a
4 changed files with 142 additions and 98 deletions

View File

@ -542,7 +542,10 @@ public class EntityListener implements Listener {
Entity entity = event.getEntity();
Entity target = event.getTarget();
if (entity instanceof Tameable && target instanceof Player) {
if (!(entity instanceof Tameable) || !(target instanceof Player)) {
return;
}
Player player = (Player) target;
Tameable tameable = (Tameable) entity;
@ -554,8 +557,6 @@ public class EntityListener implements Listener {
// So we can make some assumptions here, about our casting and our check
if (!(Permissions.friendlyFire(player) && Permissions.friendlyFire((Player) tameable.getOwner()))) {
event.setCancelled(true);
return;
}
}
}
}

View File

@ -18,8 +18,6 @@ import org.bukkit.event.inventory.FurnaceSmeltEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.inventory.FurnaceInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
@ -27,6 +25,7 @@ import org.bukkit.metadata.MetadataValue;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config;
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;
@ -41,7 +40,7 @@ public class InventoryListener implements Listener {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.MONITOR)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryOpen(InventoryOpenEvent event) {
HumanEntity player = event.getPlayer();
@ -49,25 +48,14 @@ public class InventoryListener implements Listener {
return;
}
Inventory inventory = event.getInventory();
Block furnaceBlock = Misc.processInventoryOpenorCloseEvent(event);
if (inventory instanceof FurnaceInventory) {
Furnace furnace = (Furnace) inventory.getHolder();
if (furnace == null) {
return;
}
if (furnace.getBurnTime() == 0) {
Block furnaceBlock = furnace.getBlock();
if (!furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) {
if (furnaceBlock != null && !furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) {
furnaceBlock.setMetadata(mcMMO.furnaceMetadataKey, new FixedMetadataValue(plugin, player.getName()));
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryClose(InventoryCloseEvent event) {
HumanEntity player = event.getPlayer();
@ -75,35 +63,34 @@ public class InventoryListener implements Listener {
return;
}
Inventory inventory = event.getInventory();
Block furnaceBlock = Misc.processInventoryOpenorCloseEvent(event);
if (inventory instanceof FurnaceInventory) {
Furnace furnace = (Furnace) inventory.getHolder();
if (furnace == null) {
return;
}
if (furnace.getBurnTime() == 0) {
Block furnaceBlock = furnace.getBlock();
if (furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) {
if (furnaceBlock != null && furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) {
furnaceBlock.removeMetadata(mcMMO.furnaceMetadataKey, plugin);
}
}
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onFurnaceBurnEvent(FurnaceBurnEvent event) {
Block furnaceBlock = event.getBlock();
BlockState furnaceState = furnaceBlock.getState();
if (furnaceState instanceof Furnace) {
ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting();
if (!(furnaceState instanceof Furnace)) {
return;
}
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
if (!metadata.isEmpty() && smelting != null && ItemUtils.isSmeltable(smelting)) {
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());
@ -113,19 +100,25 @@ public class InventoryListener implements Listener {
event.setBurnTime(UserManager.getPlayer(player).getSmeltingManager().fuelEfficiency(event.getBurnTime()));
}
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onFurnaceSmeltEvent(FurnaceSmeltEvent event) {
Block furnaceBlock = event.getBlock();
BlockState furnaceState = furnaceBlock.getState();
if (furnaceState instanceof Furnace) {
ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting();
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
if (!(furnaceState instanceof Furnace)) {
return;
}
if (Config.getInstance().getPotatoEnabled() && smelting.getType() == Material.POTATO_ITEM) {
ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting();
if (smelting == null) {
return;
}
Material smeltingType = smelting.getType();
if (Config.getInstance().getPotatoEnabled() && smeltingType == Material.POTATO_ITEM) {
if ((Config.getInstance().getPotatoChance() / 100.0) >= Misc.getRandom().nextDouble()) {
event.setCancelled(true);
furnaceState.getWorld().createExplosion(furnaceState.getLocation(), 4F, true);
@ -133,7 +126,12 @@ public class InventoryListener implements Listener {
}
}
if (!metadata.isEmpty() && smelting != null && ItemUtils.isSmeltable(smelting)) {
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
if (metadata.isEmpty() || !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());
@ -141,9 +139,7 @@ public class InventoryListener implements Listener {
return;
}
event.setResult(UserManager.getPlayer(player).getSmeltingManager().smeltProcessing(event.getSource().getType(), event.getResult()));
}
}
event.setResult(UserManager.getPlayer(player).getSmeltingManager().smeltProcessing(smeltingType, event.getResult()));
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@ -151,11 +147,22 @@ public class InventoryListener implements Listener {
Block furnaceBlock = event.getBlock();
BlockState furnaceState = furnaceBlock.getState();
if (furnaceState instanceof Furnace) {
ItemStack result = ((Furnace) furnaceState).getInventory().getResult();
if (!(furnaceState instanceof Furnace)) {
return;
}
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
if (!metadata.isEmpty() && result != null && ItemUtils.isSmelted(result)) {
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());
@ -169,8 +176,6 @@ public class InventoryListener implements Listener {
event.setExpToDrop(smeltingManager.vanillaXPBoost(event.getExpToDrop()));
}
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryClickEvent(InventoryClickEvent event) {
@ -179,18 +184,18 @@ public class InventoryListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onCraftItem(CraftItemEvent event) {
ItemStack result = event.getRecipe().getResult();
final HumanEntity whoClicked = event.getWhoClicked();
if (!ItemUtils.isMcMMOItem(result) || Misc.isNPCEntity(whoClicked) || !(whoClicked instanceof Player)) {
if (Misc.isNPCEntity(whoClicked) || !(whoClicked instanceof Player)) {
return;
}
mcMMO.p.getServer().getScheduler().runTaskLater(mcMMO.p, new Runnable() {
@Override
public void run() {
((Player) whoClicked).updateInventory();
ItemStack result = event.getRecipe().getResult();
if (!ItemUtils.isMcMMOItem(result)) {
return;
}
}, 0);
new PlayerUpdateInventoryTask((Player) whoClicked).runTaskLater(plugin, 0);
}
}

View File

@ -0,0 +1,17 @@
package com.gmail.nossr50.runnables;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
public class PlayerUpdateInventoryTask extends BukkitRunnable {
private Player player;
public PlayerUpdateInventoryTask(Player player) {
this.player = player;
}
@Override
public void run() {
player.updateInventory();
}
}

View File

@ -6,9 +6,14 @@ import java.util.Random;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Furnace;
import org.bukkit.entity.Entity;
import org.bukkit.entity.HumanEntity;
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 com.gmail.nossr50.mcMMO;
@ -204,6 +209,22 @@ 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 Random getRandom() {
return random;
}