Added UpdateReason.SUPPRESSED for suppressing VI event calls

This commit is contained in:
NichtStudioCode 2022-12-20 18:28:33 +01:00
parent f45417340f
commit ae4eb08097
2 changed files with 68 additions and 32 deletions

@ -319,6 +319,9 @@ public class VirtualInventory {
* @return The {@link ItemUpdateEvent} after it has been handled by the {@link #itemUpdateHandler} * @return The {@link ItemUpdateEvent} after it has been handled by the {@link #itemUpdateHandler}
*/ */
public ItemUpdateEvent callPreUpdateEvent(@Nullable UpdateReason updateReason, int slot, @Nullable ItemStack previousItemStack, @Nullable ItemStack newItemStack) { public ItemUpdateEvent callPreUpdateEvent(@Nullable UpdateReason updateReason, int slot, @Nullable ItemStack previousItemStack, @Nullable ItemStack newItemStack) {
if (updateReason == UpdateReason.SUPPRESSED)
throw new IllegalArgumentException("Cannot call ItemUpdateEvent with UpdateReason.SUPPRESSED");
ItemUpdateEvent event = new ItemUpdateEvent(this, slot, updateReason, previousItemStack, newItemStack); ItemUpdateEvent event = new ItemUpdateEvent(this, slot, updateReason, previousItemStack, newItemStack);
if (itemUpdateHandler != null) { if (itemUpdateHandler != null) {
try { try {
@ -337,9 +340,11 @@ public class VirtualInventory {
* @param slot The slot of the affected {@link ItemStack} * @param slot The slot of the affected {@link ItemStack}
* @param previousItemStack The {@link ItemStack} that was on that slot previously. * @param previousItemStack The {@link ItemStack} that was on that slot previously.
* @param newItemStack The {@link ItemStack} that is on that slot now. * @param newItemStack The {@link ItemStack} that is on that slot now.
* @return The {@link InventoryUpdatedEvent} after it has been handled by the {@link #inventoryUpdatedHandler}
*/ */
public InventoryUpdatedEvent callAfterUpdateEvent(@Nullable UpdateReason updateReason, int slot, @Nullable ItemStack previousItemStack, @Nullable ItemStack newItemStack) { public void callAfterUpdateEvent(@Nullable UpdateReason updateReason, int slot, @Nullable ItemStack previousItemStack, @Nullable ItemStack newItemStack) {
if (updateReason == UpdateReason.SUPPRESSED)
throw new IllegalArgumentException("Cannot call InventoryUpdatedEvent with UpdateReason.SUPPRESSED");
InventoryUpdatedEvent event = new InventoryUpdatedEvent(this, slot, updateReason, previousItemStack, newItemStack); InventoryUpdatedEvent event = new InventoryUpdatedEvent(this, slot, updateReason, previousItemStack, newItemStack);
if (inventoryUpdatedHandler != null) { if (inventoryUpdatedHandler != null) {
try { try {
@ -348,7 +353,6 @@ public class VirtualInventory {
e.printStackTrace(); e.printStackTrace();
} }
} }
return event;
} }
/** /**
@ -395,6 +399,10 @@ public class VirtualInventory {
* @return If the action was successful * @return If the action was successful
*/ */
public boolean forceSetItemStack(@Nullable UpdateReason updateReason, int slot, @Nullable ItemStack itemStack) { public boolean forceSetItemStack(@Nullable UpdateReason updateReason, int slot, @Nullable ItemStack itemStack) {
if (updateReason == UpdateReason.SUPPRESSED) {
setItemStackSilently(slot, itemStack);
return true;
} else {
ItemStack previousStack = items[slot]; ItemStack previousStack = items[slot];
ItemUpdateEvent event = callPreUpdateEvent(updateReason, slot, previousStack, itemStack); ItemUpdateEvent event = callPreUpdateEvent(updateReason, slot, previousStack, itemStack);
if (!event.isCancelled()) { if (!event.isCancelled()) {
@ -405,6 +413,7 @@ public class VirtualInventory {
} }
return false; return false;
} }
}
/** /**
* Changes the {@link ItemStack} on a specific slot to the given one, regardless of what previously was on * Changes the {@link ItemStack} on a specific slot to the given one, regardless of what previously was on
@ -438,9 +447,13 @@ public class VirtualInventory {
int currentAmount = currentStack == null ? 0 : currentStack.getAmount(); int currentAmount = currentStack == null ? 0 : currentStack.getAmount();
int maxStackSize = getMaxStackSize(slot, itemStack.getMaxStackSize()); int maxStackSize = getMaxStackSize(slot, itemStack.getMaxStackSize());
if (currentAmount < maxStackSize) { if (currentAmount < maxStackSize) {
ItemStack newItemStack = itemStack.clone(); int additionalAmount = itemStack.getAmount();
newItemStack.setAmount(min(currentAmount + itemStack.getAmount(), maxStackSize)); int newAmount = min(currentAmount + additionalAmount, maxStackSize);
ItemStack newItemStack = itemStack.clone();
newItemStack.setAmount(newAmount);
if (updateReason != UpdateReason.SUPPRESSED) {
ItemUpdateEvent event = callPreUpdateEvent(updateReason, slot, currentStack, newItemStack); ItemUpdateEvent event = callPreUpdateEvent(updateReason, slot, currentStack, newItemStack);
if (!event.isCancelled()) { if (!event.isCancelled()) {
newItemStack = event.getNewItemStack(); newItemStack = event.getNewItemStack();
@ -449,8 +462,13 @@ public class VirtualInventory {
callAfterUpdateEvent(updateReason, slot, currentStack, newItemStack); callAfterUpdateEvent(updateReason, slot, currentStack, newItemStack);
int newAmount = newItemStack != null ? newItemStack.getAmount() : 0; int newAmountEvent = newItemStack != null ? newItemStack.getAmount() : 0;
return itemStack.getAmount() - (newAmount - currentAmount); return itemStack.getAmount() - (newAmountEvent - currentAmount);
}
} else {
items[slot] = newItemStack;
notifyWindows();
return additionalAmount - (newAmount - currentAmount);
} }
} }
} }
@ -481,6 +499,7 @@ public class VirtualInventory {
newItemStack = null; newItemStack = null;
} }
if (updateReason != UpdateReason.SUPPRESSED) {
ItemUpdateEvent event = callPreUpdateEvent(updateReason, slot, currentStack, newItemStack); ItemUpdateEvent event = callPreUpdateEvent(updateReason, slot, currentStack, newItemStack);
if (!event.isCancelled()) { if (!event.isCancelled()) {
newItemStack = event.getNewItemStack(); newItemStack = event.getNewItemStack();
@ -491,6 +510,11 @@ public class VirtualInventory {
return newItemStack != null ? newItemStack.getAmount() : 0; return newItemStack != null ? newItemStack.getAmount() : 0;
} }
} else {
items[slot] = newItemStack;
notifyWindows();
return amount;
}
return currentStack.getAmount(); return currentStack.getAmount();
} }
@ -731,6 +755,7 @@ public class VirtualInventory {
newStack.setAmount(amount - take); newStack.setAmount(amount - take);
} else newStack = null; } else newStack = null;
if (updateReason != UpdateReason.SUPPRESSED) {
ItemUpdateEvent event = callPreUpdateEvent(updateReason, index, itemStack, newStack); ItemUpdateEvent event = callPreUpdateEvent(updateReason, index, itemStack, newStack);
if (!event.isCancelled()) { if (!event.isCancelled()) {
newStack = event.getNewItemStack(); newStack = event.getNewItemStack();
@ -738,6 +763,11 @@ public class VirtualInventory {
notifyWindows(); notifyWindows();
callAfterUpdateEvent(updateReason, index, itemStack, newStack); callAfterUpdateEvent(updateReason, index, itemStack, newStack);
return itemStack.getAmount() - (newStack == null ? 0 : newStack.getAmount());
}
} else {
items[index] = newStack;
notifyWindows();
return take; return take;
} }

@ -1,4 +1,10 @@
package de.studiocode.invui.virtualinventory.event; package de.studiocode.invui.virtualinventory.event;
public interface UpdateReason { public interface UpdateReason {
/**
* An {@link UpdateReason} that suppresses all event calls.
*/
UpdateReason SUPPRESSED = new UpdateReason() {};
} }