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 entity = event.getEntity();
Entity target = event.getTarget(); Entity target = event.getTarget();
if (entity instanceof Tameable && target instanceof Player) { if (!(entity instanceof Tameable) || !(target instanceof Player)) {
return;
}
Player player = (Player) target; Player player = (Player) target;
Tameable tameable = (Tameable) entity; 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 // So we can make some assumptions here, about our casting and our check
if (!(Permissions.friendlyFire(player) && Permissions.friendlyFire((Player) tameable.getOwner()))) { if (!(Permissions.friendlyFire(player) && Permissions.friendlyFire((Player) tameable.getOwner()))) {
event.setCancelled(true); 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.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.FixedMetadataValue; import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue; import org.bukkit.metadata.MetadataValue;
@ -27,6 +25,7 @@ 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;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.runnables.PlayerUpdateInventoryTask;
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;
@ -41,7 +40,7 @@ public class InventoryListener implements Listener {
this.plugin = plugin; this.plugin = plugin;
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryOpen(InventoryOpenEvent event) { public void onInventoryOpen(InventoryOpenEvent event) {
HumanEntity player = event.getPlayer(); HumanEntity player = event.getPlayer();
@ -49,25 +48,14 @@ public class InventoryListener implements Listener {
return; return;
} }
Inventory inventory = event.getInventory(); Block furnaceBlock = Misc.processInventoryOpenorCloseEvent(event);
if (inventory instanceof FurnaceInventory) { if (furnaceBlock != null && !furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) {
Furnace furnace = (Furnace) inventory.getHolder();
if (furnace == null) {
return;
}
if (furnace.getBurnTime() == 0) {
Block furnaceBlock = furnace.getBlock();
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, ignoreCancelled = true)
public void onInventoryClose(InventoryCloseEvent event) { public void onInventoryClose(InventoryCloseEvent event) {
HumanEntity player = event.getPlayer(); HumanEntity player = event.getPlayer();
@ -75,35 +63,34 @@ public class InventoryListener implements Listener {
return; return;
} }
Inventory inventory = event.getInventory(); Block furnaceBlock = Misc.processInventoryOpenorCloseEvent(event);
if (inventory instanceof FurnaceInventory) { if (furnaceBlock != null && furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) {
Furnace furnace = (Furnace) inventory.getHolder();
if (furnace == null) {
return;
}
if (furnace.getBurnTime() == 0) {
Block furnaceBlock = furnace.getBlock();
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) furnaceState).getInventory().getSmelting(); return;
}
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey); 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 // 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()); 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())); 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) furnaceState).getInventory().getSmelting(); return;
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey); }
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()) { if ((Config.getInstance().getPotatoChance() / 100.0) >= Misc.getRandom().nextDouble()) {
event.setCancelled(true); event.setCancelled(true);
furnaceState.getWorld().createExplosion(furnaceState.getLocation(), 4F, 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 // 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()); Player player = plugin.getServer().getPlayer(metadata.get(0).asString());
@ -141,9 +139,7 @@ public class InventoryListener implements Listener {
return; 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) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@ -151,11 +147,22 @@ public class InventoryListener implements Listener {
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 result = ((Furnace) furnaceState).getInventory().getResult(); return;
}
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey); 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 // 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()); Player player = plugin.getServer().getPlayer(metadata.get(0).asString());
@ -169,8 +176,6 @@ public class InventoryListener implements Listener {
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) {
@ -179,18 +184,18 @@ public class InventoryListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onCraftItem(CraftItemEvent event) { public void onCraftItem(CraftItemEvent event) {
ItemStack result = event.getRecipe().getResult();
final HumanEntity whoClicked = event.getWhoClicked(); final HumanEntity whoClicked = event.getWhoClicked();
if (!ItemUtils.isMcMMOItem(result) || Misc.isNPCEntity(whoClicked) || !(whoClicked instanceof Player)) { if (Misc.isNPCEntity(whoClicked) || !(whoClicked instanceof Player)) {
return; return;
} }
mcMMO.p.getServer().getScheduler().runTaskLater(mcMMO.p, new Runnable() { ItemStack result = event.getRecipe().getResult();
@Override
public void run() { if (!ItemUtils.isMcMMOItem(result)) {
((Player) whoClicked).updateInventory(); 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.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block;
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.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 com.gmail.nossr50.mcMMO; 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() { public static Random getRandom() {
return random; return random;
} }