mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-26 07:06: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 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)) {
|
||||||
Player player = (Player) target;
|
return;
|
||||||
Tameable tameable = (Tameable) entity;
|
}
|
||||||
|
|
||||||
if (!CombatUtils.isFriendlyPet(player, tameable)) {
|
Player player = (Player) target;
|
||||||
return;
|
Tameable tameable = (Tameable) entity;
|
||||||
}
|
|
||||||
|
|
||||||
// isFriendlyPet ensures that the Tameable is: Tamed, owned by a player, and the owner is in the same party
|
if (!CombatUtils.isFriendlyPet(player, tameable)) {
|
||||||
// So we can make some assumptions here, about our casting and our check
|
return;
|
||||||
if (!(Permissions.friendlyFire(player) && Permissions.friendlyFire((Player) tameable.getOwner()))) {
|
}
|
||||||
event.setCancelled(true);
|
|
||||||
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.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();
|
furnaceBlock.setMetadata(mcMMO.furnaceMetadataKey, new FixedMetadataValue(plugin, player.getName()));
|
||||||
|
|
||||||
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()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@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,22 +63,10 @@ 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();
|
furnaceBlock.removeMetadata(mcMMO.furnaceMetadataKey, plugin);
|
||||||
|
|
||||||
if (furnace == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (furnace.getBurnTime() == 0) {
|
|
||||||
Block furnaceBlock = furnace.getBlock();
|
|
||||||
|
|
||||||
if (furnaceBlock.hasMetadata(mcMMO.furnaceMetadataKey)) {
|
|
||||||
furnaceBlock.removeMetadata(mcMMO.furnaceMetadataKey, plugin);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,21 +75,30 @@ 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 smelting = ((Furnace) furnaceState).getInventory().getSmelting();
|
return;
|
||||||
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()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||||
@ -121,29 +106,40 @@ 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 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 ((Config.getInstance().getPotatoChance() / 100.0) >= Misc.getRandom().nextDouble()) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
furnaceState.getWorld().createExplosion(furnaceState.getLocation(), 4F, true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!metadata.isEmpty() && smelting != null && ItemUtils.isSmeltable(smelting)) {
|
if (smelting == null) {
|
||||||
// We can make this assumption because we (should) be the only ones using this exact metadata
|
return;
|
||||||
Player player = plugin.getServer().getPlayer(metadata.get(0).asString());
|
}
|
||||||
|
|
||||||
if (Misc.isNPCEntity(player) || !Permissions.skillEnabled(player, SkillType.SMELTING)) {
|
Material smeltingType = smelting.getType();
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||||
@ -151,24 +147,33 @@ 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);
|
}
|
||||||
|
|
||||||
if (!metadata.isEmpty() && result != null && ItemUtils.isSmelted(result)) {
|
List<MetadataValue> metadata = furnaceBlock.getMetadata(mcMMO.furnaceMetadataKey);
|
||||||
// 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)) {
|
if (metadata.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
SmeltingManager smeltingManager = UserManager.getPlayer(player).getSmeltingManager();
|
ItemStack result = ((Furnace) furnaceState).getInventory().getResult();
|
||||||
|
|
||||||
if (smeltingManager.canUseVanillaXpBoost()) {
|
if (result == null || !ItemUtils.isSmelted(result)) {
|
||||||
event.setExpToDrop(smeltingManager.vanillaXPBoost(event.getExpToDrop()));
|
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)
|
@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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user