diff --git a/invui/src/main/java/xyz/xenondevs/invui/inventory/Inventory.java b/invui/src/main/java/xyz/xenondevs/invui/inventory/Inventory.java index 2ffc44a..dc15924 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/inventory/Inventory.java +++ b/invui/src/main/java/xyz/xenondevs/invui/inventory/Inventory.java @@ -50,8 +50,8 @@ import java.util.stream.Stream; public abstract class Inventory { private final Set windows = new HashSet<>(); - private Consumer itemUpdateHandler; - private Consumer inventoryUpdatedHandler; + private Consumer preUpdateHandler; + private Consumer postUpdateHandler; private int guiPriority = 0; /** @@ -178,13 +178,31 @@ public abstract class Inventory { windows.forEach(window -> window.handleInventoryUpdate(this)); } + /** + * Gets the configured pre update handler. + * + * @return The pre update handler + */ + public @Nullable Consumer getPreUpdateHandler() { + return preUpdateHandler; + } + /** * Sets a handler which is called every time something gets updated in the {@link Inventory}. * - * @param itemUpdateHandler The new item update handler + * @param preUpdateHandler The new item update handler */ - public void setPreUpdateHandler(Consumer itemUpdateHandler) { - this.itemUpdateHandler = itemUpdateHandler; + public void setPreUpdateHandler(@NotNull Consumer<@NotNull ItemPreUpdateEvent> preUpdateHandler) { + this.preUpdateHandler = preUpdateHandler; + } + + /** + * Gets the configured post update handler. + * + * @return The post update handler + */ + public @Nullable Consumer<@NotNull ItemPostUpdateEvent> getPostUpdateHandler() { + return postUpdateHandler; } /** @@ -192,27 +210,27 @@ public abstract class Inventory { * * @param inventoryUpdatedHandler The new handler */ - public void setPostUpdateHandler(Consumer inventoryUpdatedHandler) { - this.inventoryUpdatedHandler = inventoryUpdatedHandler; + public void setPostUpdateHandler(@NotNull Consumer<@NotNull ItemPostUpdateEvent> inventoryUpdatedHandler) { + this.postUpdateHandler = inventoryUpdatedHandler; } /** - * Creates an {@link ItemPreUpdateEvent} and calls the {@link #itemUpdateHandler} to handle it. + * Creates an {@link ItemPreUpdateEvent} and calls the {@link #preUpdateHandler} to handle it. * * @param updateReason The {@link UpdateReason}. * @param slot The slot of the affected {@link ItemStack}. * @param previousItemStack The {@link ItemStack} that was previously on that slot. * @param newItemStack The {@link ItemStack} that will be on that slot. - * @return The {@link ItemPreUpdateEvent} after it has been handled by the {@link #itemUpdateHandler}. + * @return The {@link ItemPreUpdateEvent} after it has been handled by the {@link #preUpdateHandler}. */ public ItemPreUpdateEvent 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"); ItemPreUpdateEvent event = new ItemPreUpdateEvent(this, slot, updateReason, previousItemStack, newItemStack); - if (itemUpdateHandler != null) { + if (preUpdateHandler != null) { try { - itemUpdateHandler.accept(event); + preUpdateHandler.accept(event); } catch (Throwable t) { InvUI.getInstance().getLogger().log(Level.SEVERE, "An exception occurred while handling an inventory event", t); } @@ -221,7 +239,7 @@ public abstract class Inventory { } /** - * Creates an {@link ItemPostUpdateEvent} and calls the {@link #inventoryUpdatedHandler} to handle it. + * Creates an {@link ItemPostUpdateEvent} and calls the {@link #postUpdateHandler} to handle it. * * @param updateReason The {@link UpdateReason}. * @param slot The slot of the affected {@link ItemStack}. @@ -233,9 +251,9 @@ public abstract class Inventory { throw new IllegalArgumentException("Cannot call InventoryUpdatedEvent with UpdateReason.SUPPRESSED"); ItemPostUpdateEvent event = new ItemPostUpdateEvent(this, slot, updateReason, previousItemStack, newItemStack); - if (inventoryUpdatedHandler != null) { + if (postUpdateHandler != null) { try { - inventoryUpdatedHandler.accept(event); + postUpdateHandler.accept(event); } catch (Throwable t) { InvUI.getInstance().getLogger().log(Level.SEVERE, "An exception occurred while handling an inventory event", t); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/inventory/VirtualInventory.java b/invui/src/main/java/xyz/xenondevs/invui/inventory/VirtualInventory.java index 15b6d00..5191cd2 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/inventory/VirtualInventory.java +++ b/invui/src/main/java/xyz/xenondevs/invui/inventory/VirtualInventory.java @@ -13,6 +13,7 @@ import java.util.UUID; /** * A serializable {@link Inventory} implementation that is identified by a {@link UUID} and backed by a simple {@link ItemStack} array. + * * @see VirtualInventoryManager */ public class VirtualInventory extends Inventory { @@ -25,9 +26,9 @@ public class VirtualInventory extends Inventory { /** * Constructs a new {@link VirtualInventory} * - * @param uuid The {@link UUID} of this {@link VirtualInventory}. Can be null, only used for serialization. - * @param size The amount of slots this {@link VirtualInventory} has. - * @param items A predefined array of content. Can be null. Will not get copied! + * @param uuid The {@link UUID} of this {@link VirtualInventory}. Can be null, only used for serialization. + * @param size The amount of slots this {@link VirtualInventory} has. + * @param items A predefined array of content. Can be null. Will not get copied! * @param maxStackSizes An array of maximum allowed stack sizes for each slot in the {@link VirtualInventory}. Can be null for 64. * @throws IllegalArgumentException If the given size does not match the length of the items array or the length of the stackSizes array. */ @@ -52,8 +53,8 @@ public class VirtualInventory extends Inventory { /** * Constructs a new {@link VirtualInventory} * - * @param size The amount of slots this {@link VirtualInventory} has. - * @param items A predefined array of content. Can be null. Will not get copied! + * @param size The amount of slots this {@link VirtualInventory} has. + * @param items A predefined array of content. Can be null. Will not get copied! * @param maxStackSizes An array of maximum allowed stack sizes for each slot in the {@link VirtualInventory}. Can be null for 64. */ public VirtualInventory(int size, @Nullable ItemStack @Nullable [] items, int @Nullable [] maxStackSizes) { @@ -63,8 +64,8 @@ public class VirtualInventory extends Inventory { /** * Constructs a new {@link VirtualInventory} * - * @param uuid The {@link UUID} of this {@link VirtualInventory}. Can be null, only used for serialization. - * @param items A predefined array of content. Will not get copied! + * @param uuid The {@link UUID} of this {@link VirtualInventory}. Can be null, only used for serialization. + * @param items A predefined array of content. Will not get copied! * @param maxStackSizes An array of maximum allowed stack sizes for each slot in the {@link VirtualInventory}. Can be null for 64. */ public VirtualInventory(@Nullable UUID uuid, @Nullable ItemStack @NotNull [] items, int @Nullable [] maxStackSizes) { @@ -74,7 +75,7 @@ public class VirtualInventory extends Inventory { /** * Constructs a new {@link VirtualInventory} * - * @param items A predefined array of content. Will not get copied! + * @param items A predefined array of content. Will not get copied! * @param maxStackSizes An array of maximum allowed stack sizes for each slot in the {@link VirtualInventory}. Can be null for 64. */ public VirtualInventory(@Nullable ItemStack @NotNull [] items, int @Nullable [] maxStackSizes) { @@ -103,7 +104,7 @@ public class VirtualInventory extends Inventory { /** * Constructs a new {@link VirtualInventory} * - * @param uuid The {@link UUID} of this {@link VirtualInventory}. Can be null, only used for serialization. + * @param uuid The {@link UUID} of this {@link VirtualInventory}. Can be null, only used for serialization. * @param maxStackSizes An array of maximum allowed stack sizes for each slot in the {@link VirtualInventory}. */ public VirtualInventory(@Nullable UUID uuid, int @NotNull [] maxStackSizes) { @@ -138,6 +139,18 @@ public class VirtualInventory extends Inventory { this(null, size, null, null); } + /** + * Creates a copy of the given {@link VirtualInventory}. + * + * @param inventory The {@link VirtualInventory} to copy. + */ + public VirtualInventory(VirtualInventory inventory) { + this(inventory.uuid, inventory.size, ItemUtils.clone(inventory.items), inventory.maxStackSizes.clone()); + setGuiPriority(inventory.getGuiPriority()); + setPreUpdateHandler(inventory.getPreUpdateHandler()); + setPostUpdateHandler(inventory.getPostUpdateHandler()); + } + /** * Deserializes a {@link VirtualInventory} from a byte array. *