mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-25 22:56:45 +01:00
Tidy up our InventoryListener.
This commit is contained in:
parent
a1792fe125
commit
28a78d820a
@ -542,20 +542,21 @@ public class EntityListener implements Listener {
|
||||
Entity entity = event.getEntity();
|
||||
Entity target = event.getTarget();
|
||||
|
||||
if (entity instanceof Tameable && target instanceof Player) {
|
||||
Player player = (Player) target;
|
||||
Tameable tameable = (Tameable) entity;
|
||||
if (!(entity instanceof Tameable) || !(target instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!CombatUtils.isFriendlyPet(player, tameable)) {
|
||||
return;
|
||||
}
|
||||
Player player = (Player) target;
|
||||
Tameable tameable = (Tameable) entity;
|
||||
|
||||
// isFriendlyPet ensures that the Tameable is: Tamed, owned by a player, and the owner is in the same party
|
||||
// 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;
|
||||
}
|
||||
if (!CombatUtils.isFriendlyPet(player, tameable)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// isFriendlyPet ensures that the Tameable is: Tamed, owned by a player, and the owner is in the same party
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)) {
|
||||
furnaceBlock.setMetadata(mcMMO.furnaceMetadataKey, new FixedMetadataValue(plugin, player.getName()));
|
||||
}
|
||||
}
|
||||
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,22 +63,10 @@ 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)) {
|
||||
furnaceBlock.removeMetadata(mcMMO.furnaceMetadataKey, plugin);
|
||||
}
|
||||
}
|
||||
if (furnaceBlock != null && furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) {
|
||||
furnaceBlock.removeMetadata(mcMMO.furnaceMetadataKey, plugin);
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,21 +75,30 @@ public class InventoryListener implements Listener {
|
||||
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 (!metadata.isEmpty() && smelting != null && ItemUtils.isSmeltable(smelting)) {
|
||||
// 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());
|
||||
|
||||
if (Misc.isNPCEntity(player) || !Permissions.fuelEfficiency(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.setBurnTime(UserManager.getPlayer(player).getSmeltingManager().fuelEfficiency(event.getBurnTime()));
|
||||
}
|
||||
if (!(furnaceState instanceof Furnace)) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
|
||||
|
||||
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());
|
||||
|
||||
if (Misc.isNPCEntity(player) || !Permissions.fuelEfficiency(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.setBurnTime(UserManager.getPlayer(player).getSmeltingManager().fuelEfficiency(event.getBurnTime()));
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
@ -121,29 +106,40 @@ public class InventoryListener implements Listener {
|
||||
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) {
|
||||
if ((Config.getInstance().getPotatoChance() / 100.0) >= Misc.getRandom().nextDouble()) {
|
||||
event.setCancelled(true);
|
||||
furnaceState.getWorld().createExplosion(furnaceState.getLocation(), 4F, true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
ItemStack smelting = ((Furnace) furnaceState).getInventory().getSmelting();
|
||||
|
||||
if (!metadata.isEmpty() && smelting != null && ItemUtils.isSmeltable(smelting)) {
|
||||
// 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());
|
||||
if (smelting == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Misc.isNPCEntity(player) || !Permissions.skillEnabled(player, SkillType.SMELTING)) {
|
||||
return;
|
||||
}
|
||||
Material smeltingType = smelting.getType();
|
||||
|
||||
event.setResult(UserManager.getPlayer(player).getSmeltingManager().smeltProcessing(event.getSource().getType(), event.getResult()));
|
||||
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);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
if (Misc.isNPCEntity(player) || !Permissions.skillEnabled(player, SkillType.SMELTING)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.setResult(UserManager.getPlayer(player).getSmeltingManager().smeltProcessing(smeltingType, event.getResult()));
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
@ -151,24 +147,33 @@ public class InventoryListener implements Listener {
|
||||
Block furnaceBlock = event.getBlock();
|
||||
BlockState furnaceState = furnaceBlock.getState();
|
||||
|
||||
if (furnaceState instanceof Furnace) {
|
||||
ItemStack result = ((Furnace) furnaceState).getInventory().getResult();
|
||||
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
|
||||
if (!(furnaceState instanceof Furnace)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!metadata.isEmpty() && result != null && ItemUtils.isSmelted(result)) {
|
||||
// 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());
|
||||
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
|
||||
|
||||
if (Misc.isNPCEntity(player)) {
|
||||
return;
|
||||
}
|
||||
if (metadata.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
SmeltingManager smeltingManager = UserManager.getPlayer(player).getSmeltingManager();
|
||||
ItemStack result = ((Furnace) furnaceState).getInventory().getResult();
|
||||
|
||||
if (smeltingManager.canUseVanillaXpBoost()) {
|
||||
event.setExpToDrop(smeltingManager.vanillaXPBoost(event.getExpToDrop()));
|
||||
}
|
||||
}
|
||||
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());
|
||||
|
||||
if (Misc.isNPCEntity(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
SmeltingManager smeltingManager = UserManager.getPlayer(player).getSmeltingManager();
|
||||
|
||||
if (smeltingManager.canUseVanillaXpBoost()) {
|
||||
event.setExpToDrop(smeltingManager.vanillaXPBoost(event.getExpToDrop()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}, 0);
|
||||
ItemStack result = event.getRecipe().getResult();
|
||||
|
||||
if (!ItemUtils.isMcMMOItem(result)) {
|
||||
return;
|
||||
}
|
||||
|
||||
new PlayerUpdateInventoryTask((Player) whoClicked).runTaskLater(plugin, 0);
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user