VirtualInventory copy constructor

This commit is contained in:
NichtStudioCode 2023-04-13 19:30:18 +02:00
parent c7596f8bc1
commit ae20d680e0
2 changed files with 54 additions and 23 deletions

@ -50,8 +50,8 @@ import java.util.stream.Stream;
public abstract class Inventory {
private final Set<AbstractWindow> windows = new HashSet<>();
private Consumer<ItemPreUpdateEvent> itemUpdateHandler;
private Consumer<ItemPostUpdateEvent> inventoryUpdatedHandler;
private Consumer<ItemPreUpdateEvent> preUpdateHandler;
private Consumer<ItemPostUpdateEvent> 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<ItemPreUpdateEvent> 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<ItemPreUpdateEvent> 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<ItemPostUpdateEvent> 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);
}

@ -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.
*