Event Interpretation Improvements. Modified ItemStack similar.

This commit is contained in:
Olof Larsson 2016-08-28 12:32:56 +02:00
parent 2941e9b9b8
commit 43ef6d26e2
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
2 changed files with 72 additions and 12 deletions

View File

@ -470,7 +470,9 @@ public class DataItemStack implements Comparable<DataItemStack>
public boolean isSimilar(DataItemStack that) public boolean isSimilar(DataItemStack that)
{ {
// Just copy equals and comment out count check. // A copy of the equals logic above. However we comment out:
// * Count
// * Repaircost
return MUtil.equals( return MUtil.equals(
this.getId(), that.getId(), this.getId(), that.getId(),
// this.getCount(), that.getCount(), // this.getCount(), that.getCount(),
@ -478,7 +480,7 @@ public class DataItemStack implements Comparable<DataItemStack>
this.getName(), that.getName(), this.getName(), that.getName(),
this.getLore(), that.getLore(), this.getLore(), that.getLore(),
this.getEnchants(), that.getEnchants(), this.getEnchants(), that.getEnchants(),
this.getRepaircost(), that.getRepaircost(), // this.getRepaircost(), that.getRepaircost(),
this.getTitle(), that.getTitle(), this.getTitle(), that.getTitle(),
this.getAuthor(), that.getAuthor(), this.getAuthor(), that.getAuthor(),
this.getPages(), that.getPages(), this.getPages(), that.getPages(),

View File

@ -470,18 +470,20 @@ public class InventoryUtil
} }
// -------------------------------------------- // // -------------------------------------------- //
// EVENT INTERPRETATION // EVENT INTERPRETATION > WHERE
// -------------------------------------------- // // -------------------------------------------- //
public static boolean isOutside(int rawSlot) public static boolean isOutside(int rawSlot)
{ {
return rawSlot < 0; return rawSlot < 0;
} }
public static boolean isTopInventory(int rawSlot, Inventory inventory) public static boolean isTopInventory(int rawSlot, Inventory inventory)
{ {
if (isOutside(rawSlot)) return false; if (isOutside(rawSlot)) return false;
return rawSlot < inventory.getSize(); return rawSlot < inventory.getSize();
} }
public static boolean isBottomInventory(int rawSlot, Inventory inventory) public static boolean isBottomInventory(int rawSlot, Inventory inventory)
{ {
if (isOutside(rawSlot)) return false; if (isOutside(rawSlot)) return false;
@ -492,21 +494,44 @@ public class InventoryUtil
{ {
return isOutside(event.getRawSlot()); return isOutside(event.getRawSlot());
} }
public static boolean isTopInventory(InventoryClickEvent event) public static boolean isTopInventory(InventoryClickEvent event)
{ {
return isTopInventory(event.getRawSlot(), event.getInventory()); return isTopInventory(event.getRawSlot(), event.getInventory());
} }
public static boolean isBottomInventory(InventoryClickEvent event) public static boolean isBottomInventory(InventoryClickEvent event)
{ {
return isBottomInventory(event.getRawSlot(), event.getInventory()); return isBottomInventory(event.getRawSlot(), event.getInventory());
} }
public static boolean isAltering(InventoryClickEvent event) // -------------------------------------------- //
// EVENT INTERPRETATION > ALTER
// -------------------------------------------- //
public static boolean isAltering(InventoryInteractEvent event)
{ {
return getAlter(event).isAltering(); return getAlter(event).isAltering();
} }
public static InventoryAlter getAlter(InventoryClickEvent event) public static InventoryAlter getAlter(InventoryInteractEvent event)
{
if (event instanceof InventoryClickEvent)
{
InventoryClickEvent clickEvent = (InventoryClickEvent)event;
return getAlter(clickEvent);
}
if (event instanceof InventoryDragEvent)
{
InventoryDragEvent dragEvent = (InventoryDragEvent)event;
return getAlter(dragEvent);
}
throw new IllegalArgumentException("Neither InventoryClickEvent nor InventoryDragEvent: " + event);
}
private static InventoryAlter getAlter(InventoryClickEvent event)
{ {
if (isOutside(event)) return InventoryAlter.NONE; if (isOutside(event)) return InventoryAlter.NONE;
boolean topClicked = isTopInventory(event); boolean topClicked = isTopInventory(event);
@ -526,7 +551,7 @@ public class InventoryUtil
boolean give = isSomething(hotbar); boolean give = isSomething(hotbar);
boolean take = isSomething(current); boolean take = isSomething(current);
return getAlter(give, take); return InventoryAlter.get(give, take);
// Neither give nor take // Neither give nor take
case NOTHING: return InventoryAlter.NONE; case NOTHING: return InventoryAlter.NONE;
@ -569,12 +594,33 @@ public class InventoryUtil
} }
} }
private static InventoryAlter getAlter(boolean give, boolean take) // Drag events by nature only matters when they affect the top inventory.
// What you are holding in the cursor is already yours.
// If you drag it into your own inventory you are not really taking anything.
// If you drag into the top inventory however, you may both give and take.
// You "take" by dragging over an existing item.
private static InventoryAlter getAlter(InventoryDragEvent event)
{ {
if (give && take) return InventoryAlter.BOTH; // Create
if (give) return InventoryAlter.GIVE; boolean giving = false;
if (take) return InventoryAlter.TAKE; boolean taking = false;
return InventoryAlter.NONE;
// Fill
final Inventory inventory = event.getInventory();
for (Entry<Integer, ItemStack> entry : event.getNewItems().entrySet())
{
int rawSlot = entry.getKey();
if (InventoryUtil.isBottomInventory(rawSlot, inventory)) continue;
ItemStack take = inventory.getItem(rawSlot);
if (isSomething(take)) taking = true;
ItemStack give = entry.getValue();
if (isSomething(give)) giving = true;
}
// Return
return InventoryAlter.get(giving, taking);
} }
public enum InventoryAlter public enum InventoryAlter
@ -600,13 +646,25 @@ public class InventoryUtil
return this == TAKE || this == BOTH; return this == TAKE || this == BOTH;
} }
public boolean isNone() public boolean isNothing()
{ {
return this == NONE; return this == NONE;
} }
public static InventoryAlter get(boolean giving, boolean taking)
{
if (giving && taking) return BOTH;
if (giving) return GIVE;
if (taking) return TAKE;
return NONE;
} }
}
// -------------------------------------------- //
// EVENT INTERPRETATION > EQUIPPING
// -------------------------------------------- //
/** /**
* This method will return the ItemStack the player is trying to equip. * 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. * If the click event would not result in equipping something null will be returned.