More work to the inventory util.

This commit is contained in:
Olof Larsson 2013-03-22 11:05:06 +01:00
parent c12c1ac765
commit 90f2c1059b
2 changed files with 60 additions and 51 deletions

View File

@ -3,7 +3,10 @@ package com.massivecraft.mcore.util;
import java.util.HashMap; import java.util.HashMap;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryClickEvent; 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.Inventory;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -16,6 +19,9 @@ public class InventoryUtil
public static boolean isGiving(InventoryClickEvent event) public static boolean isGiving(InventoryClickEvent event)
{ {
// Outside inventory?
if (event.getRawSlot() < 0) return false;
// Was the upper inventory clicked? // Was the upper inventory clicked?
boolean upperClicked = event.getRawSlot() < event.getInventory().getSize(); boolean upperClicked = event.getRawSlot() < event.getInventory().getSize();
@ -35,6 +41,9 @@ public class InventoryUtil
public static boolean isTaking(InventoryClickEvent event) public static boolean isTaking(InventoryClickEvent event)
{ {
// Outside inventory?
if (event.getRawSlot() < 0) return false;
// Was the upper inventory clicked? // Was the upper inventory clicked?
boolean upperClicked = event.getRawSlot() < event.getInventory().getSize(); boolean upperClicked = event.getRawSlot() < event.getInventory().getSize();
@ -48,6 +57,57 @@ public class InventoryUtil
return ret; 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? // IS EMPTY?
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -34,13 +34,9 @@ import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause; 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.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import com.massivecraft.mcore.InternalListener; import com.massivecraft.mcore.InternalListener;
import com.massivecraft.mcore.MCore; import com.massivecraft.mcore.MCore;
@ -235,53 +231,6 @@ public class MUtil
return ret; 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) public static boolean isCombatEvent(EntityDamageEvent event)
{ {
if (event.getCause() != DamageCause.ENTITY_ATTACK && event.getCause() != DamageCause.PROJECTILE) return false; if (event.getCause() != DamageCause.ENTITY_ATTACK && event.getCause() != DamageCause.PROJECTILE) return false;