From 215f088e3f6d67e4c42da98fad3b2f6ea0555b93 Mon Sep 17 00:00:00 2001 From: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com> Date: Wed, 19 Jul 2023 09:24:04 +0200 Subject: [PATCH] Add PagedInventoriesGuiImpl --- .../xyz/xenondevs/invui/gui/PagedGui.java | 46 ++++++++++ .../invui/gui/PagedInventoriesGuiImpl.java | 84 +++++++++++++++++++ .../invui/gui/PagedItemsGuiImpl.java | 3 +- .../invui/gui/PagedNestedGuiImpl.java | 3 +- 4 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedInventoriesGuiImpl.java diff --git a/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedGui.java b/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedGui.java index 7f71256..26dd896 100644 --- a/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedGui.java +++ b/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedGui.java @@ -4,6 +4,7 @@ import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import xyz.xenondevs.invui.gui.structure.Structure; +import xyz.xenondevs.invui.inventory.Inventory; import xyz.xenondevs.invui.item.Item; import java.util.List; @@ -107,6 +108,51 @@ public interface PagedGui extends Gui { return new PagedNestedGuiImpl(guis, structure); } + /** + * Creates a new {@link Builder Gui Builder} for a {@link PagedGui} that uses {@link Inventory Inventories} as content. + * + * @return The new {@link Builder Gui Builder}. + */ + static @NotNull Builder<@NotNull Inventory> inventories() { + return new PagedInventoriesGuiImpl.Builder(); + } + + /** + * Creates a new {@link PagedGui} after configuring a {@link Builder Gui Builder} using the given {@link Consumer}. + * + * @param consumer The {@link Consumer} to configure the {@link Builder Gui Builder}. + * @return The created {@link PagedGui}. + */ + static @NotNull PagedGui<@NotNull Inventory> inventories(@NotNull Consumer<@NotNull Builder<@NotNull Inventory>> consumer) { + Builder builder = inventories(); + consumer.accept(builder); + return builder.build(); + } + + /** + * Creates a new {@link PagedGui}. + * + * @param width The width of the {@link PagedGui}. + * @param height The height of the {@link PagedGui}. + * @param inventories The {@link Inventory Inventories} to use as pages. + * @param contentListSlots The slots where content should be displayed. + * @return The created {@link PagedGui}. + */ + static @NotNull PagedGui<@NotNull Inventory> ofInventories(int width, int height, @NotNull List<@NotNull Inventory> inventories, int... contentListSlots) { + return new PagedInventoriesGuiImpl(width, height, inventories, contentListSlots); + } + + /** + * Creates a new {@link PagedGui}. + * + * @param structure The {@link Structure} to use. + * @param inventories The {@link Inventory Inventories} to use as pages. + * @return The created {@link PagedGui}. + */ + static @NotNull PagedGui<@NotNull Inventory> ofInventories(@NotNull Structure structure, @NotNull List<@NotNull Inventory> inventories) { + return new PagedInventoriesGuiImpl(inventories, structure); + } + /** * Gets the amount of pages this {@link PagedGui} has. * diff --git a/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedInventoriesGuiImpl.java b/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedInventoriesGuiImpl.java new file mode 100644 index 0000000..6e4f06a --- /dev/null +++ b/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedInventoriesGuiImpl.java @@ -0,0 +1,84 @@ +package xyz.xenondevs.invui.gui; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import xyz.xenondevs.invui.gui.structure.Structure; +import xyz.xenondevs.invui.inventory.Inventory; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +/** + * An {@link AbstractPagedGui} where every page is its own {@link Inventory}. + * + * @see PagedItemsGuiImpl + * @see PagedNestedGuiImpl + */ +final class PagedInventoriesGuiImpl extends AbstractPagedGui { + + private List inventories; + + /** + * Creates a new {@link PagedInventoriesGuiImpl}. + * + * @param width The width of this Gui. + * @param height The height of this Gui. + * @param inventories The {@link Inventory Inventories} to use as pages. + * @param contentListSlots The slots where content should be displayed. + */ + public PagedInventoriesGuiImpl(int width, int height, @Nullable List<@NotNull Inventory> inventories, int... contentListSlots) { + super(width, height, false, contentListSlots); + setContent(inventories); + } + + /** + * Creates a new {@link PagedInventoriesGuiImpl}. + * + * @param inventories The {@link Inventory Inventories} to use as pages. + * @param structure The {@link Structure} to use. + */ + public PagedInventoriesGuiImpl(@Nullable List<@NotNull Inventory> inventories, @NotNull Structure structure) { + super(structure.getWidth(), structure.getHeight(), false, structure); + setContent(inventories); + } + + @Override + public int getPageAmount() { + return inventories.size(); + } + + @Override + public void setContent(@Nullable List<@NotNull Inventory> inventories) { + this.inventories = inventories == null ? new ArrayList<>() : inventories; + update(); + } + + @Override + protected List getPageElements(int page) { + if (inventories.size() <= page) return new ArrayList<>(); + + Inventory inventory = inventories.get(page); + int size = inventory.getSize(); + + return IntStream.range(0, size) + .mapToObj(i -> new SlotElement.InventorySlotElement(inventory, i)) + .collect(Collectors.toList()); + } + + public static final class Builder extends AbstractBuilder { + + @Override + public @NotNull PagedGui build() { + if (structure == null) + throw new IllegalStateException("Structure is not defined."); + + var gui = new PagedInventoriesGuiImpl(content, structure); + applyModifiers(gui); + return gui; + } + + } + +} diff --git a/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedItemsGuiImpl.java b/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedItemsGuiImpl.java index 5d5a252..9e3991c 100644 --- a/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedItemsGuiImpl.java +++ b/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedItemsGuiImpl.java @@ -11,9 +11,10 @@ import java.util.function.BiConsumer; import java.util.stream.Collectors; /** - * A {@link AbstractPagedGui} that is filled with {@link Item Items}. + * An {@link AbstractPagedGui} that is filled with {@link Item Items}. * * @see PagedNestedGuiImpl + * @see PagedInventoriesGuiImpl */ final class PagedItemsGuiImpl extends AbstractPagedGui { diff --git a/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedNestedGuiImpl.java b/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedNestedGuiImpl.java index 368a7ab..fb4f357 100644 --- a/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedNestedGuiImpl.java +++ b/invui-core/src/main/java/xyz/xenondevs/invui/gui/PagedNestedGuiImpl.java @@ -10,9 +10,10 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; /** - * A {@link AbstractPagedGui} where every page is its own {@link Gui}. + * An {@link AbstractPagedGui} where every page is its own {@link Gui}. * * @see PagedItemsGuiImpl + * @see PagedInventoriesGuiImpl */ final class PagedNestedGuiImpl extends AbstractPagedGui {