Improved VirtualInventory events

This commit is contained in:
NichtStudioCode 2022-06-25 11:26:53 +02:00
parent 632341ad74
commit 3bb3873dc0
3 changed files with 19 additions and 21 deletions

@ -6,12 +6,10 @@ import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
// TODO: better solution?
/** /**
* An event that is called after the {@link VirtualInventory} has been updated. * An event that is called after the {@link VirtualInventory} has been updated.
*/ */
public class InventoryUpdatedEvent extends ItemUpdateEvent { public class InventoryUpdatedEvent extends UpdateEvent {
/** /**
* Creates a new {@link ItemUpdateEvent}. * Creates a new {@link ItemUpdateEvent}.

@ -11,6 +11,8 @@ import org.jetbrains.annotations.Nullable;
*/ */
public class ItemUpdateEvent extends UpdateEvent { public class ItemUpdateEvent extends UpdateEvent {
private boolean cancelled;
/** /**
* Creates a new {@link ItemUpdateEvent}. * Creates a new {@link ItemUpdateEvent}.
* *
@ -29,7 +31,16 @@ public class ItemUpdateEvent extends UpdateEvent {
super(virtualInventory, slot, updateReason, previousItemStack, newItemStack); super(virtualInventory, slot, updateReason, previousItemStack, newItemStack);
} }
private boolean cancelled; /**
* Change the {@link ItemStack} that will appear in the {@link VirtualInventory}
* to a different one.
*
* @param newItemStack The {@link ItemStack} to appear in the {@link VirtualInventory}
* if the {@link ItemUpdateEvent} is not cancelled.
*/
public void setNewItemStack(@Nullable ItemStack newItemStack) {
this.newItemStack = newItemStack;
}
/** /**
* Gets the cancellation state of this event. * Gets the cancellation state of this event.

@ -9,10 +9,10 @@ import org.jetbrains.annotations.Nullable;
abstract class UpdateEvent { abstract class UpdateEvent {
private final VirtualInventory virtualInventory; private final VirtualInventory virtualInventory;
private final ItemStack previousItemStack;
private final UpdateReason updateReason; private final UpdateReason updateReason;
private final int slot; private final int slot;
private ItemStack newItemStack; private final ItemStack previousItemStack;
protected ItemStack newItemStack;
/** /**
* Creates a new {@link ItemUpdateEvent}. * Creates a new {@link ItemUpdateEvent}.
@ -32,8 +32,8 @@ abstract class UpdateEvent {
this.virtualInventory = virtualInventory; this.virtualInventory = virtualInventory;
this.slot = slot; this.slot = slot;
this.updateReason = updateReason; this.updateReason = updateReason;
this.previousItemStack = previousItemStack; this.previousItemStack = previousItemStack != null ? previousItemStack.clone() : null;
this.newItemStack = newItemStack; this.newItemStack = newItemStack != null ? newItemStack.clone() : null;
} }
/** /**
@ -55,7 +55,7 @@ abstract class UpdateEvent {
} }
/** /**
* Gets the {@link ItemStack} that was there previously. * Gets a clone of the {@link ItemStack} that was there previously.
* *
* @return The {@link ItemStack} * @return The {@link ItemStack}
*/ */
@ -64,7 +64,7 @@ abstract class UpdateEvent {
} }
/** /**
* The new {@link ItemStack} that will be there if the event isn't cancelled. * Gets clone of the new {@link ItemStack} that will be there if the event isn't cancelled.
* *
* @return The new {@link ItemStack} * @return The new {@link ItemStack}
*/ */
@ -72,17 +72,6 @@ abstract class UpdateEvent {
return newItemStack; return newItemStack;
} }
/**
* Change the {@link ItemStack} that will appear in the {@link VirtualInventory}
* to a different one.
*
* @param newItemStack The {@link ItemStack} to appear in the {@link VirtualInventory}
* if the {@link ItemUpdateEvent} is not cancelled.
*/
public void setNewItemStack(@Nullable ItemStack newItemStack) {
this.newItemStack = newItemStack;
}
/** /**
* Gets the slot that is affected. * Gets the slot that is affected.
* *