From 90f2c1059b6e55c5755d817f32a8c428e6161046 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 22 Mar 2013 11:05:06 +0100 Subject: [PATCH] More work to the inventory util. --- .../mcore/util/InventoryUtil.java | 60 +++++++++++++++++++ src/com/massivecraft/mcore/util/MUtil.java | 51 ---------------- 2 files changed, 60 insertions(+), 51 deletions(-) diff --git a/src/com/massivecraft/mcore/util/InventoryUtil.java b/src/com/massivecraft/mcore/util/InventoryUtil.java index c50d1bda..368ffd5d 100644 --- a/src/com/massivecraft/mcore/util/InventoryUtil.java +++ b/src/com/massivecraft/mcore/util/InventoryUtil.java @@ -3,7 +3,10 @@ package com.massivecraft.mcore.util; import java.util.HashMap; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.inventory.InventoryType.SlotType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; @@ -16,6 +19,9 @@ public class InventoryUtil public static boolean isGiving(InventoryClickEvent event) { + // Outside inventory? + if (event.getRawSlot() < 0) return false; + // Was the upper inventory clicked? boolean upperClicked = event.getRawSlot() < event.getInventory().getSize(); @@ -35,6 +41,9 @@ public class InventoryUtil public static boolean isTaking(InventoryClickEvent event) { + // Outside inventory? + if (event.getRawSlot() < 0) return false; + // Was the upper inventory clicked? boolean upperClicked = event.getRawSlot() < event.getInventory().getSize(); @@ -48,6 +57,57 @@ public class InventoryUtil return ret; } + public static boolean isAltering(InventoryClickEvent event) + { + return isGiving(event) || isTaking(event); + } + + /** + * This method will return the ItemStack the player is trying to equip. + * If the click event would not result in equipping something null will be returned. + * Note that this algorithm is not perfect. It's an adequate guess. + * + * @param event The InventoryClickEvent to analyze. + * @return The ItemStack the player is trying to equip. + */ + public static ItemStack isEquipping(InventoryClickEvent event) + { + /* + System.out.println("---"); + System.out.println("getInventory().getType() "+event.getInventory().getType()); + System.out.println("getView().getTopInventory().getType() "+event.getView().getTopInventory().getType()); + System.out.println("getView().getType() "+event.getView().getType()); + System.out.println("getView().getBottomInventory().getType() "+event.getView().getBottomInventory().getType()); + System.out.println("event.getSlotType() "+event.getSlotType()); + System.out.println("event.getRawSlot() "+event.getRawSlot()); + System.out.println("event.getSlot() "+event.getSlot()); + */ + + boolean isShiftClick = event.isShiftClick(); + InventoryType inventoryType = event.getInventory().getType(); + SlotType slotType = event.getSlotType(); + ItemStack cursor = event.getCursor(); + ItemStack currentItem = event.getCurrentItem(); + + if (isShiftClick) + { + if (inventoryType != InventoryType.CRAFTING) return null; + if (slotType == SlotType.CRAFTING) return null; + if (slotType == SlotType.ARMOR) return null; + if (slotType == SlotType.RESULT) return null; + if (currentItem.getType() == Material.AIR) return null; + return currentItem; + } + else + { + if (slotType == SlotType.ARMOR) + { + return cursor; + } + return null; + } + } + // -------------------------------------------- // // IS EMPTY? // -------------------------------------------- // diff --git a/src/com/massivecraft/mcore/util/MUtil.java b/src/com/massivecraft/mcore/util/MUtil.java index 690dc2b6..453ac652 100644 --- a/src/com/massivecraft/mcore/util/MUtil.java +++ b/src/com/massivecraft/mcore/util/MUtil.java @@ -34,13 +34,9 @@ import org.bukkit.event.block.Action; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent.DamageCause; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryType; -import org.bukkit.event.inventory.InventoryType.SlotType; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.inventory.ItemStack; import com.massivecraft.mcore.InternalListener; import com.massivecraft.mcore.MCore; @@ -235,53 +231,6 @@ public class MUtil return ret; } - /** - * This method will return the ItemStack the player is trying to equip. - * If the click event would not result in equipping something null will be returned. - * Note that this algorithm is not perfect. It's an adequate guess. - * - * @param event The InventoryClickEvent to analyze. - * @return The ItemStack the player is trying to equip. - */ - public static ItemStack getEquipping(InventoryClickEvent event) - { - /* - System.out.println("---"); - System.out.println("getInventory().getType() "+event.getInventory().getType()); - System.out.println("getView().getTopInventory().getType() "+event.getView().getTopInventory().getType()); - System.out.println("getView().getType() "+event.getView().getType()); - System.out.println("getView().getBottomInventory().getType() "+event.getView().getBottomInventory().getType()); - System.out.println("event.getSlotType() "+event.getSlotType()); - System.out.println("event.getRawSlot() "+event.getRawSlot()); - System.out.println("event.getSlot() "+event.getSlot()); - */ - - boolean isShiftClick = event.isShiftClick(); - InventoryType inventoryType = event.getInventory().getType(); - SlotType slotType = event.getSlotType(); - ItemStack cursor = event.getCursor(); - ItemStack currentItem = event.getCurrentItem(); - - if (isShiftClick) - { - if (inventoryType != InventoryType.CRAFTING) return null; - if (slotType == SlotType.CRAFTING) return null; - if (slotType == SlotType.ARMOR) return null; - if (slotType == SlotType.RESULT) return null; - if (currentItem.getType() == Material.AIR) return null; - return currentItem; - } - else - { - if (slotType == SlotType.ARMOR) - { - return cursor; - } - return null; - } - } - - public static boolean isCombatEvent(EntityDamageEvent event) { if (event.getCause() != DamageCause.ENTITY_ATTACK && event.getCause() != DamageCause.PROJECTILE) return false;