diff --git a/src/main/java/net/theevilreaper/aves/inventory/GlobalInventoryBuilder.java b/src/main/java/net/theevilreaper/aves/inventory/GlobalInventoryBuilder.java index 562948d3..e99d82a3 100644 --- a/src/main/java/net/theevilreaper/aves/inventory/GlobalInventoryBuilder.java +++ b/src/main/java/net/theevilreaper/aves/inventory/GlobalInventoryBuilder.java @@ -1,18 +1,16 @@ package net.theevilreaper.aves.inventory; -import net.theevilreaper.aves.inventory.holder.InventoryHolderImpl; import net.kyori.adventure.text.Component; import net.minestom.server.inventory.Inventory; import net.minestom.server.inventory.InventoryType; import net.minestom.server.item.ItemStack; -import net.minestom.server.item.Material; import org.jetbrains.annotations.NotNull; import java.util.Locale; /** * The {@link GlobalInventoryBuilder} builds an inventory which can be used in a global context. - * That means that the inventory is related to all player's on the server and not bound to a single player. + * That means that the inventory is related to all players on the server and not bound to a single player. * * @author Patrick Zdarsky / Rxcki * @version 1.0.0 @@ -33,7 +31,6 @@ public class GlobalInventoryBuilder extends BaseInventoryBuilderImpl { public GlobalInventoryBuilder(@NotNull Component title, @NotNull InventoryType type) { super(type); this.titleComponent = title; - this.holder = new InventoryHolderImpl(this); } /** @@ -86,22 +83,20 @@ protected void updateInventory() { } /** - * Applies the data layout to the inventory, if the specific layout is set. + * Applies the data layout to the inventory if the specific layout is set. */ @Override protected void applyDataLayout() { + if (getDataLayout() == null) return; synchronized (this) { - if (getDataLayout() == null) return; - LOGGER.info("Applying data layouts"); + LOGGER.debug("Applying data layouts"); ItemStack[] contents = inventory.getItemStacks(); getDataLayout().applyLayout(contents, null); for (int i = 0; i < contents.length; i++) { - if (contents[i] == null) { - this.inventory.setItemStack(i, ItemStack.AIR); - continue; + ItemStack stack = contents[i]; + if (stack != null && !stack.isAir()) { + this.inventory.setItemStack(i, stack); } - if (contents[i].material() == Material.AIR) continue; - this.inventory.setItemStack(i, contents[i]); } this.dataLayoutValid = true; updateViewer(inventory); diff --git a/src/main/java/net/theevilreaper/aves/inventory/GlobalTranslatedInventoryBuilder.java b/src/main/java/net/theevilreaper/aves/inventory/GlobalTranslatedInventoryBuilder.java index bf9dfab6..481a72e4 100644 --- a/src/main/java/net/theevilreaper/aves/inventory/GlobalTranslatedInventoryBuilder.java +++ b/src/main/java/net/theevilreaper/aves/inventory/GlobalTranslatedInventoryBuilder.java @@ -1,13 +1,13 @@ package net.theevilreaper.aves.inventory; import net.kyori.adventure.text.Component; +import net.minestom.server.item.ItemStack; import net.theevilreaper.aves.i18n.TextData; import net.theevilreaper.aves.inventory.holder.InventoryHolderImpl; import net.kyori.adventure.translation.GlobalTranslator; import net.minestom.server.entity.Player; import net.minestom.server.inventory.Inventory; import net.minestom.server.inventory.InventoryType; -import net.minestom.server.item.Material; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -91,18 +91,18 @@ protected void updateInventory() { @Override protected void applyDataLayout() { + if (getDataLayout() == null) return; + LOGGER.debug("Applying data layout"); synchronized (this) { - if (getDataLayout() != null) { - LOGGER.info("Applying data layout"); - for (var entry : inventoryTranslatedObjectCache.entrySet()) { - var contents = entry.getValue().getItemStacks(); - getDataLayout().applyLayout(contents, entry.getKey()); - for (int i = 0; i < contents.length; i++) { - if (contents[i].material() == Material.AIR) continue; - entry.getValue().setItemStack(i, contents[i]); - entry.getValue().update(); - } + for (var entry : inventoryTranslatedObjectCache.entrySet()) { + var contents = entry.getValue().getItemStacks(); + getDataLayout().applyLayout(contents, entry.getKey()); + for (int i = 0; i < contents.length; i++) { + ItemStack stack = contents[i]; + if (stack.isAir()) continue; + entry.getValue().setItemStack(i, stack); } + entry.getValue().update(); } } } diff --git a/src/main/java/net/theevilreaper/aves/inventory/InventoryBuilder.java b/src/main/java/net/theevilreaper/aves/inventory/InventoryBuilder.java index a9318cc5..a39ae175 100644 --- a/src/main/java/net/theevilreaper/aves/inventory/InventoryBuilder.java +++ b/src/main/java/net/theevilreaper/aves/inventory/InventoryBuilder.java @@ -10,11 +10,11 @@ import net.minestom.server.inventory.click.Click; import net.minestom.server.inventory.click.ClickType; import net.minestom.server.item.ItemStack; -import net.minestom.server.item.Material; import net.theevilreaper.aves.inventory.click.ClickHolder; import net.theevilreaper.aves.inventory.function.CloseFunction; import net.theevilreaper.aves.inventory.function.InventoryClick; import net.theevilreaper.aves.inventory.function.OpenFunction; +import net.theevilreaper.aves.inventory.layout.InventoryLayout; import net.theevilreaper.aves.inventory.slot.EmptySlot; import net.theevilreaper.aves.inventory.slot.ISlot; import net.theevilreaper.aves.inventory.util.InventoryConstants; @@ -31,7 +31,7 @@ * The {@link InventoryBuilder} is the base class which contains the necessary methods to create different context implementations for an inventory. * * @author Patrick Zdarsky / Rxcki - * @version 1.3.0 + * @version 1.3.1 * @since 1.0.12 */ @SuppressWarnings("java:S3252") @@ -39,15 +39,17 @@ public abstract class InventoryBuilder { protected static final Logger LOGGER = LoggerFactory.getLogger(InventoryBuilder.class); protected final InventoryType type; - private InventoryLayout inventoryLayout; - private InventoryLayout dataLayout; - protected boolean inventoryLayoutValid = true; - protected boolean dataLayoutValid = false; + protected volatile boolean inventoryLayoutValid = true; + protected volatile boolean dataLayoutValid = false; + protected volatile boolean dataLayoutPending = false; protected OpenFunction openFunction; protected CloseFunction closeFunction; protected ThrowingFunction dataLayoutFunction; protected InventoryClick inventoryClick; + private InventoryLayout inventoryLayout; + private InventoryLayout dataLayout; + /** * Creates a new instance from the inventory builder with the given size. * @@ -94,11 +96,7 @@ private void acceptClick( @NotNull ItemStack stack, @NotNull Consumer result ) { - if (slot == null) { - result.accept(ClickHolder.noClick()); - return; - } - if (slot instanceof EmptySlot) { + if (slot == null || slot instanceof EmptySlot) { result.accept(ClickHolder.noClick()); return; } @@ -108,7 +106,7 @@ private void acceptClick( /** * Set a new reference to the data layout * - * @param dataLayout The {@link InventoryLayoutImpl} to set + * @param dataLayout The {@link InventoryLayout} to set */ public void setDataLayoutFunction(ThrowingFunction dataLayout) { this.dataLayoutFunction = dataLayout; @@ -167,9 +165,9 @@ public void invalidateLayout() { public void invalidateDataLayout() { if (isOpen()) { retrieveDataLayout(); - LOGGER.info("DataLayout invalidated on open inv, requested data..."); + LOGGER.debug("DataLayout invalidated on open inv, requested data..."); } else { - LOGGER.info("DataLayout invalidated on closed inv"); + LOGGER.debug("DataLayout invalidated on closed inv"); dataLayoutValid = false; } } @@ -197,39 +195,27 @@ protected void handleClose(@NotNull InventoryCloseEvent event) { /** * Updates the given inventory with the content. * - * @param inventory the inventory which should receive the update + * @param inventory the inventory that should receive the update * @param locale the locale for the inventory * @param applyLayout if the layout should be applied */ - protected void updateInventory( - @NotNull Inventory inventory, - Locale locale, - boolean applyLayout - ) { + protected void updateInventory(@NotNull Inventory inventory, Locale locale, boolean applyLayout){ + if (!applyLayout) return; if (this.inventoryLayout == null) { throw new IllegalStateException("Can't update content because the layout is null"); } // Design - if (applyLayout) { - var contents = inventory.getItemStacks(); - inventory.clear(); - getLayout().applyLayout(contents, locale); - this.setItemsInternal(inventory, contents); - LOGGER.info("UpdateInventory applied the InventoryLayout!"); - this.inventoryLayoutValid = true; - } + ItemStack[] contents = inventory.getItemStacks(); + inventory.clear(); + this.inventoryLayout.applyLayout(contents, locale); + this.setItemsInternal(inventory, contents); + LOGGER.debug("UpdateInventory applied the InventoryLayout!"); + this.inventoryLayoutValid = true; - // Values synchronized (this) { if (!dataLayoutValid) { retrieveDataLayout(); - } else { - if (getDataLayout() != null) { - var contents = inventory.getItemStacks(); - getDataLayout().applyLayout(contents, locale); - this.setItemsInternal(inventory, contents); - } } } } @@ -238,28 +224,32 @@ protected void updateInventory( * Set's the given array with the {@link ItemStack}'s into an inventory. * * @param inventory the inventory for the items - * @param contents the array itself which contains all items + * @param contents the array itself that contains all items */ private void setItemsInternal(@NotNull Inventory inventory, @NotNull ItemStack[] contents) { for (int i = 0; i < contents.length; i++) { var contentSlot = contents[i]; - if (contentSlot == null || contentSlot.material() == Material.AIR) continue; - inventory.setItemStack(i, contents[i]); + if (contentSlot == null || contentSlot.isAir()) continue; + inventory.setItemStack(i, contentSlot); } } /** - * Executes the logic to retrieve the {@link InventoryLayoutImpl} which comes from the {@link ThrowingFunction}. + * Executes the logic to retrieve the {@link InventoryLayout} which comes from the {@link ThrowingFunction}. */ protected void retrieveDataLayout() { + if (this.dataLayoutFunction == null) return; + if (dataLayoutPending) return; synchronized (this) { - if (this.dataLayoutFunction == null) return; + this.dataLayoutPending = true; MinecraftServer.getSchedulerManager().scheduleNextTick(() -> { try { this.dataLayout = this.dataLayoutFunction.acceptThrows(this.dataLayout); applyDataLayout(); } catch (Exception exception) { MinecraftServer.getExceptionManager().handleException(exception); + } finally { + this.dataLayoutPending = false; } }); } @@ -328,7 +318,7 @@ public InventoryBuilder setCloseFunction(CloseFunction closeFunction) { } /** - * Set a new instance of the {@link InventoryLayoutImpl} to the builder + * Set a new instance of the {@link InventoryLayout} to the builder * * @param inventoryLayoutImpl The layout to set * @return the current instance of the builder @@ -339,7 +329,7 @@ public InventoryBuilder setLayout(@NotNull InventoryLayout inventoryLayoutImpl) } /** - * Returns the underlying {@link InventoryLayoutImpl}. + * Returns the underlying {@link InventoryLayout}. * * @return the given layout */ @@ -348,7 +338,7 @@ public InventoryBuilder setLayout(@NotNull InventoryLayout inventoryLayoutImpl) } /** - * Get underlying data {@link InventoryLayoutImpl}. + * Get underlying data {@link InventoryLayout}. * * @return the given layout */ diff --git a/src/main/java/net/theevilreaper/aves/inventory/click/ClickHolder.java b/src/main/java/net/theevilreaper/aves/inventory/click/ClickHolder.java index e907cca8..0499d550 100644 --- a/src/main/java/net/theevilreaper/aves/inventory/click/ClickHolder.java +++ b/src/main/java/net/theevilreaper/aves/inventory/click/ClickHolder.java @@ -1,7 +1,6 @@ package net.theevilreaper.aves.inventory.click; import net.minestom.server.inventory.click.Click; -import org.jetbrains.annotations.NotNull; /** * The {@link ClickHolder} interface represents a holder for click actions in the Aves inventory system. @@ -9,7 +8,7 @@ * This allows for flexible handling of click actions within the inventory system. * * @author theEvilReaper - * @version 1.0.0 + * @version 1.0.1 * @since 1.9.0 */ public sealed interface ClickHolder permits ClickHolder.MinestomClick, ClickHolder.CancelClick, ClickHolder.NOPClick { @@ -19,7 +18,7 @@ public sealed interface ClickHolder permits ClickHolder.MinestomClick, ClickHold * * @return the no-click reference */ - static @NotNull ClickHolder noClick() { + static ClickHolder noClick() { return InternalClickRegistry.NOP_CLICK; } @@ -28,7 +27,7 @@ public sealed interface ClickHolder permits ClickHolder.MinestomClick, ClickHold * * @return the cancel-click reference */ - static @NotNull ClickHolder cancelClick() { + static ClickHolder cancelClick() { return InternalClickRegistry.CANCEL_CLICK; } @@ -38,7 +37,7 @@ public sealed interface ClickHolder permits ClickHolder.MinestomClick, ClickHold * @param click the click to wrap * @return the wrapped click holder */ - static @NotNull ClickHolder of(@NotNull Click click) { + static ClickHolder of(Click click) { return new MinestomClick(click); } @@ -50,7 +49,7 @@ public sealed interface ClickHolder permits ClickHolder.MinestomClick, ClickHold * @version 1.0.0 * @since 1.9.0 */ - record MinestomClick(@NotNull Click click) implements ClickHolder { + record MinestomClick(Click click) implements ClickHolder { } /** diff --git a/src/main/java/net/theevilreaper/aves/inventory/click/package-info.java b/src/main/java/net/theevilreaper/aves/inventory/click/package-info.java new file mode 100644 index 00000000..acd1a059 --- /dev/null +++ b/src/main/java/net/theevilreaper/aves/inventory/click/package-info.java @@ -0,0 +1,4 @@ +@NotNullByDefault +package net.theevilreaper.aves.inventory.click; + +import org.jetbrains.annotations.NotNullByDefault; \ No newline at end of file diff --git a/src/main/java/net/theevilreaper/aves/inventory/exception/ListenerStateException.java b/src/main/java/net/theevilreaper/aves/inventory/exception/ListenerStateException.java index d62b2953..b3d4c7e9 100644 --- a/src/main/java/net/theevilreaper/aves/inventory/exception/ListenerStateException.java +++ b/src/main/java/net/theevilreaper/aves/inventory/exception/ListenerStateException.java @@ -1,13 +1,11 @@ package net.theevilreaper.aves.inventory.exception; -import org.jetbrains.annotations.NotNull; - /** * The {@link ListenerStateException} is a custom exception which is thrown when a listener is in an invalid state. * Its former used in the in inventory listener handling to indicate that the listener is not in a valid state to process an event. * * @author theEvilReaper - * @version 1.0.0 + * @version 1.0.1 * @since 1.0.12 */ public final class ListenerStateException extends IllegalStateException { @@ -17,7 +15,7 @@ public final class ListenerStateException extends IllegalStateException { * * @param message the message for the exception */ - public ListenerStateException(@NotNull String message) { + public ListenerStateException(String message) { super(message); } @@ -27,7 +25,7 @@ public ListenerStateException(@NotNull String message) { * @param message the message for the exception * @param cause the cause of the exception */ - public ListenerStateException(@NotNull String message, @NotNull Throwable cause) { + public ListenerStateException(String message, Throwable cause) { super(message, cause); } @@ -36,7 +34,7 @@ public ListenerStateException(@NotNull String message, @NotNull Throwable cause) * * @param cause the cause of the exception */ - public ListenerStateException(@NotNull Throwable cause) { + public ListenerStateException(Throwable cause) { super(cause); } } diff --git a/src/main/java/net/theevilreaper/aves/inventory/exception/package-info.java b/src/main/java/net/theevilreaper/aves/inventory/exception/package-info.java new file mode 100644 index 00000000..4e94b3b1 --- /dev/null +++ b/src/main/java/net/theevilreaper/aves/inventory/exception/package-info.java @@ -0,0 +1,4 @@ +@NotNullByDefault +package net.theevilreaper.aves.inventory.exception; + +import org.jetbrains.annotations.NotNullByDefault; \ No newline at end of file diff --git a/src/main/java/net/theevilreaper/aves/inventory/holder/InventoryHolder.java b/src/main/java/net/theevilreaper/aves/inventory/holder/InventoryHolder.java index 656b4117..327f3932 100644 --- a/src/main/java/net/theevilreaper/aves/inventory/holder/InventoryHolder.java +++ b/src/main/java/net/theevilreaper/aves/inventory/holder/InventoryHolder.java @@ -1,19 +1,20 @@ package net.theevilreaper.aves.inventory.holder; import net.minestom.server.inventory.Inventory; -import org.jetbrains.annotations.NotNull; /** * Represents a wrapper objects which holds an instance from an inventory + * * @author theEvilReaper - * @version 1.0.0 + * @version 1.0.1 * @since 1.2.0 */ public interface InventoryHolder { /** * Returns the inventory from the holder + * * @return the underling inventory */ - @NotNull Inventory getInventory(); + Inventory getInventory(); } diff --git a/src/main/java/net/theevilreaper/aves/inventory/holder/InventoryHolderImpl.java b/src/main/java/net/theevilreaper/aves/inventory/holder/InventoryHolderImpl.java index a0feebd4..ab0ded6c 100644 --- a/src/main/java/net/theevilreaper/aves/inventory/holder/InventoryHolderImpl.java +++ b/src/main/java/net/theevilreaper/aves/inventory/holder/InventoryHolderImpl.java @@ -3,7 +3,6 @@ import net.theevilreaper.aves.inventory.InventoryBuilder; import net.minestom.server.inventory.Inventory; import net.theevilreaper.aves.inventory.function.InventoryClick; -import org.jetbrains.annotations.NotNull; /** * This class is the implementation for the {@link InventoryHolder} interface. @@ -12,10 +11,10 @@ * * @param inventoryBuilder the {@link InventoryBuilder} which is bound to the holder * @author theEvilReaper - * @version 1.0.0 + * @version 1.0.1 * @since 1.2.0 */ -public record InventoryHolderImpl(@NotNull InventoryBuilder inventoryBuilder) implements InventoryHolder { +public record InventoryHolderImpl(InventoryBuilder inventoryBuilder) implements InventoryHolder { /** * Returns the {@link Inventory} which is bound a holder reference. @@ -23,7 +22,7 @@ public record InventoryHolderImpl(@NotNull InventoryBuilder inventoryBuilder) im * @return the given {@link Inventory} */ @Override - public @NotNull Inventory getInventory() { + public Inventory getInventory() { return inventoryBuilder.getInventory(); } } diff --git a/src/main/java/net/theevilreaper/aves/inventory/holder/package-info.java b/src/main/java/net/theevilreaper/aves/inventory/holder/package-info.java new file mode 100644 index 00000000..9d2e423b --- /dev/null +++ b/src/main/java/net/theevilreaper/aves/inventory/holder/package-info.java @@ -0,0 +1,4 @@ +@NotNullByDefault +package net.theevilreaper.aves.inventory.holder; + +import org.jetbrains.annotations.NotNullByDefault; \ No newline at end of file diff --git a/src/main/java/net/theevilreaper/aves/inventory/InventoryLayout.java b/src/main/java/net/theevilreaper/aves/inventory/layout/InventoryLayout.java similarity index 79% rename from src/main/java/net/theevilreaper/aves/inventory/InventoryLayout.java rename to src/main/java/net/theevilreaper/aves/inventory/layout/InventoryLayout.java index 475f1864..009277f4 100644 --- a/src/main/java/net/theevilreaper/aves/inventory/InventoryLayout.java +++ b/src/main/java/net/theevilreaper/aves/inventory/layout/InventoryLayout.java @@ -1,4 +1,4 @@ -package net.theevilreaper.aves.inventory; +package net.theevilreaper.aves.inventory.layout; import net.theevilreaper.aves.inventory.function.ApplyLayoutFunction; import net.theevilreaper.aves.inventory.function.InventoryClick; @@ -9,7 +9,6 @@ import net.theevilreaper.aves.inventory.util.InventoryConstants; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Locale; @@ -17,7 +16,7 @@ import static net.theevilreaper.aves.inventory.util.InventoryConstants.CANCEL_CLICK; /** - * The {@link InventoryLayout} contains a bunch of method to set or update the layout of an inventory. + * The {@link InventoryLayout} contains a bunch of methods to set or update the layout of an inventory. * * @author theEvilReaper * @version 1.2.0 @@ -33,7 +32,7 @@ public sealed interface InventoryLayout permits InventoryLayoutImpl { * @return the created reference */ @Contract(value = "_ -> new", pure = true) - static @NotNull InventoryLayout fromType(@NotNull InventoryType type) { + static InventoryLayout fromType(InventoryType type) { return new InventoryLayoutImpl(type); } @@ -44,7 +43,7 @@ public sealed interface InventoryLayout permits InventoryLayoutImpl { * @return the copied reference */ @Contract(value = "_ -> new", pure = true) - static @NotNull InventoryLayout of(@NotNull InventoryLayout layout) { + static InventoryLayout of(InventoryLayout layout) { return new InventoryLayoutImpl((InventoryLayoutImpl) layout); } @@ -53,22 +52,22 @@ public sealed interface InventoryLayout permits InventoryLayoutImpl { * * @param applyLayoutFunction the new implementation to set */ - void setApplyLayoutFunction(@NotNull ApplyLayoutFunction applyLayoutFunction); + void setApplyLayoutFunction(ApplyLayoutFunction applyLayoutFunction); /** * Applies an array of {@link ItemStack} to the layout. * - * @param itemStacks the array which should be applied + * @param itemStacks the array that should be applied */ void applyLayout(ItemStack[] itemStacks); /** * Applies an array of {@link ItemStack} to the layout with a given locale. * - * @param itemStacks the array which should be applied + * @param itemStacks the array that should be applied * @param locale the locale to apply */ - void applyLayout(ItemStack[] itemStacks, Locale locale); + void applyLayout(ItemStack[] itemStacks, @Nullable Locale locale); /** * Set's a single item to one slot with a given listener. @@ -78,8 +77,7 @@ public sealed interface InventoryLayout permits InventoryLayoutImpl { * @param clickEvent the event logic for the slot (can be null) * @return the layout reference */ - @NotNull - default InventoryLayout setItem(int slot, @NotNull ItemStack.Builder itemBuilder, @Nullable InventoryClick clickEvent) { + default InventoryLayout setItem(int slot, ItemStack.Builder itemBuilder, @Nullable InventoryClick clickEvent) { this.setItem(slot, itemBuilder.build(), clickEvent); return this; } @@ -92,7 +90,7 @@ default InventoryLayout setItem(int slot, @NotNull ItemStack.Builder itemBuilder * @param clickEvent the event logic for the slot (can be null) * @return the layout reference */ - @NotNull InventoryLayout setItem(int slot, @NotNull ItemStack itemStack, @Nullable InventoryClick clickEvent); + InventoryLayout setItem(int slot, ItemStack itemStack, @Nullable InventoryClick clickEvent); /** * Set's a single item to one slot with a given listener. @@ -102,7 +100,7 @@ default InventoryLayout setItem(int slot, @NotNull ItemStack.Builder itemBuilder * @param clickEvent the event logic for the slot (can be null) * @return the layout reference */ - @NotNull InventoryLayout setItem(int slot, ISlot iSlot, InventoryClick clickEvent); + InventoryLayout setItem(int slot, ISlot iSlot, InventoryClick clickEvent); /** * Set's a single item to a slot without a listener. @@ -111,7 +109,7 @@ default InventoryLayout setItem(int slot, @NotNull ItemStack.Builder itemBuilder * @param itemStack the {@link ItemStack} for the slot * @return the layout reference */ - default @NotNull InventoryLayout setItem(int slot, ItemStack itemStack) { + default InventoryLayout setItem(int slot, ItemStack itemStack) { return this.setItem(slot, itemStack, CANCEL_CLICK); } @@ -122,7 +120,7 @@ default InventoryLayout setItem(int slot, @NotNull ItemStack.Builder itemBuilder * @param itemBuilder the builder reference which contains the information about the {@link ItemStack} * @return the layout reference */ - default @NotNull InventoryLayout setItem(int slot, @NotNull ItemStack.Builder itemBuilder) { + default InventoryLayout setItem(int slot, ItemStack.Builder itemBuilder) { return this.setItem(slot, itemBuilder.build()); } @@ -133,7 +131,7 @@ default InventoryLayout setItem(int slot, @NotNull ItemStack.Builder itemBuilder * @param slotItem the slot reference * @return the instance from the layout */ - @NotNull InventoryLayout setItem(int slot, @NotNull ISlot slotItem); + InventoryLayout setItem(int slot, ISlot slotItem); /** * Set's a single {@link ItemStack} to each slot which is provided by the array. @@ -142,8 +140,7 @@ default InventoryLayout setItem(int slot, @NotNull ItemStack.Builder itemBuilder * @param stack the {@link ItemStack} which should be set into each slot index * @return the instance from the layout */ - @NotNull - default InventoryLayout setItems(int[] array, @NotNull ItemStack stack) { + default InventoryLayout setItems(int[] array, ItemStack stack) { for (int i = 0; i < array.length; i++) { setItem(array[i], stack); } @@ -159,7 +156,7 @@ default InventoryLayout setItems(int[] array, @NotNull ItemStack stack) { * @param clickEvent the listener for the slot * @return the instance from the layout */ - default @NotNull InventoryLayout setItems(int[] array, @NotNull ItemStack stack, @Nullable InventoryClick clickEvent) { + default InventoryLayout setItems(int[] array, ItemStack stack, @Nullable InventoryClick clickEvent) { for (int i = 0; i < array.length; i++) { setItem(array[i], stack, clickEvent); } @@ -174,7 +171,7 @@ default InventoryLayout setItems(int[] array, @NotNull ItemStack stack) { * @param clickEvent the listener for the slot * @return the instance from the layout */ - default @NotNull InventoryLayout setItems(int[] array, @NotNull ItemStack.Builder itemBuilder, @Nullable InventoryClick clickEvent) { + default InventoryLayout setItems(int[] array, ItemStack.Builder itemBuilder, @Nullable InventoryClick clickEvent) { for (int i = 0; i < array.length; i++) { setItem(array[i], itemBuilder, clickEvent); } @@ -188,7 +185,7 @@ default InventoryLayout setItems(int[] array, @NotNull ItemStack stack) { * @param itemBuilder the builder reference which contains the information about the {@link ItemStack} * @return the instance from the layout */ - default @NotNull InventoryLayout setItems(int[] array, ItemStack.Builder itemBuilder) { + default InventoryLayout setItems(int[] array, ItemStack.Builder itemBuilder) { for (int i = 0; i < array.length; i++) { setItem(array[i], itemBuilder); } @@ -202,7 +199,7 @@ default InventoryLayout setItems(int[] array, @NotNull ItemStack stack) { * @param slot the {@link ISlot} which should be set into each slot index * @return the instance from the layout */ - default @NotNull InventoryLayout setItems(int[] array, @NotNull ISlot slot) { + default InventoryLayout setItems(int[] array, ISlot slot) { for (int i = 0; i < array.length; i++) { setItem(array[i], slot); } @@ -215,7 +212,7 @@ default InventoryLayout setItems(int[] array, @NotNull ItemStack stack) { * @param slot the slot to blank * @return the instance from the layout */ - @NotNull InventoryLayout blank(int slot); + InventoryLayout blank(int slot); /** * Marks an all-given slot with a fake slot object. @@ -223,7 +220,6 @@ default InventoryLayout setItems(int[] array, @NotNull ItemStack stack) { * @param slots the slots to mark * @return the instance from the layout */ - @NotNull default InventoryLayout blank(int... slots) { for (int i = 0; i < slots.length; i++) { this.blank(slots[i]); @@ -237,7 +233,7 @@ default InventoryLayout blank(int... slots) { * @param slot The index to remove the slot * @return the instance from the layout */ - @NotNull InventoryLayout clear(int slot); + InventoryLayout clear(int slot); /** * Removes all slot object that stands in the given array. @@ -245,7 +241,6 @@ default InventoryLayout blank(int... slots) { * @param slots The array which contains all slots to remove * @return the instance from the layout */ - @NotNull default InventoryLayout clear(int... slots) { for (int i = 0; i < slots.length; i++) { this.clear(slots[i]); @@ -261,7 +256,7 @@ default InventoryLayout clear(int... slots) { * @param listener the listener to set * @return the instance from the layout */ - @NotNull InventoryLayout update(int index, @Nullable InventoryClick listener); + InventoryLayout update(int index, @Nullable InventoryClick listener); /** * Updates the given ItemStack from a slot. @@ -271,17 +266,17 @@ default InventoryLayout clear(int... slots) { * @param stack the {@link ItemStack} to set * @return the instance from the layout */ - @NotNull InventoryLayout update(int index, @Nullable ItemStack stack); + InventoryLayout update(int index, @Nullable ItemStack stack); /** * Updates an {@link ISlot} at a given index with a new {@link ItemStack}. * * @param index the slot index * @param stack the new stack tot set - * @param click the click listener for the slot + * @param click click listener for the slot * @return the instance from the layout */ - @NotNull InventoryLayout update(int index, @NotNull ItemStack stack, @Nullable InventoryClick click); + InventoryLayout update(int index, ItemStack stack, @Nullable InventoryClick click); /** * Returns a slot from the content array by a specific index. @@ -289,8 +284,7 @@ default InventoryLayout clear(int... slots) { * @param index the index to get the slot * @return the fetched slot otherwise null */ - @Nullable - default ISlot getSlot(int index) { + default @Nullable ISlot getSlot(int index) { Check.argCondition(index < 0 || index > getContents().length, "The given index does not fit into the array (0, " + getContents().length + ")"); return getContents()[index]; @@ -302,7 +296,7 @@ default ISlot getSlot(int index) { * @param index the index to remove the slot * @return the instance from the layout */ - @NotNull InventoryLayout remove(int index); + InventoryLayout remove(int index); /** * Removes a list of slots from the layout. @@ -310,7 +304,7 @@ default ISlot getSlot(int index) { * @param indices the array which contains the indices to remove * @return the instance from the layout */ - default @NotNull InventoryLayout remove(int @NotNull ... indices) { + default InventoryLayout remove(int ... indices) { for (int i = 0; i < indices.length; i++) { remove(indices[i]); } @@ -322,7 +316,7 @@ default ISlot getSlot(int index) { * * @return the underlying array */ - @NotNull ISlot[] getContents(); + ISlot[] getContents(); /** * Returns the size from the layout. @@ -338,5 +332,5 @@ default int getSize() { * * @return the underlying instance from the interface */ - @NotNull ApplyLayoutFunction getApplyLayoutFunction(); + ApplyLayoutFunction getApplyLayoutFunction(); } diff --git a/src/main/java/net/theevilreaper/aves/inventory/InventoryLayoutImpl.java b/src/main/java/net/theevilreaper/aves/inventory/layout/InventoryLayoutImpl.java similarity index 61% rename from src/main/java/net/theevilreaper/aves/inventory/InventoryLayoutImpl.java rename to src/main/java/net/theevilreaper/aves/inventory/layout/InventoryLayoutImpl.java index 6c8701eb..d3093a4d 100644 --- a/src/main/java/net/theevilreaper/aves/inventory/InventoryLayoutImpl.java +++ b/src/main/java/net/theevilreaper/aves/inventory/layout/InventoryLayoutImpl.java @@ -1,18 +1,17 @@ -package net.theevilreaper.aves.inventory; +package net.theevilreaper.aves.inventory.layout; +import net.theevilreaper.aves.inventory.InventorySlot; import net.theevilreaper.aves.inventory.function.ApplyLayoutFunction; import net.theevilreaper.aves.inventory.function.DefaultApplyLayoutFunction; import net.theevilreaper.aves.inventory.function.InventoryClick; -import net.theevilreaper.aves.inventory.slot.EmptySlot; import net.theevilreaper.aves.inventory.slot.ISlot; import net.theevilreaper.aves.inventory.slot.TranslatedSlot; import net.minestom.server.inventory.InventoryType; import net.minestom.server.item.ItemStack; import net.minestom.server.utils.validate.Check; -import net.theevilreaper.aves.inventory.util.InventoryConstants; import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Arrays; @@ -25,24 +24,28 @@ * Represents a layout which contains all items for an inventory. * * @author Patrick Zdarsky / Rxcki - * @version 1.0.2 + * @version 1.0.3 * @since 1.0.12 */ @SuppressWarnings("java:S3776") @ApiStatus.Internal public final class InventoryLayoutImpl implements InventoryLayout { + private static final Logger LOGGER = LoggerFactory.getLogger(InventoryLayoutImpl.class); + private static final String INDEX_ERROR = "The given slot index is out of range"; - private ApplyLayoutFunction applyLayoutFunction; private final ISlot[] contents; + private ApplyLayoutFunction applyLayoutFunction; + /** * Creates a new instance from the {@link InventoryLayoutImpl}. * * @param type the given size for the layout */ - InventoryLayoutImpl(@NotNull InventoryType type) { + InventoryLayoutImpl(InventoryType type) { this.contents = new ISlot[type.getSize()]; + Arrays.fill(this.contents, BLANK_SLOT); this.applyLayoutFunction = new DefaultApplyLayoutFunction(this.contents); } @@ -51,15 +54,14 @@ public final class InventoryLayoutImpl implements InventoryLayout { * * @param layout the layout to copy */ - InventoryLayoutImpl(@NotNull InventoryLayoutImpl layout) { + InventoryLayoutImpl(InventoryLayoutImpl layout) { this.contents = new ISlot[layout.getContents().length]; for (int i = 0; i < layout.getContents().length; i++) { - var slotEntry = layout.getContents()[i]; + ISlot slotEntry = layout.getContents()[i]; switch (slotEntry) { case InventorySlot inventorySlot -> this.contents[i] = InventorySlot.of(inventorySlot); case TranslatedSlot translatedSlot -> this.contents[i] = TranslatedSlot.of(translatedSlot); - case EmptySlot emptySlot -> this.contents[i] = emptySlot; - default -> LoggerFactory.getLogger("Aves").info("Found a slot which can not be converted"); + default -> LOGGER.info("Slot: " + slotEntry + "is unknown and can't be converted"); } } this.applyLayoutFunction = layout.getApplyLayoutFunction(); @@ -71,116 +73,114 @@ public final class InventoryLayoutImpl implements InventoryLayout { * @param applyLayoutFunction the new implementation to set */ @Override - public void setApplyLayoutFunction(@NotNull ApplyLayoutFunction applyLayoutFunction) { + public void setApplyLayoutFunction(ApplyLayoutFunction applyLayoutFunction) { this.applyLayoutFunction = applyLayoutFunction; } /** * Applies a given {@link ItemStack} array to the layout. * - * @param itemStacks the array which should be applied + * @param itemStacks the array that should be applied */ @Override public void applyLayout(ItemStack[] itemStacks) { this.applyLayoutFunction.applyLayout(itemStacks, null); } + /** + * {@inheritDoc} + */ @Override - public void applyLayout(ItemStack[] itemStacks, Locale locale) { + public void applyLayout(ItemStack[] itemStacks, @Nullable Locale locale) { this.applyLayoutFunction.applyLayout(itemStacks, locale); } + /** + * {@inheritDoc} + */ @Override - public @NotNull InventoryLayoutImpl setItem(int slot, ItemStack.@NotNull Builder itemBuilder, @Nullable InventoryClick clickEvent) { + public InventoryLayoutImpl setItem(int slot, ItemStack.Builder itemBuilder, @Nullable InventoryClick clickEvent) { Check.argCondition(slot < 0 || slot > contents.length, INDEX_ERROR); this.contents[slot] = new InventorySlot(itemBuilder, clickEvent); return this; } + /** + * {@inheritDoc} + */ @Override - public @NotNull InventoryLayoutImpl setItem(int slot, @NotNull ItemStack itemStack, InventoryClick clickEvent) { + public InventoryLayoutImpl setItem(int slot, ItemStack itemStack, @Nullable InventoryClick clickEvent) { Check.argCondition(slot < 0 || slot > contents.length, INDEX_ERROR); this.contents[slot] = new InventorySlot(itemStack, clickEvent); return this; } + /** + * {@inheritDoc} + */ @Override - public @NotNull InventoryLayoutImpl setItem(int slot, ISlot iSlot, InventoryClick clickEvent) { + public InventoryLayoutImpl setItem(int slot, ISlot iSlot, InventoryClick clickEvent) { Check.argCondition(slot < 0 || slot > contents.length, INDEX_ERROR); iSlot.setClick(clickEvent); contents[slot] = iSlot; return this; } + /** + * {@inheritDoc} + */ @Override - public @NotNull InventoryLayoutImpl setItem(int slot, @NotNull ISlot slotItem) { + public InventoryLayoutImpl setItem(int slot, ISlot slotItem) { Check.argCondition(slot < 0 || slot > contents.length, INDEX_ERROR); contents[slot] = slotItem; return this; } /** - * Blanks a single slot in the layout. - * - * @param slot The slot to blank + * {@inheritDoc} */ @Override - public @NotNull InventoryLayoutImpl blank(int slot) { + public InventoryLayoutImpl blank(int slot) { Check.argCondition(slot < 0 || slot > contents.length, INDEX_ERROR); this.contents[slot] = BLANK_SLOT; return this; } /** - * Removes the slot object at a given index. - * - * @param slot The index to remove the slot + * {@inheritDoc} */ @Override - public @NotNull InventoryLayoutImpl clear(int slot) { + public InventoryLayoutImpl clear(int slot) { Check.argCondition(slot < 0 || slot > contents.length, INDEX_ERROR); - contents[slot] = null; + contents[slot] = BLANK_SLOT; return this; } /** - * Updates the given listener from a slot. - * If the listener is null the {@link InventoryConstants#CANCEL_CLICK} will set to the slot - * - * @param index The index to get the slot to update - * @param listener The listener to set + * {@inheritDoc} */ @Override - public @NotNull InventoryLayoutImpl update(int index, @Nullable InventoryClick listener) { + public InventoryLayoutImpl update(int index, @Nullable InventoryClick listener) { Check.argCondition(index < 0 || index > contents.length, INDEX_ERROR); contents[index].setClick(listener == null ? CANCEL_CLICK : listener); return this; } /** - * Updates the given ItemStack from a slot. - * If the listener is null the {@link InventoryConstants#CANCEL_CLICK} will set to the slot - * - * @param index The index to get the slot to update - * @param stack The {@link ItemStack} to set + * {@inheritDoc} */ @Override - public @NotNull InventoryLayoutImpl update(int index, @Nullable ItemStack stack) { + public InventoryLayoutImpl update(int index, @Nullable ItemStack stack) { Check.argCondition(index < 0 || index > contents.length, INDEX_ERROR); contents[index].setItemStack(stack); return this; } /** - * Updates an {@link ISlot} at a given index with a new {@link ItemStack}. - * - * @param index the slot index - * @param stack the new stack tot set - * @param click the click listener for the slot - * @return the instance from the layout + * {@inheritDoc} */ @Override - public @NotNull InventoryLayoutImpl update(int index, @NotNull ItemStack stack, @Nullable InventoryClick click) { + public InventoryLayoutImpl update(int index, ItemStack stack, @Nullable InventoryClick click) { Check.argCondition(index < 0 || index > contents.length, INDEX_ERROR); var slot = contents[index]; slot.setItemStack(stack); @@ -189,23 +189,13 @@ public void applyLayout(ItemStack[] itemStacks, Locale locale) { } /** - * Returns a slot from the content array by a specific index. - * - * @param index The index to get the slot - * @return The fetched slot otherwise null + * {@inheritDoc} */ @Override - public @Nullable ISlot getSlot(int index) { + public InventoryLayout remove(int index) { Check.argCondition(index < 0 || index > this.contents.length, "The given index does not fit into the array (0, " + this.contents.length + ")"); - return this.contents[index]; - } - - @Override - public @NotNull InventoryLayout remove(int index) { - Check.argCondition(index < 0 || index > this.contents.length, - "The given index does not fit into the array (0, " + this.contents.length + ")"); - this.contents[index] = null; + this.contents[index] = BLANK_SLOT; return this; } @@ -215,17 +205,15 @@ public void applyLayout(ItemStack[] itemStacks, Locale locale) { * @return the underlying array */ @Override - public @NotNull ISlot[] getContents() { + public ISlot[] getContents() { return contents; } /** - * Returns the given {@link ApplyLayoutFunction} instance. - * - * @return the underlying instance from the interface + * {@inheritDoc} */ @Override - public @NotNull ApplyLayoutFunction getApplyLayoutFunction() { + public ApplyLayoutFunction getApplyLayoutFunction() { return applyLayoutFunction; } @@ -235,12 +223,12 @@ public void applyLayout(ItemStack[] itemStacks, Locale locale) { * @return the textual representation */ @Override - public @NotNull String toString() { + public String toString() { return "InventoryLayout{" + "contents=" + Arrays.toString(contents) + '}'; } /** - * Checks if two layout are equals. + * Checks if two layouts are equals. * * @param o the object to check * @return True when they are equal otherwise false diff --git a/src/main/java/net/theevilreaper/aves/inventory/layout/package-info.java b/src/main/java/net/theevilreaper/aves/inventory/layout/package-info.java new file mode 100644 index 00000000..99d1c5ab --- /dev/null +++ b/src/main/java/net/theevilreaper/aves/inventory/layout/package-info.java @@ -0,0 +1,4 @@ +@NotNullByDefault +package net.theevilreaper.aves.inventory.layout; + +import org.jetbrains.annotations.NotNullByDefault; \ No newline at end of file diff --git a/src/main/java/net/theevilreaper/aves/inventory/pageable/PageableInventory.java b/src/main/java/net/theevilreaper/aves/inventory/pageable/PageableInventory.java index 4fb020fa..377ffcdf 100644 --- a/src/main/java/net/theevilreaper/aves/inventory/pageable/PageableInventory.java +++ b/src/main/java/net/theevilreaper/aves/inventory/pageable/PageableInventory.java @@ -1,6 +1,6 @@ package net.theevilreaper.aves.inventory.pageable; -import net.theevilreaper.aves.inventory.InventoryLayout; +import net.theevilreaper.aves.inventory.layout.InventoryLayout; import net.theevilreaper.aves.inventory.slot.ISlot; import net.minestom.server.entity.Player; import net.minestom.server.inventory.InventoryType; diff --git a/src/main/java/net/theevilreaper/aves/inventory/pageable/PageableInventoryBuilder.java b/src/main/java/net/theevilreaper/aves/inventory/pageable/PageableInventoryBuilder.java index 47eb319e..ed0feb35 100644 --- a/src/main/java/net/theevilreaper/aves/inventory/pageable/PageableInventoryBuilder.java +++ b/src/main/java/net/theevilreaper/aves/inventory/pageable/PageableInventoryBuilder.java @@ -1,6 +1,6 @@ package net.theevilreaper.aves.inventory.pageable; -import net.theevilreaper.aves.inventory.InventoryLayout; +import net.theevilreaper.aves.inventory.layout.InventoryLayout; import net.theevilreaper.aves.inventory.slot.ISlot; import net.theevilreaper.aves.inventory.util.LayoutCalculator; import net.minestom.server.entity.Player; diff --git a/src/main/java/net/theevilreaper/aves/inventory/pageable/PlayerPageableInventoryImpl.java b/src/main/java/net/theevilreaper/aves/inventory/pageable/PlayerPageableInventoryImpl.java index e0cb3363..b7c63d4d 100644 --- a/src/main/java/net/theevilreaper/aves/inventory/pageable/PlayerPageableInventoryImpl.java +++ b/src/main/java/net/theevilreaper/aves/inventory/pageable/PlayerPageableInventoryImpl.java @@ -1,9 +1,9 @@ package net.theevilreaper.aves.inventory.pageable; -import net.theevilreaper.aves.inventory.InventoryLayout; import net.theevilreaper.aves.inventory.PersonalInventoryBuilder; import net.theevilreaper.aves.inventory.click.ClickHolder; import net.theevilreaper.aves.inventory.function.InventoryClick; +import net.theevilreaper.aves.inventory.layout.InventoryLayout; import net.theevilreaper.aves.inventory.slot.ISlot; import net.theevilreaper.aves.item.IItem; import net.kyori.adventure.text.Component; @@ -26,7 +26,7 @@ * * @author Joltra * @author theEvilReaper - * @version 1.1.0 + * @version 1.1.1 * @since 1.2.0 */ @ApiStatus.Experimental @@ -87,23 +87,20 @@ public final class PlayerPageableInventoryImpl implements PageableInventory { this.oldBackSlot = backSlot == null ? BLANK_SLOT : ISlot.of(backSlot); - this.forwardSlot = this.layout.getSlot(this.pageableControls.getNextSlot()); - ISlot givenForwardSlot = this.layout.getSlot(this.pageableControls.getNextSlot()); this.forwardSlot = givenForwardSlot == null ? BLANK_SLOT : ISlot.of(givenForwardSlot); - this.forwardClick = (clickPlayer, slot, click, stack, result) -> { + this.forwardClick = (_, _, _, _, result) -> { this.update(PageAction.FORWARD); result.accept(ClickHolder.cancelClick()); }; - this.backwardsClick = (clickPlayer, slot, click, stack,result) -> { + this.backwardsClick = (_, _, _, _,result) -> { this.update(PageAction.BACKWARDS); result.accept(ClickHolder.cancelClick()); }; this.builder.setDataLayoutFunction(inventoryLayout -> dataLayout); - this.dataLayout.blank(slotRange); this.initItems(); this.builder.invalidateDataLayout(); this.builder.register(); @@ -118,7 +115,7 @@ public final class PlayerPageableInventoryImpl implements PageableInventory { * If the given action os {@link PageAction#REFRESH} it updates the items on the current page. * The {@link PageAction#BACKWARDS} and {@link PageAction#FORWARD} updates the page boundaries and also updates the item content. * - * @param pageAction the action which should be triggered + * @param pageAction the action that should be triggered */ public void update(@NotNull PageAction pageAction) { switch (pageAction) { @@ -151,7 +148,7 @@ private void setControlItems(@NotNull IItem controlItem, int slotIndex, boolean } /** - * This method updates all items which are currently displayed at the page. + * This method updates all items that are currently displayed at the page. */ private void updatePage() { this.updateItems(); diff --git a/src/main/java/net/theevilreaper/aves/inventory/util/InventoryConstants.java b/src/main/java/net/theevilreaper/aves/inventory/util/InventoryConstants.java index 8f7f7a71..b94eaf0f 100644 --- a/src/main/java/net/theevilreaper/aves/inventory/util/InventoryConstants.java +++ b/src/main/java/net/theevilreaper/aves/inventory/util/InventoryConstants.java @@ -3,21 +3,17 @@ import net.theevilreaper.aves.inventory.click.ClickHolder; import net.theevilreaper.aves.inventory.function.InventoryClick; import net.theevilreaper.aves.inventory.slot.EmptySlot; -import net.minestom.server.event.trait.CancellableEvent; - -import java.util.function.Consumer; /** - * Contains some constant which are used for inventories. + * Contains some constant used for inventories. + * * @author theEvilReaper - * @version 1.0.0 + * @version 1.0.1 * @since 1.2.0 **/ public final class InventoryConstants { public static final InventoryClick CANCEL_CLICK = (player, slot, click, stack, result) -> result.accept(ClickHolder.cancelClick()); - @Deprecated(forRemoval = true, since = "Not needed anymore due to the changes to the inventory system") - public static final Consumer CANCELLABLE_EVENT = event -> event.setCancelled(true); public static final EmptySlot BLANK_SLOT = new EmptySlot(); public static final int INVENTORY_WIDTH = 9; public static final int INVALID_SLOT_ID = -999; @@ -25,5 +21,6 @@ public final class InventoryConstants { /** * Private constructor without any usage. */ - private InventoryConstants() {} + private InventoryConstants() { + } } diff --git a/src/main/java/net/theevilreaper/aves/inventory/util/LayoutCalculator.java b/src/main/java/net/theevilreaper/aves/inventory/util/LayoutCalculator.java index 1217e138..25f2e7fe 100644 --- a/src/main/java/net/theevilreaper/aves/inventory/util/LayoutCalculator.java +++ b/src/main/java/net/theevilreaper/aves/inventory/util/LayoutCalculator.java @@ -3,7 +3,6 @@ import net.minestom.server.inventory.InventoryType; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; import java.util.Arrays; @@ -13,7 +12,7 @@ * Contains some useful methods to calculate some layouts for an inventory. * * @author Patrick Zdarsky / Rxcki - * @version 1.0.1 + * @version 1.0.2 * @since 1.0.0 */ public final class LayoutCalculator { @@ -30,7 +29,7 @@ private LayoutCalculator() { * @param toSlot The id from the end slot * @return an array which contains the slot numbers */ - public static int @NotNull [] repeat(int fromSlot, int toSlot) { + public static int[] repeat(int fromSlot, int toSlot) { Check.argCondition(fromSlot > toSlot, "fromSlot cannot be higher that toSlot!"); var arr = new int[toSlot - fromSlot]; for (int i = 0; i < arr.length; i++) { @@ -47,7 +46,7 @@ private LayoutCalculator() { * @return a new array with the given slots */ @Contract(pure = true) - public static int @NotNull [] from(int... slots) { + public static int[] from(int... slots) { return Arrays.copyOfRange(slots, 0, slots.length); } @@ -58,7 +57,7 @@ private LayoutCalculator() { * @param lastCornerSlot The last corner slot * @return an array which contains the slot numbers */ - public static int @NotNull [] quad(int firstCornerSlot, int lastCornerSlot) { + public static int[] quad(int firstCornerSlot, int lastCornerSlot) { var x1 = firstCornerSlot % INVENTORY_WIDTH; var y1 = Math.floor(firstCornerSlot / (double) INVENTORY_WIDTH); @@ -90,7 +89,7 @@ private LayoutCalculator() { * @param lastCornerSlot The last corner slot * @return an array which contains the slot numbers */ - public static int @NotNull [] frame(int firstCornerSlot, int lastCornerSlot) { + public static int[] frame(int firstCornerSlot, int lastCornerSlot) { Check.argCondition(firstCornerSlot == lastCornerSlot, "The values can't be the smae"); var x1 = firstCornerSlot % INVENTORY_WIDTH; var y1 = Math.floor(firstCornerSlot / (double) INVENTORY_WIDTH); @@ -122,7 +121,7 @@ private LayoutCalculator() { * @param type The {@link InventoryType} to get the maximum slot value of a row * @return an array which contains the slot numbers */ - public static int @NotNull [] fillRow(@NotNull InventoryType type) { + public static int[] fillRow(InventoryType type) { return repeat(type.getSize() - INVENTORY_WIDTH, type.getSize()); } @@ -133,7 +132,7 @@ private LayoutCalculator() { * @param column The column to start * @return an array which contains the slot numbers */ - public static int @NotNull [] fillColumn(@NotNull InventoryType type, int column) { + public static int[] fillColumn(InventoryType type, int column) { Check.argCondition(column < 0 || column > INVENTORY_WIDTH - 1, "Column cant be less than 0 or more than 8"); var arr = new int[getRowCount(type)]; for (int i = 0; i < arr.length; i++) { @@ -148,7 +147,7 @@ private LayoutCalculator() { * @param type The type to define the row count * @return Returns the determined row count */ - public static int getRowCount(@NotNull InventoryType type) { + public static int getRowCount(InventoryType type) { if (!isChestInventory(type)) return 1; return type.getSize() / INVENTORY_WIDTH; } @@ -159,7 +158,7 @@ public static int getRowCount(@NotNull InventoryType type) { * @param type The typ to check * @return True if the type is a chest otherwise false */ - public static boolean isChestInventory(@NotNull InventoryType type) { + public static boolean isChestInventory(InventoryType type) { return type.name().startsWith(CHEST_INVENTORY); } } diff --git a/src/main/java/net/theevilreaper/aves/inventory/util/package-info.java b/src/main/java/net/theevilreaper/aves/inventory/util/package-info.java new file mode 100644 index 00000000..562b63c6 --- /dev/null +++ b/src/main/java/net/theevilreaper/aves/inventory/util/package-info.java @@ -0,0 +1,4 @@ +@NotNullByDefault +package net.theevilreaper.aves.inventory.util; + +import org.jetbrains.annotations.NotNullByDefault; \ No newline at end of file diff --git a/src/test/java/net/theevilreaper/aves/inventory/GlobalInventoryBuilderTest.java b/src/test/java/net/theevilreaper/aves/inventory/GlobalInventoryBuilderTest.java index c4649b80..0c0924f1 100644 --- a/src/test/java/net/theevilreaper/aves/inventory/GlobalInventoryBuilderTest.java +++ b/src/test/java/net/theevilreaper/aves/inventory/GlobalInventoryBuilderTest.java @@ -2,6 +2,7 @@ import net.minestom.server.instance.Instance; import net.theevilreaper.aves.inventory.exception.ListenerStateException; +import net.theevilreaper.aves.inventory.layout.InventoryLayout; import net.theevilreaper.aves.inventory.util.LayoutCalculator; import net.kyori.adventure.text.Component; import net.minestom.server.coordinate.Pos; @@ -24,8 +25,8 @@ void testGlobalBuilder(Env env) { var instance = env.createFlatInstance(); var player = env.createPlayer(instance, Pos.ZERO); var builder = new GlobalInventoryBuilder(Component.text("Test"), InventoryType.CHEST_2_ROW); - var layout = new InventoryLayoutImpl(builder.getType()); - var dataLayout = new InventoryLayoutImpl(builder.getType()); + var layout = InventoryLayout.fromType(builder.getType()); + var dataLayout = InventoryLayout.fromType(builder.getType()); dataLayout.setItems(LayoutCalculator.fillRow(InventoryType.CHEST_2_ROW), ItemStack.builder(Material.DIAMOND)); diff --git a/src/test/java/net/theevilreaper/aves/inventory/GlobalTranslatedInventoryBuilderTest.java b/src/test/java/net/theevilreaper/aves/inventory/GlobalTranslatedInventoryBuilderTest.java index 66e67600..642e808d 100644 --- a/src/test/java/net/theevilreaper/aves/inventory/GlobalTranslatedInventoryBuilderTest.java +++ b/src/test/java/net/theevilreaper/aves/inventory/GlobalTranslatedInventoryBuilderTest.java @@ -9,6 +9,7 @@ import net.minestom.server.inventory.InventoryType; import net.minestom.testing.Env; import net.minestom.testing.extension.MicrotusExtension; +import net.theevilreaper.aves.inventory.layout.InventoryLayout; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -50,6 +51,4 @@ void testGlobalTranslatedBuilderWithRegistry(Env env) { assertNotNull(builder.getLayout()); assertNull(builder.getDataLayout()); } - - } diff --git a/src/test/java/net/theevilreaper/aves/inventory/InventoryBuilderTest.java b/src/test/java/net/theevilreaper/aves/inventory/InventoryBuilderTest.java index b24ca53e..98bd69b1 100644 --- a/src/test/java/net/theevilreaper/aves/inventory/InventoryBuilderTest.java +++ b/src/test/java/net/theevilreaper/aves/inventory/InventoryBuilderTest.java @@ -3,27 +3,19 @@ import net.kyori.adventure.text.Component; import net.minestom.server.inventory.InventoryType; import net.minestom.testing.extension.MicrotusExtension; +import net.theevilreaper.aves.inventory.layout.InventoryLayout; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.extension.ExtendWith; import static net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer.plainText; import static org.junit.jupiter.api.Assertions.*; @ExtendWith(MicrotusExtension.class) -@TestInstance(TestInstance.Lifecycle.PER_CLASS) class InventoryBuilderTest { private final Component title = Component.text("Title"); private final InventoryType type = InventoryType.CHEST_3_ROW; - @Test - void testInventoryUpdateWhichRaisesAnException() { - var builder = new GlobalInventoryBuilder(title, type); - var exception = assertThrows(IllegalStateException.class, builder::updateInventory); - assertEquals("Can't update content because the layout is null", exception.getMessage()); - } - @Test void testTitleUpdate() { var builder = new GlobalInventoryBuilder(title, type); diff --git a/src/test/java/net/theevilreaper/aves/inventory/InventoryClickLogicIntegrationTest.java b/src/test/java/net/theevilreaper/aves/inventory/InventoryClickLogicIntegrationTest.java index a5d1a38e..f4c9de2b 100644 --- a/src/test/java/net/theevilreaper/aves/inventory/InventoryClickLogicIntegrationTest.java +++ b/src/test/java/net/theevilreaper/aves/inventory/InventoryClickLogicIntegrationTest.java @@ -15,6 +15,7 @@ import net.minestom.testing.TestConnection; import net.minestom.testing.extension.MicrotusExtension; import net.theevilreaper.aves.inventory.click.ClickHolder; +import net.theevilreaper.aves.inventory.layout.InventoryLayout; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; diff --git a/src/test/java/net/theevilreaper/aves/inventory/InventoryLayoutTest.java b/src/test/java/net/theevilreaper/aves/inventory/InventoryLayoutTest.java index dbb13e80..dc40e5dd 100644 --- a/src/test/java/net/theevilreaper/aves/inventory/InventoryLayoutTest.java +++ b/src/test/java/net/theevilreaper/aves/inventory/InventoryLayoutTest.java @@ -1,6 +1,9 @@ package net.theevilreaper.aves.inventory; +import net.minestom.testing.extension.MicrotusExtension; import net.theevilreaper.aves.inventory.function.DefaultApplyLayoutFunction; +import net.theevilreaper.aves.inventory.layout.InventoryLayout; +import net.theevilreaper.aves.inventory.slot.EmptySlot; import net.theevilreaper.aves.inventory.slot.TranslatedSlot; import net.theevilreaper.aves.inventory.util.LayoutCalculator; import net.theevilreaper.aves.item.TranslatedItem; @@ -8,6 +11,7 @@ import net.minestom.server.item.ItemStack; import net.minestom.server.item.Material; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.function.Executable; import java.util.Arrays; @@ -16,11 +20,12 @@ import static net.theevilreaper.aves.inventory.util.InventoryConstants.*; import static org.junit.jupiter.api.Assertions.*; +@ExtendWith(MicrotusExtension.class) class InventoryLayoutTest { @Test void testCopyConstructor() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_1_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_1_ROW); layout.setItem(0, new TranslatedSlot(TranslatedItem.of(Material.ITEM_FRAME))); layout.setItems(LayoutCalculator.fillRow(InventoryType.CHEST_1_ROW), ItemStack.builder(Material.ALLIUM).build()); var copiedLayout = InventoryLayout.of(layout); @@ -30,7 +35,7 @@ void testCopyConstructor() { @Test void testSetApplyFunction() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_1_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_1_ROW); var newFunction = new DefaultApplyLayoutFunction(layout.getContents()); layout.setApplyLayoutFunction(newFunction); @@ -40,7 +45,7 @@ void testSetApplyFunction() { @Test void testApplyMethods() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_2_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_2_ROW); layout.setItem(0, ItemStack.builder(Material.BLACK_STAINED_GLASS).build()); var itemStacks = new ItemStack[InventoryType.CHEST_1_ROW.getSize()]; @@ -58,7 +63,7 @@ void testApplyMethods() { @Test void testSetItemMethods() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_2_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_2_ROW); layout.setItem(0, new InventorySlot(ItemStack.builder(Material.BLACK_STAINED_GLASS))); layout.setItem(1, ItemStack.of(Material.PAPER)); @@ -73,7 +78,7 @@ void testSetItemMethods() { @Test void testSetItemsMethods() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_5_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_5_ROW); layout.setItems(LayoutCalculator.fillRow(InventoryType.CHEST_1_ROW), ItemStack.builder(Material.ENDER_CHEST), CANCEL_CLICK); layout.setItems(LayoutCalculator.fillRow(InventoryType.CHEST_2_ROW), ItemStack.of(Material.ACACIA_SLAB), CANCEL_CLICK); layout.setItems(LayoutCalculator.fillRow(InventoryType.CHEST_3_ROW), ItemStack.builder(Material.ALLIUM)); @@ -92,7 +97,7 @@ void testSetItemsMethods() { @Test void testNonClickItemMethods() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_2_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_2_ROW); layout.setItem(0, new InventorySlot(ItemStack.builder(Material.BLACK_STAINED_GLASS))); layout.setItem(1, ItemStack.builder(Material.DIAMOND).build()); @@ -104,7 +109,7 @@ void testNonClickItemMethods() { @Test void testSetNonItemsMethods() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_4_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_4_ROW); layout.setItems(LayoutCalculator.fillRow(InventoryType.CHEST_1_ROW), ItemStack.builder(Material.ENDER_CHEST)); layout.setItems(LayoutCalculator.fillRow(InventoryType.CHEST_2_ROW), ItemStack.of(Material.PAPER)); layout.setItems(LayoutCalculator.fillRow(InventoryType.CHEST_3_ROW), ItemStack.builder(Material.ALLIUM)); @@ -118,7 +123,7 @@ void testSetNonItemsMethods() { @Test void testBlankSlotMethods() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_2_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_2_ROW); layout.blank(0); layout.blank(LayoutCalculator.fillRow(InventoryType.CHEST_2_ROW)); @@ -129,19 +134,19 @@ void testBlankSlotMethods() { @Test void testClearSlotMethods() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_2_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_2_ROW); layout.setItems(LayoutCalculator.repeat(0, InventoryType.CHEST_2_ROW.getSize() - 1), ItemStack.of(Material.PAPER)); layout.clear(0); layout.clear(LayoutCalculator.repeat(5, 10)); - assertNull(layout.getSlot(0)); + assertInstanceOf(EmptySlot.class, layout.getSlot(0)); assertNotNull(layout.getSlot(11)); } @Test void testUpdateSlotMethods() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_2_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_2_ROW); layout.setItem(1, ItemStack.of(Material.PAPER)); layout.update(1, CANCEL_CLICK); @@ -155,14 +160,14 @@ void testUpdateSlotMethods() { @Test void failGetSlot() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_1_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_1_ROW); assertThrowsExactly(IllegalArgumentException.class, () -> layout.getSlot(-1)); assertThrowsExactly(IllegalArgumentException.class, () -> layout.getSlot(12)); } @Test void failItemSet() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_1_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_1_ROW); catchError(IllegalArgumentException.class, () -> layout.setItem(-1, ItemStack.builder(Material.ALLIUM))); catchError(IllegalArgumentException.class, () -> layout.setItem(100, ItemStack.builder(Material.ALLIUM))); @@ -186,7 +191,7 @@ void failItemSet() { @Test void testFailBlankAndClearMethod() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_1_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_1_ROW); catchError(IllegalArgumentException.class, () -> layout.blank(-1)); catchError(IllegalArgumentException.class, () -> layout.blank(100)); @@ -197,7 +202,7 @@ void testFailBlankAndClearMethod() { @Test void testUpdateMethods() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_1_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_1_ROW); catchError(IllegalArgumentException.class, () -> layout.update(-1, CANCEL_CLICK)); catchError(IllegalArgumentException.class, () -> layout.update(100, CANCEL_CLICK)); @@ -208,40 +213,40 @@ void testUpdateMethods() { @Test void testGetContents() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_1_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_1_ROW); assertNotNull(layout.getContents()); } @Test void testGetSize() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_1_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_1_ROW); assertNotSame(InventoryType.CHEST_6_ROW.getSize(), layout.getSize()); } @Test void testGetApplyFunction() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_1_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_1_ROW); assertNotNull(layout.getApplyLayoutFunction()); } @Test void testToString() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_1_ROW); - assertTrue(layout.toString().contains("null")); + var layout = InventoryLayout.fromType(InventoryType.CHEST_1_ROW); + assertFalse(layout.toString().contains("null")); } @Test void testEqualsMethod() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_1_ROW); - var anotherLayout = new InventoryLayoutImpl(InventoryType.CHEST_1_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_1_ROW); + var anotherLayout = InventoryLayout.fromType(InventoryType.CHEST_1_ROW); assertEquals(layout, anotherLayout); - assertNotEquals(layout, new InventoryLayoutImpl(InventoryType.CARTOGRAPHY)); + assertNotEquals(layout, InventoryLayout.fromType(InventoryType.CARTOGRAPHY)); } @Test void testHashCode() { - var layout = new InventoryLayoutImpl(InventoryType.CHEST_1_ROW); + var layout = InventoryLayout.fromType(InventoryType.CHEST_1_ROW); assertNotSame(1231, layout.hashCode()); } diff --git a/src/test/java/net/theevilreaper/aves/inventory/PersonalInventoryBuilderTest.java b/src/test/java/net/theevilreaper/aves/inventory/PersonalInventoryBuilderTest.java index 6ddfbcbd..f445c174 100644 --- a/src/test/java/net/theevilreaper/aves/inventory/PersonalInventoryBuilderTest.java +++ b/src/test/java/net/theevilreaper/aves/inventory/PersonalInventoryBuilderTest.java @@ -5,6 +5,7 @@ import net.minestom.server.inventory.InventoryType; import net.minestom.testing.Env; import net.minestom.testing.extension.MicrotusExtension; +import net.theevilreaper.aves.inventory.layout.InventoryLayout; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -30,4 +31,4 @@ void testPersonalBuilder(Env env) { assertNotNull(player); assertNotNull(builder); } -} \ No newline at end of file +} diff --git a/src/test/java/net/theevilreaper/aves/inventory/pageable/PageableInventoryTest.java b/src/test/java/net/theevilreaper/aves/inventory/pageable/PageableInventoryTest.java index 4f85dd2f..64c313f0 100644 --- a/src/test/java/net/theevilreaper/aves/inventory/pageable/PageableInventoryTest.java +++ b/src/test/java/net/theevilreaper/aves/inventory/pageable/PageableInventoryTest.java @@ -1,7 +1,7 @@ package net.theevilreaper.aves.inventory.pageable; -import net.theevilreaper.aves.inventory.InventoryLayout; import net.theevilreaper.aves.inventory.InventorySlot; +import net.theevilreaper.aves.inventory.layout.InventoryLayout; import net.theevilreaper.aves.inventory.slot.ISlot; import net.theevilreaper.aves.inventory.util.LayoutCalculator; import net.kyori.adventure.text.Component;