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}
*/
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);
if (itemUpdateHandler != null) {
try {
@ -337,9 +340,11 @@ public class VirtualInventory {
* @param slot The slot of the affected {@link ItemStack}
* @param previousItemStack The {@link ItemStack} that was on that slot previously.
* @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);
if (inventoryUpdatedHandler != null) {
try {
@ -348,7 +353,6 @@ public class VirtualInventory {
e.printStackTrace();
}
}
return event;
}
/**
@ -395,6 +399,10 @@ public class VirtualInventory {
* @return If the action was successful
*/
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];
ItemUpdateEvent event = callPreUpdateEvent(updateReason, slot, previousStack, itemStack);
if (!event.isCancelled()) {
@ -405,6 +413,7 @@ public class VirtualInventory {
}
return false;
}
}
/**
* 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 maxStackSize = getMaxStackSize(slot, itemStack.getMaxStackSize());
if (currentAmount < maxStackSize) {
ItemStack newItemStack = itemStack.clone();
newItemStack.setAmount(min(currentAmount + itemStack.getAmount(), maxStackSize));
int additionalAmount = itemStack.getAmount();
int newAmount = min(currentAmount + additionalAmount, maxStackSize);
ItemStack newItemStack = itemStack.clone();
newItemStack.setAmount(newAmount);
if (updateReason != UpdateReason.SUPPRESSED) {
ItemUpdateEvent event = callPreUpdateEvent(updateReason, slot, currentStack, newItemStack);
if (!event.isCancelled()) {
newItemStack = event.getNewItemStack();
@ -449,8 +462,13 @@ public class VirtualInventory {
callAfterUpdateEvent(updateReason, slot, currentStack, newItemStack);
int newAmount = newItemStack != null ? newItemStack.getAmount() : 0;
return itemStack.getAmount() - (newAmount - currentAmount);
int newAmountEvent = newItemStack != null ? newItemStack.getAmount() : 0;
return itemStack.getAmount() - (newAmountEvent - currentAmount);
}
} else {
items[slot] = newItemStack;
notifyWindows();
return additionalAmount - (newAmount - currentAmount);
}
}
}
@ -481,6 +499,7 @@ public class VirtualInventory {
newItemStack = null;
}
if (updateReason != UpdateReason.SUPPRESSED) {
ItemUpdateEvent event = callPreUpdateEvent(updateReason, slot, currentStack, newItemStack);
if (!event.isCancelled()) {
newItemStack = event.getNewItemStack();
@ -491,6 +510,11 @@ public class VirtualInventory {
return newItemStack != null ? newItemStack.getAmount() : 0;
}
} else {
items[slot] = newItemStack;
notifyWindows();
return amount;
}
return currentStack.getAmount();
}
@ -731,6 +755,7 @@ public class VirtualInventory {
newStack.setAmount(amount - take);
} else newStack = null;
if (updateReason != UpdateReason.SUPPRESSED) {
ItemUpdateEvent event = callPreUpdateEvent(updateReason, index, itemStack, newStack);
if (!event.isCancelled()) {
newStack = event.getNewItemStack();
@ -738,6 +763,11 @@ public class VirtualInventory {
notifyWindows();
callAfterUpdateEvent(updateReason, index, itemStack, newStack);
return itemStack.getAmount() - (newStack == null ? 0 : newStack.getAmount());
}
} else {
items[index] = newStack;
notifyWindows();
return take;
}

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