From d00922379da0bee1e81249fa66b712eaa91c0a18 Mon Sep 17 00:00:00 2001 From: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com> Date: Sun, 29 Jan 2023 17:18:07 +0100 Subject: [PATCH] Improved Window- and GuiBuilder - New, more flexible GuiBuilders - Cloneable Window- and GuiBuilder - Modifiers for Window- and GuiBuilder - Multiple rename handlers for AnvilWindows --- .../r1/AnvilInventoryImpl.java | 16 +- .../r10/AnvilInventoryImpl.java | 16 +- .../r11/AnvilInventoryImpl.java | 16 +- .../r12/AnvilInventoryImpl.java | 16 +- .../r2/AnvilInventoryImpl.java | 16 +- .../r3/AnvilInventoryImpl.java | 16 +- .../r4/AnvilInventoryImpl.java | 16 +- .../r5/AnvilInventoryImpl.java | 16 +- .../r6/AnvilInventoryImpl.java | 16 +- .../r7/AnvilInventoryImpl.java | 16 +- .../r8/AnvilInventoryImpl.java | 16 +- .../r9/AnvilInventoryImpl.java | 16 +- .../inventoryaccess/InventoryAccess.java | 10 +- .../component/AdventureComponentWrapper.java | 12 +- .../component/BaseComponentWrapper.java | 16 +- .../component/ComponentWrapper.java | 8 +- .../kotlin/xyz/xenondevs/invui/GuiBuilders.kt | 11 ++ .../invui/{Windows.kt => WindowBuilders.kt} | 4 +- .../xenondevs/invui/gui/AbstractPagedGui.java | 8 +- .../invui/gui/AbstractScrollGui.java | 25 ++- .../xenondevs/invui/gui/AbstractTabGui.java | 4 +- .../java/xyz/xenondevs/invui/gui/Gui.java | 4 +- .../xyz/xenondevs/invui/gui/PagedGui.java | 6 +- .../xyz/xenondevs/invui/gui/ScrollGui.java | 31 +++- .../java/xyz/xenondevs/invui/gui/TabGui.java | 4 +- .../invui/gui/builder/AbstractGuiBuilder.java | 159 ++++++++++++++++++ .../gui/builder/AbstractPagedGuiBuilder.java | 60 +++++++ .../gui/builder/AbstractScrollGuiBuilder.java | 60 +++++++ .../invui/gui/builder/GuiBuilder.java | 123 +------------- .../invui/gui/builder/GuiContext.java | 45 ----- .../xenondevs/invui/gui/builder/GuiType.java | 42 +++++ .../invui/gui/builder/NormalGuiBuilder.java | 33 ++++ .../gui/builder/PagedItemsGuiBuilder.java | 34 ++++ .../gui/builder/PagedNestedGuiBuilder.java | 34 ++++ .../builder/ScrollInventoryGuiBuilder.java | 34 ++++ .../gui/builder/ScrollItemsGuiBuilder.java | 34 ++++ .../gui/builder/ScrollNestedGuiBuilder.java | 34 ++++ .../invui/gui/builder/TabGuiBuilder.java | 83 +++++++++ .../invui/gui/builder/guitype/GuiType.java | 40 ----- .../gui/builder/guitype/NormalGUIType.java | 17 -- .../gui/builder/guitype/PagedGUIsGUIType.java | 18 -- .../builder/guitype/PagedItemsGUIType.java | 18 -- .../builder/guitype/ScrollGUIsGUIType.java | 18 -- .../builder/guitype/ScrollItemsGUIType.java | 18 -- .../gui/builder/guitype/ScrollVIGUIType.java | 18 -- .../invui/gui/builder/guitype/TabGUIType.java | 18 -- .../invui/gui/impl/PagedItemsGuiImpl.java | 12 +- .../invui/gui/impl/PagedNestedGuiImpl.java | 10 +- ...iImpl.java => ScrollInventoryGuiImpl.java} | 21 ++- .../invui/gui/impl/ScrollItemsGuiImpl.java | 14 +- .../invui/gui/impl/ScrollNestedGuiImpl.java | 14 +- .../xenondevs/invui/gui/impl/TabGuiImpl.java | 4 +- .../invui/window/AbstractWindow.java | 18 +- .../xyz/xenondevs/invui/window/Window.java | 8 + .../builder/AbstractSingleWindowBuilder.java | 7 +- .../builder/AbstractSplitWindowBuilder.java | 11 +- .../window/builder/AbstractWindowBuilder.java | 52 +++++- .../builder/AnvilSingleWindowBuilder.java | 34 +++- .../builder/AnvilSplitWindowBuilder.java | 35 +++- .../CartographySingleWindowBuilder.java | 12 +- .../CartographySplitWindowBuilder.java | 12 +- .../builder/NormalMergedWindowBuilder.java | 12 +- .../builder/NormalSingleWindowBuilder.java | 14 +- .../builder/NormalSplitWindowBuilder.java | 14 +- .../invui/window/builder/WindowBuilder.java | 7 +- .../window/impl/AnvilSingleWindowImpl.java | 14 +- .../window/impl/AnvilSplitWindowImpl.java | 15 +- .../impl/CartographySingleWindowImpl.java | 12 +- .../impl/CartographySplitWindowImpl.java | 13 +- .../window/impl/NormalMergedWindowImpl.java | 10 +- .../window/impl/NormalSingleWindowImpl.java | 10 +- .../window/impl/NormalSplitWindowImpl.java | 11 +- 72 files changed, 1168 insertions(+), 503 deletions(-) create mode 100644 invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/GuiBuilders.kt rename invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/{Windows.kt => WindowBuilders.kt} (64%) create mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/AbstractGuiBuilder.java create mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/AbstractPagedGuiBuilder.java create mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/AbstractScrollGuiBuilder.java delete mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/GuiContext.java create mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/GuiType.java create mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/NormalGuiBuilder.java create mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/PagedItemsGuiBuilder.java create mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/PagedNestedGuiBuilder.java create mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/ScrollInventoryGuiBuilder.java create mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/ScrollItemsGuiBuilder.java create mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/ScrollNestedGuiBuilder.java create mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/TabGuiBuilder.java delete mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/GuiType.java delete mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/NormalGUIType.java delete mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/PagedGUIsGUIType.java delete mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/PagedItemsGUIType.java delete mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/ScrollGUIsGUIType.java delete mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/ScrollItemsGUIType.java delete mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/ScrollVIGUIType.java delete mode 100644 invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/TabGUIType.java rename invui/src/main/java/xyz/xenondevs/invui/gui/impl/{ScrollVIGuiImpl.java => ScrollInventoryGuiImpl.java} (73%) diff --git a/inventoryaccess/inventory-access-r1/src/main/java/xyz/xenondevs/inventoryaccess/r1/AnvilInventoryImpl.java b/inventoryaccess/inventory-access-r1/src/main/java/xyz/xenondevs/inventoryaccess/r1/AnvilInventoryImpl.java index 56c5689..c811de9 100644 --- a/inventoryaccess/inventory-access-r1/src/main/java/xyz/xenondevs/inventoryaccess/r1/AnvilInventoryImpl.java +++ b/inventoryaccess/inventory-access-r1/src/main/java/xyz/xenondevs/inventoryaccess/r1/AnvilInventoryImpl.java @@ -15,6 +15,7 @@ import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; import xyz.xenondevs.inventoryaccess.util.ReflectionUtils; import java.lang.reflect.Field; +import java.util.List; import java.util.function.Consumer; class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { @@ -24,7 +25,7 @@ class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { private static final Field RESULT_INVENTORY_FIELD = ReflectionUtils.getField(ContainerAnvil.class, true, "resultInventory"); private final IChatBaseComponent title; - private final Consumer renameHandler; + private final List> renameHandlers; private final CraftInventoryView view; private final EntityPlayer player; @@ -34,16 +35,16 @@ class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { private String text; private boolean open; - public AnvilInventoryImpl(Player player, @NotNull ComponentWrapper title, Consumer renameHandler) { - this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandler); + public AnvilInventoryImpl(Player player, @NotNull ComponentWrapper title, List> renameHandlers) { + this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers); } - public AnvilInventoryImpl(EntityPlayer player, IChatBaseComponent title, Consumer renameHandler) { + public AnvilInventoryImpl(EntityPlayer player, IChatBaseComponent title, List> renameHandlers) { super(player.nextContainerCounter(), player.inventory, ContainerAccess.at(player.getWorld(), new BlockPosition(0, 0, 0))); this.title = title; - this.renameHandler = renameHandler; + this.renameHandlers = renameHandlers; this.player = player; repairInventory = ReflectionUtils.getFieldValue(REPAIR_INVENTORY_FIELD, this); @@ -138,8 +139,9 @@ class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { // save rename text text = s; - // call the rename handler - if (renameHandler != null) renameHandler.accept(s); + // call rename handlers + if (renameHandlers != null) + renameHandlers.forEach(handler -> handler.accept(s)); // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again sendItem(2); diff --git a/inventoryaccess/inventory-access-r10/src/main/java/xyz/xenondevs/inventoryaccess/r10/AnvilInventoryImpl.java b/inventoryaccess/inventory-access-r10/src/main/java/xyz/xenondevs/inventoryaccess/r10/AnvilInventoryImpl.java index 6d92fa8..2e126f9 100644 --- a/inventoryaccess/inventory-access-r10/src/main/java/xyz/xenondevs/inventoryaccess/r10/AnvilInventoryImpl.java +++ b/inventoryaccess/inventory-access-r10/src/main/java/xyz/xenondevs/inventoryaccess/r10/AnvilInventoryImpl.java @@ -25,28 +25,29 @@ import org.jetbrains.annotations.NotNull; import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; +import java.util.List; import java.util.function.Consumer; class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { private final Component title; - private final Consumer renameHandler; + private final List> renameHandlers; private final CraftInventoryView view; private final ServerPlayer player; private String text; private boolean open; - public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, Consumer renameHandler) { - this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandler); + public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, List> renameHandlers) { + this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers); } - public AnvilInventoryImpl(ServerPlayer player, Component title, Consumer renameHandler) { + public AnvilInventoryImpl(ServerPlayer player, Component title, List> renameHandlers) { super(player.nextContainerCounter(), player.getInventory(), ContainerLevelAccess.create(player.level, new BlockPos(0, 0, 0))); this.title = title; - this.renameHandler = renameHandler; + this.renameHandlers = renameHandlers; this.player = player; CraftInventoryAnvil inventory = new CraftInventoryAnvil(access.getLocation(), @@ -145,8 +146,9 @@ class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { // save rename text text = s; - // call the rename handler - if (renameHandler != null) renameHandler.accept(s); + // call rename handlers + if (renameHandlers != null) + renameHandlers.forEach(handler -> handler.accept(s)); // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again sendItem(2); diff --git a/inventoryaccess/inventory-access-r11/src/main/java/xyz/xenondevs/inventoryaccess/r11/AnvilInventoryImpl.java b/inventoryaccess/inventory-access-r11/src/main/java/xyz/xenondevs/inventoryaccess/r11/AnvilInventoryImpl.java index 2b7041b..fdd2c4a 100644 --- a/inventoryaccess/inventory-access-r11/src/main/java/xyz/xenondevs/inventoryaccess/r11/AnvilInventoryImpl.java +++ b/inventoryaccess/inventory-access-r11/src/main/java/xyz/xenondevs/inventoryaccess/r11/AnvilInventoryImpl.java @@ -25,28 +25,29 @@ import org.jetbrains.annotations.NotNull; import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; +import java.util.List; import java.util.function.Consumer; class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { private final Component title; - private final Consumer renameHandler; + private final List> renameHandlers; private final CraftInventoryView view; private final ServerPlayer player; private String text; private boolean open; - public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, Consumer renameHandler) { - this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandler); + public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, List> renameHandlers) { + this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers); } - public AnvilInventoryImpl(ServerPlayer player, Component title, Consumer renameHandler) { + public AnvilInventoryImpl(ServerPlayer player, Component title, List> renameHandlers) { super(player.nextContainerCounter(), player.getInventory(), ContainerLevelAccess.create(player.level, new BlockPos(0, 0, 0))); this.title = title; - this.renameHandler = renameHandler; + this.renameHandlers = renameHandlers; this.player = player; CraftInventoryAnvil inventory = new CraftInventoryAnvil(access.getLocation(), @@ -145,8 +146,9 @@ class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { // save rename text text = s; - // call the rename handler - if (renameHandler != null) renameHandler.accept(s); + // call rename handlers + if (renameHandlers != null) + renameHandlers.forEach(handler -> handler.accept(s)); // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again sendItem(2); diff --git a/inventoryaccess/inventory-access-r12/src/main/java/xyz/xenondevs/inventoryaccess/r12/AnvilInventoryImpl.java b/inventoryaccess/inventory-access-r12/src/main/java/xyz/xenondevs/inventoryaccess/r12/AnvilInventoryImpl.java index 99fe9f4..2acd073 100644 --- a/inventoryaccess/inventory-access-r12/src/main/java/xyz/xenondevs/inventoryaccess/r12/AnvilInventoryImpl.java +++ b/inventoryaccess/inventory-access-r12/src/main/java/xyz/xenondevs/inventoryaccess/r12/AnvilInventoryImpl.java @@ -25,28 +25,29 @@ import org.jetbrains.annotations.NotNull; import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; +import java.util.List; import java.util.function.Consumer; class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { private final Component title; - private final Consumer renameHandler; + private final List> renameHandlers; private final CraftInventoryView view; private final ServerPlayer player; private String text; private boolean open; - public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, Consumer renameHandler) { - this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandler); + public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, List> renameHandlers) { + this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers); } - public AnvilInventoryImpl(ServerPlayer player, Component title, Consumer renameHandler) { + public AnvilInventoryImpl(ServerPlayer player, Component title, List> renameHandlers) { super(player.nextContainerCounter(), player.getInventory(), ContainerLevelAccess.create(player.level, new BlockPos(0, 0, 0))); this.title = title; - this.renameHandler = renameHandler; + this.renameHandlers = renameHandlers; this.player = player; CraftInventoryAnvil inventory = new CraftInventoryAnvil(access.getLocation(), @@ -145,8 +146,9 @@ class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { // save rename text text = s; - // call the rename handler - if (renameHandler != null) renameHandler.accept(s); + // call rename handlers + if (renameHandlers != null) + renameHandlers.forEach(handler -> handler.accept(s)); // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again sendItem(2); diff --git a/inventoryaccess/inventory-access-r2/src/main/java/xyz/xenondevs/inventoryaccess/r2/AnvilInventoryImpl.java b/inventoryaccess/inventory-access-r2/src/main/java/xyz/xenondevs/inventoryaccess/r2/AnvilInventoryImpl.java index 7dca651..e07832f 100644 --- a/inventoryaccess/inventory-access-r2/src/main/java/xyz/xenondevs/inventoryaccess/r2/AnvilInventoryImpl.java +++ b/inventoryaccess/inventory-access-r2/src/main/java/xyz/xenondevs/inventoryaccess/r2/AnvilInventoryImpl.java @@ -15,6 +15,7 @@ import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; import xyz.xenondevs.inventoryaccess.util.ReflectionUtils; import java.lang.reflect.Field; +import java.util.List; import java.util.function.Consumer; class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { @@ -24,7 +25,7 @@ class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { private static final Field RESULT_INVENTORY_FIELD = ReflectionUtils.getField(ContainerAnvil.class, true, "resultInventory"); private final IChatBaseComponent title; - private final Consumer renameHandler; + private final List> renameHandlers; private final CraftInventoryView view; private final EntityPlayer player; @@ -34,16 +35,16 @@ class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { private String text; private boolean open; - public AnvilInventoryImpl(Player player, @NotNull ComponentWrapper title, Consumer renameHandler) { - this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandler); + public AnvilInventoryImpl(Player player, @NotNull ComponentWrapper title, List> renameHandlers) { + this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers); } - public AnvilInventoryImpl(EntityPlayer player, IChatBaseComponent title, Consumer renameHandler) { + public AnvilInventoryImpl(EntityPlayer player, IChatBaseComponent title, List> renameHandlers) { super(player.nextContainerCounter(), player.inventory, ContainerAccess.at(player.getWorld(), new BlockPosition(0, 0, 0))); this.title = title; - this.renameHandler = renameHandler; + this.renameHandlers = renameHandlers; this.player = player; repairInventory = ReflectionUtils.getFieldValue(REPAIR_INVENTORY_FIELD, this); @@ -138,8 +139,9 @@ class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { // save rename text text = s; - // call the rename handler - if (renameHandler != null) renameHandler.accept(s); + // call rename handlers + if (renameHandlers != null) + renameHandlers.forEach(handler -> handler.accept(s)); // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again sendItem(2); diff --git a/inventoryaccess/inventory-access-r3/src/main/java/xyz/xenondevs/inventoryaccess/r3/AnvilInventoryImpl.java b/inventoryaccess/inventory-access-r3/src/main/java/xyz/xenondevs/inventoryaccess/r3/AnvilInventoryImpl.java index 78eaeb5..ded76e9 100644 --- a/inventoryaccess/inventory-access-r3/src/main/java/xyz/xenondevs/inventoryaccess/r3/AnvilInventoryImpl.java +++ b/inventoryaccess/inventory-access-r3/src/main/java/xyz/xenondevs/inventoryaccess/r3/AnvilInventoryImpl.java @@ -13,28 +13,29 @@ import org.jetbrains.annotations.NotNull; import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; +import java.util.List; import java.util.function.Consumer; class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { private final IChatBaseComponent title; - private final Consumer renameHandler; + private final List> renameHandlers; private final CraftInventoryView view; private final EntityPlayer player; private String text; private boolean open; - public AnvilInventoryImpl(Player player, @NotNull ComponentWrapper title, Consumer renameHandler) { - this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandler); + public AnvilInventoryImpl(Player player, @NotNull ComponentWrapper title, List> renameHandlers) { + this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers); } - public AnvilInventoryImpl(EntityPlayer player, IChatBaseComponent title, Consumer renameHandler) { + public AnvilInventoryImpl(EntityPlayer player, IChatBaseComponent title, List> renameHandlers) { super(player.nextContainerCounter(), player.inventory, ContainerAccess.at(player.getWorld(), new BlockPosition(0, 0, 0))); this.title = title; - this.renameHandler = renameHandler; + this.renameHandlers = renameHandlers; this.player = player; CraftInventoryAnvil inventory = new CraftInventoryAnvil(containerAccess.getLocation(), @@ -125,8 +126,9 @@ class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { // save rename text text = s; - // call the rename handler - if (renameHandler != null) renameHandler.accept(s); + // call rename handlers + if (renameHandlers != null) + renameHandlers.forEach(handler -> handler.accept(s)); // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again sendItem(2); diff --git a/inventoryaccess/inventory-access-r4/src/main/java/xyz/xenondevs/inventoryaccess/r4/AnvilInventoryImpl.java b/inventoryaccess/inventory-access-r4/src/main/java/xyz/xenondevs/inventoryaccess/r4/AnvilInventoryImpl.java index ae9b850..dc9b533 100644 --- a/inventoryaccess/inventory-access-r4/src/main/java/xyz/xenondevs/inventoryaccess/r4/AnvilInventoryImpl.java +++ b/inventoryaccess/inventory-access-r4/src/main/java/xyz/xenondevs/inventoryaccess/r4/AnvilInventoryImpl.java @@ -13,28 +13,29 @@ import org.jetbrains.annotations.NotNull; import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; +import java.util.List; import java.util.function.Consumer; class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { private final IChatBaseComponent title; - private final Consumer renameHandler; + private final List> renameHandlers; private final CraftInventoryView view; private final EntityPlayer player; private String text; private boolean open; - public AnvilInventoryImpl(Player player, @NotNull ComponentWrapper title, Consumer renameHandler) { - this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandler); + public AnvilInventoryImpl(Player player, @NotNull ComponentWrapper title, List> renameHandlers) { + this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers); } - public AnvilInventoryImpl(EntityPlayer player, IChatBaseComponent title, Consumer renameHandler) { + public AnvilInventoryImpl(EntityPlayer player, IChatBaseComponent title, List> renameHandlers) { super(player.nextContainerCounter(), player.inventory, ContainerAccess.at(player.getWorld(), new BlockPosition(0, 0, 0))); this.title = title; - this.renameHandler = renameHandler; + this.renameHandlers = renameHandlers; this.player = player; CraftInventoryAnvil inventory = new CraftInventoryAnvil(containerAccess.getLocation(), @@ -125,8 +126,9 @@ class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { // save rename text text = s; - // call the rename handler - if (renameHandler != null) renameHandler.accept(s); + // call rename handlers + if (renameHandlers != null) + renameHandlers.forEach(handler -> handler.accept(s)); // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again sendItem(2); diff --git a/inventoryaccess/inventory-access-r5/src/main/java/xyz/xenondevs/inventoryaccess/r5/AnvilInventoryImpl.java b/inventoryaccess/inventory-access-r5/src/main/java/xyz/xenondevs/inventoryaccess/r5/AnvilInventoryImpl.java index 27b4584..910f321 100644 --- a/inventoryaccess/inventory-access-r5/src/main/java/xyz/xenondevs/inventoryaccess/r5/AnvilInventoryImpl.java +++ b/inventoryaccess/inventory-access-r5/src/main/java/xyz/xenondevs/inventoryaccess/r5/AnvilInventoryImpl.java @@ -13,28 +13,29 @@ import org.jetbrains.annotations.NotNull; import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; +import java.util.List; import java.util.function.Consumer; class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { private final IChatBaseComponent title; - private final Consumer renameHandler; + private final List> renameHandlers; private final CraftInventoryView view; private final EntityPlayer player; private String text; private boolean open; - public AnvilInventoryImpl(Player player, @NotNull ComponentWrapper title, Consumer renameHandler) { - this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandler); + public AnvilInventoryImpl(Player player, @NotNull ComponentWrapper title, List> renameHandlers) { + this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers); } - public AnvilInventoryImpl(EntityPlayer player, IChatBaseComponent title, Consumer renameHandler) { + public AnvilInventoryImpl(EntityPlayer player, IChatBaseComponent title, List> renameHandlers) { super(player.nextContainerCounter(), player.inventory, ContainerAccess.at(player.getWorld(), new BlockPosition(0, 0, 0))); this.title = title; - this.renameHandler = renameHandler; + this.renameHandlers = renameHandlers; this.player = player; CraftInventoryAnvil inventory = new CraftInventoryAnvil(containerAccess.getLocation(), @@ -125,8 +126,9 @@ class AnvilInventoryImpl extends ContainerAnvil implements AnvilInventory { // save rename text text = s; - // call the rename handler - if (renameHandler != null) renameHandler.accept(s); + // call rename handlers + if (renameHandlers != null) + renameHandlers.forEach(handler -> handler.accept(s)); // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again sendItem(2); diff --git a/inventoryaccess/inventory-access-r6/src/main/java/xyz/xenondevs/inventoryaccess/r6/AnvilInventoryImpl.java b/inventoryaccess/inventory-access-r6/src/main/java/xyz/xenondevs/inventoryaccess/r6/AnvilInventoryImpl.java index 44e9734..ef069d8 100644 --- a/inventoryaccess/inventory-access-r6/src/main/java/xyz/xenondevs/inventoryaccess/r6/AnvilInventoryImpl.java +++ b/inventoryaccess/inventory-access-r6/src/main/java/xyz/xenondevs/inventoryaccess/r6/AnvilInventoryImpl.java @@ -24,28 +24,29 @@ import org.jetbrains.annotations.NotNull; import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; +import java.util.List; import java.util.function.Consumer; class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { private final Component title; - private final Consumer renameHandler; + private final List> renameHandlers; private final CraftInventoryView view; private final ServerPlayer player; private String text; private boolean open; - public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, Consumer renameHandler) { - this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandler); + public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, List> renameHandlers) { + this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers); } - public AnvilInventoryImpl(ServerPlayer player, Component title, Consumer renameHandler) { + public AnvilInventoryImpl(ServerPlayer player, Component title, List> renameHandlers) { super(player.nextContainerCounter(), player.getInventory(), ContainerLevelAccess.create(player.level, new BlockPos(0, 0, 0))); this.title = title; - this.renameHandler = renameHandler; + this.renameHandlers = renameHandlers; this.player = player; CraftInventoryAnvil inventory = new CraftInventoryAnvil(access.getLocation(), @@ -139,8 +140,9 @@ class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { // save rename text text = s; - // call the rename handler - if (renameHandler != null) renameHandler.accept(s); + // call rename handlers + if (renameHandlers != null) + renameHandlers.forEach(handler -> handler.accept(s)); // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again sendItem(2); diff --git a/inventoryaccess/inventory-access-r7/src/main/java/xyz/xenondevs/inventoryaccess/r7/AnvilInventoryImpl.java b/inventoryaccess/inventory-access-r7/src/main/java/xyz/xenondevs/inventoryaccess/r7/AnvilInventoryImpl.java index a7fd537..8022f95 100644 --- a/inventoryaccess/inventory-access-r7/src/main/java/xyz/xenondevs/inventoryaccess/r7/AnvilInventoryImpl.java +++ b/inventoryaccess/inventory-access-r7/src/main/java/xyz/xenondevs/inventoryaccess/r7/AnvilInventoryImpl.java @@ -25,28 +25,29 @@ import org.jetbrains.annotations.NotNull; import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; +import java.util.List; import java.util.function.Consumer; class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { private final Component title; - private final Consumer renameHandler; + private final List> renameHandlers; private final CraftInventoryView view; private final ServerPlayer player; private String text; private boolean open; - public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, Consumer renameHandler) { - this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandler); + public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, List> renameHandlers) { + this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers); } - public AnvilInventoryImpl(ServerPlayer player, Component title, Consumer renameHandler) { + public AnvilInventoryImpl(ServerPlayer player, Component title, List> renameHandlers) { super(player.nextContainerCounter(), player.getInventory(), ContainerLevelAccess.create(player.level, new BlockPos(0, 0, 0))); this.title = title; - this.renameHandler = renameHandler; + this.renameHandlers = renameHandlers; this.player = player; CraftInventoryAnvil inventory = new CraftInventoryAnvil(access.getLocation(), @@ -145,8 +146,9 @@ class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { // save rename text text = s; - // call the rename handler - if (renameHandler != null) renameHandler.accept(s); + // call rename handlers + if (renameHandlers != null) + renameHandlers.forEach(handler -> handler.accept(s)); // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again sendItem(2); diff --git a/inventoryaccess/inventory-access-r8/src/main/java/xyz/xenondevs/inventoryaccess/r8/AnvilInventoryImpl.java b/inventoryaccess/inventory-access-r8/src/main/java/xyz/xenondevs/inventoryaccess/r8/AnvilInventoryImpl.java index e846565..97a70ea 100644 --- a/inventoryaccess/inventory-access-r8/src/main/java/xyz/xenondevs/inventoryaccess/r8/AnvilInventoryImpl.java +++ b/inventoryaccess/inventory-access-r8/src/main/java/xyz/xenondevs/inventoryaccess/r8/AnvilInventoryImpl.java @@ -25,28 +25,29 @@ import org.jetbrains.annotations.NotNull; import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; +import java.util.List; import java.util.function.Consumer; class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { private final Component title; - private final Consumer renameHandler; + private final List> renameHandlers; private final CraftInventoryView view; private final ServerPlayer player; private String text; private boolean open; - public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, Consumer renameHandler) { - this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandler); + public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, List> renameHandlers) { + this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers); } - public AnvilInventoryImpl(ServerPlayer player, Component title, Consumer renameHandler) { + public AnvilInventoryImpl(ServerPlayer player, Component title, List> renameHandlers) { super(player.nextContainerCounter(), player.getInventory(), ContainerLevelAccess.create(player.level, new BlockPos(0, 0, 0))); this.title = title; - this.renameHandler = renameHandler; + this.renameHandlers = renameHandlers; this.player = player; CraftInventoryAnvil inventory = new CraftInventoryAnvil(access.getLocation(), @@ -145,8 +146,9 @@ class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { // save rename text text = s; - // call the rename handler - if (renameHandler != null) renameHandler.accept(s); + // call rename handlers + if (renameHandlers != null) + renameHandlers.forEach(handler -> handler.accept(s)); // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again sendItem(2); diff --git a/inventoryaccess/inventory-access-r9/src/main/java/xyz/xenondevs/inventoryaccess/r9/AnvilInventoryImpl.java b/inventoryaccess/inventory-access-r9/src/main/java/xyz/xenondevs/inventoryaccess/r9/AnvilInventoryImpl.java index bce46e4..1a099e7 100644 --- a/inventoryaccess/inventory-access-r9/src/main/java/xyz/xenondevs/inventoryaccess/r9/AnvilInventoryImpl.java +++ b/inventoryaccess/inventory-access-r9/src/main/java/xyz/xenondevs/inventoryaccess/r9/AnvilInventoryImpl.java @@ -25,28 +25,29 @@ import org.jetbrains.annotations.NotNull; import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; +import java.util.List; import java.util.function.Consumer; class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { private final Component title; - private final Consumer renameHandler; + private final List> renameHandlers; private final CraftInventoryView view; private final ServerPlayer player; private String text; private boolean open; - public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, Consumer renameHandler) { - this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandler); + public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, List> renameHandlers) { + this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers); } - public AnvilInventoryImpl(ServerPlayer player, Component title, Consumer renameHandler) { + public AnvilInventoryImpl(ServerPlayer player, Component title, List> renameHandlers) { super(player.nextContainerCounter(), player.getInventory(), ContainerLevelAccess.create(player.level, new BlockPos(0, 0, 0))); this.title = title; - this.renameHandler = renameHandler; + this.renameHandlers = renameHandlers; this.player = player; CraftInventoryAnvil inventory = new CraftInventoryAnvil(access.getLocation(), @@ -145,8 +146,9 @@ class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { // save rename text text = s; - // call the rename handler - if (renameHandler != null) renameHandler.accept(s); + // call rename handlers + if (renameHandlers != null) + renameHandlers.forEach(handler -> handler.accept(s)); // the client expects the item to change to it's new name and removes it from the inventory, so it needs to be sent again sendItem(2); diff --git a/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/InventoryAccess.java b/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/InventoryAccess.java index 7c333c3..f9ca358 100644 --- a/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/InventoryAccess.java +++ b/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/InventoryAccess.java @@ -2,6 +2,7 @@ package xyz.xenondevs.inventoryaccess; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; import xyz.xenondevs.inventoryaccess.abstraction.inventory.CartographyInventory; import xyz.xenondevs.inventoryaccess.abstraction.util.InventoryUtils; @@ -11,6 +12,7 @@ import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; import xyz.xenondevs.inventoryaccess.util.ReflectionUtils; import java.lang.reflect.Constructor; +import java.util.List; import java.util.function.Consumer; public class InventoryAccess { @@ -62,12 +64,12 @@ public class InventoryAccess { * * @param player The {@link Player} that should see this {@link AnvilInventory} * @param title The inventory title - * @param renameHandler A {@link Consumer} that is called whenever the {@link Player} - * types something in the renaming section of the anvil + * @param renameHandlers A list of {@link Consumer}s that are called whenever the {@link Player} + * types something in the renaming section of the anvil. * @return The {@link AnvilInventory} */ - public static AnvilInventory createAnvilInventory(@NotNull Player player, @NotNull ComponentWrapper title, Consumer renameHandler) { - return ReflectionUtils.construct(ANVIL_INVENTORY_CONSTRUCTOR, player, title, renameHandler); + public static AnvilInventory createAnvilInventory(@NotNull Player player, @NotNull ComponentWrapper title, @Nullable List<@NotNull Consumer> renameHandlers) { + return ReflectionUtils.construct(ANVIL_INVENTORY_CONSTRUCTOR, player, title, renameHandlers); } /** diff --git a/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/AdventureComponentWrapper.java b/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/AdventureComponentWrapper.java index d093ec1..56d29a8 100644 --- a/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/AdventureComponentWrapper.java +++ b/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/AdventureComponentWrapper.java @@ -2,6 +2,7 @@ package xyz.xenondevs.inventoryaccess.component; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import org.jetbrains.annotations.NotNull; public class AdventureComponentWrapper implements ComponentWrapper { @@ -12,8 +13,17 @@ public class AdventureComponentWrapper implements ComponentWrapper { } @Override - public String serializeToJson() { + public @NotNull String serializeToJson() { return GsonComponentSerializer.gson().serialize(component); } + @Override + public @NotNull ComponentWrapper clone() { + try { + return (ComponentWrapper) super.clone(); + } catch (CloneNotSupportedException e) { + throw new AssertionError(); + } + } + } diff --git a/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/BaseComponentWrapper.java b/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/BaseComponentWrapper.java index f9c01f9..d78556c 100644 --- a/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/BaseComponentWrapper.java +++ b/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/BaseComponentWrapper.java @@ -2,6 +2,7 @@ package xyz.xenondevs.inventoryaccess.component; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.chat.ComponentSerializer; +import org.jetbrains.annotations.NotNull; public class BaseComponentWrapper implements ComponentWrapper { @@ -12,8 +13,21 @@ public class BaseComponentWrapper implements ComponentWrapper { } @Override - public String serializeToJson() { + public @NotNull String serializeToJson() { return ComponentSerializer.toString(components); } + @Override + public @NotNull BaseComponentWrapper clone() { + try { + var clone = (BaseComponentWrapper) super.clone(); + for (int i = 0; i < clone.components.length; i++) { + clone.components[i] = clone.components[i].duplicate(); + } + return clone; + } catch (CloneNotSupportedException e) { + throw new AssertionError(); + } + } + } diff --git a/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/ComponentWrapper.java b/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/ComponentWrapper.java index c080f17..b64e3c6 100644 --- a/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/ComponentWrapper.java +++ b/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/component/ComponentWrapper.java @@ -1,7 +1,11 @@ package xyz.xenondevs.inventoryaccess.component; -public interface ComponentWrapper { +import org.jetbrains.annotations.NotNull; + +public interface ComponentWrapper extends Cloneable { - String serializeToJson(); + @NotNull String serializeToJson(); + + @NotNull ComponentWrapper clone(); } diff --git a/invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/GuiBuilders.kt b/invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/GuiBuilders.kt new file mode 100644 index 0000000..53b9197 --- /dev/null +++ b/invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/GuiBuilders.kt @@ -0,0 +1,11 @@ +@file:Suppress("PackageDirectoryMismatch") + +package xyz.xenondevs.invui.gui.builder + +import xyz.xenondevs.invui.gui.Gui + +fun > GuiType.create(builderConsumer: B.() -> Unit): G { + val builder = builder() + builder.builderConsumer() + return builder.build() +} \ No newline at end of file diff --git a/invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/Windows.kt b/invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/WindowBuilders.kt similarity index 64% rename from invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/Windows.kt rename to invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/WindowBuilders.kt index ccd7ffe..1e5c040 100644 --- a/invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/Windows.kt +++ b/invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/WindowBuilders.kt @@ -1,10 +1,8 @@ @file:Suppress("PackageDirectoryMismatch") -package xyz.xenondevs.invui.window.type +package xyz.xenondevs.invui.window.builder import xyz.xenondevs.invui.window.Window -import xyz.xenondevs.invui.window.builder.WindowBuilder -import xyz.xenondevs.invui.window.builder.WindowType fun > WindowType.create(builderConsumer: B.() -> Unit): W { val builder = builder() diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractPagedGui.java b/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractPagedGui.java index aa37417..77bb435 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractPagedGui.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractPagedGui.java @@ -2,7 +2,7 @@ package xyz.xenondevs.invui.gui; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import xyz.xenondevs.invui.gui.builder.GuiBuilder; +import xyz.xenondevs.invui.gui.builder.GuiType; import xyz.xenondevs.invui.gui.impl.PagedItemsGuiImpl; import xyz.xenondevs.invui.gui.impl.PagedNestedGuiImpl; import xyz.xenondevs.invui.gui.structure.Structure; @@ -14,7 +14,7 @@ import java.util.function.BiConsumer; /** * A {@link Gui} with pages. * - * @see GuiBuilder + * @see GuiType * @see PagedItemsGuiImpl * @see PagedNestedGuiImpl */ @@ -108,7 +108,7 @@ public abstract class AbstractPagedGui extends AbstractGui implements PagedGu } @Override - public void registerPageChangeHandler(@NotNull BiConsumer pageChangeHandler) { + public void addPageChangeHandler(@NotNull BiConsumer pageChangeHandler) { if (pageChangeHandlers == null) { pageChangeHandlers = new ArrayList<>(); } @@ -117,7 +117,7 @@ public abstract class AbstractPagedGui extends AbstractGui implements PagedGu } @Override - public void unregisterPageChangeHandler(@NotNull BiConsumer pageChangeHandler) { + public void removePageChangeHandler(@NotNull BiConsumer pageChangeHandler) { if (pageChangeHandlers != null) { pageChangeHandlers.remove(pageChangeHandler); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractScrollGui.java b/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractScrollGui.java index 1e57224..1cd6734 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractScrollGui.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractScrollGui.java @@ -1,11 +1,14 @@ package xyz.xenondevs.invui.gui; +import org.jetbrains.annotations.NotNull; import xyz.xenondevs.invui.gui.impl.ScrollItemsGuiImpl; import xyz.xenondevs.invui.gui.impl.ScrollNestedGuiImpl; import xyz.xenondevs.invui.gui.structure.Structure; import xyz.xenondevs.invui.util.SlotUtils; +import java.util.ArrayList; import java.util.List; +import java.util.function.BiConsumer; /** * A scrollable {@link Gui} @@ -19,8 +22,9 @@ public abstract class AbstractScrollGui extends AbstractGui implements Scroll private final int lineLength; private final int lineAmount; private final int[] contentListSlots; + private int offset; - protected int offset; + private List> scrollHandlers; public AbstractScrollGui(int width, int height, boolean infiniteLines, int... contentListSlots) { super(width, height); @@ -109,6 +113,25 @@ public abstract class AbstractScrollGui extends AbstractGui implements Scroll } } + @Override + public void setScrollHandlers(@NotNull List<@NotNull BiConsumer> scrollHandlers) { + this.scrollHandlers = scrollHandlers; + } + + @Override + public void addScrollHandler(@NotNull BiConsumer scrollHandler) { + if (scrollHandlers == null) + scrollHandlers = new ArrayList<>(); + + scrollHandlers.add(scrollHandler); + } + + @Override + public void removeScrollHandler(@NotNull BiConsumer scrollHandler) { + if (scrollHandlers != null) + scrollHandlers.remove(scrollHandler); + } + protected abstract List getElements(int from, int to); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractTabGui.java b/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractTabGui.java index 9d998e1..1a75e29 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractTabGui.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractTabGui.java @@ -82,13 +82,13 @@ public abstract class AbstractTabGui extends AbstractGui implements TabGui { this.tabChangeHandlers = tabChangeHandlers; } - public void registerTabChangeHandler(@NotNull BiConsumer tabChangeHandler) { + public void addTabChangeHandler(@NotNull BiConsumer tabChangeHandler) { if (tabChangeHandlers == null) tabChangeHandlers = new ArrayList<>(); tabChangeHandlers.add(tabChangeHandler); } @Override - public void unregisterTabChangeHandler(@NotNull BiConsumer tabChangeHandler) { + public void removeTabChangeHandler(@NotNull BiConsumer tabChangeHandler) { if (tabChangeHandlers != null) tabChangeHandlers.remove(tabChangeHandler); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/Gui.java b/invui/src/main/java/xyz/xenondevs/invui/gui/Gui.java index fe5a201..03f04f0 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/Gui.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/Gui.java @@ -5,7 +5,7 @@ import org.bukkit.inventory.Inventory; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import xyz.xenondevs.invui.animation.Animation; -import xyz.xenondevs.invui.gui.builder.GuiBuilder; +import xyz.xenondevs.invui.gui.builder.GuiType; import xyz.xenondevs.invui.gui.impl.NormalGuiImpl; import xyz.xenondevs.invui.gui.structure.Structure; import xyz.xenondevs.invui.item.Item; @@ -28,7 +28,7 @@ import java.util.function.Predicate; * In order to create an {@link Inventory} which is visible * to players, you will need to use a {@link Window}. * - * @see GuiBuilder + * @see GuiType * @see AbstractGui * @see AbstractPagedGui * @see AbstractScrollGui diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/PagedGui.java b/invui/src/main/java/xyz/xenondevs/invui/gui/PagedGui.java index 154e71f..cc0d15c 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/PagedGui.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/PagedGui.java @@ -125,7 +125,7 @@ public interface PagedGui extends Gui { * * @param content The content to set. */ - void setContent(List<@Nullable C> content); + void setContent(@Nullable List<@NotNull C> content); /** * Gets the registered page change handlers. @@ -146,13 +146,13 @@ public interface PagedGui extends Gui { * * @param handler The handler to register. */ - void registerPageChangeHandler(@NotNull BiConsumer handler); + void addPageChangeHandler(@NotNull BiConsumer handler); /** * Unregisters a page change handler. * * @param handler The handler to unregister. */ - void unregisterPageChangeHandler(@NotNull BiConsumer handler); + void removePageChangeHandler(@NotNull BiConsumer handler); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/ScrollGui.java b/invui/src/main/java/xyz/xenondevs/invui/gui/ScrollGui.java index 6199802..094ae86 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/ScrollGui.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/ScrollGui.java @@ -1,14 +1,16 @@ package xyz.xenondevs.invui.gui; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import xyz.xenondevs.invui.gui.impl.ScrollItemsGuiImpl; import xyz.xenondevs.invui.gui.impl.ScrollNestedGuiImpl; -import xyz.xenondevs.invui.gui.impl.ScrollVIGuiImpl; +import xyz.xenondevs.invui.gui.impl.ScrollInventoryGuiImpl; import xyz.xenondevs.invui.gui.structure.Structure; import xyz.xenondevs.invui.item.Item; import xyz.xenondevs.invui.virtualinventory.VirtualInventory; import java.util.List; +import java.util.function.BiConsumer; @SuppressWarnings("deprecation") public interface ScrollGui extends Gui { @@ -71,7 +73,7 @@ public interface ScrollGui extends Gui { * @return The created {@link ScrollGui}. */ static @NotNull ScrollGui ofInventories(int width, int height, @NotNull List<@NotNull VirtualInventory> inventories, int... contentListSlots) { - return new ScrollVIGuiImpl(width, height, inventories, contentListSlots); + return new ScrollInventoryGuiImpl(width, height, inventories, contentListSlots); } /** @@ -82,7 +84,7 @@ public interface ScrollGui extends Gui { * @return The created {@link ScrollGui}. */ static @NotNull ScrollGui ofInventories(@NotNull Structure structure, @NotNull List<@NotNull VirtualInventory> inventories) { - return new ScrollVIGuiImpl(inventories, structure); + return new ScrollInventoryGuiImpl(inventories, structure); } /** @@ -125,6 +127,27 @@ public interface ScrollGui extends Gui { * * @param content The content to set. */ - void setContent(@NotNull List<@NotNull C> content); + void setContent(@Nullable List<@NotNull C> content); + + /** + * Replaces the currently registered scroll handlers with the specified ones. + * + * @param scrollHandlers The new scroll handlers. + */ + void setScrollHandlers(@NotNull List<@NotNull BiConsumer> scrollHandlers); + + /** + * Adds a scroll handler to this {@link ScrollGui}. + * + * @param scrollHandler The scroll handler to add. + */ + void addScrollHandler(@NotNull BiConsumer scrollHandler); + + /** + * Removes the specified scroll handler from this {@link ScrollGui}. + * + * @param scrollHandler The scroll handler to remove. + */ + void removeScrollHandler(@NotNull BiConsumer scrollHandler); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/TabGui.java b/invui/src/main/java/xyz/xenondevs/invui/gui/TabGui.java index 8baf1f2..116e9dc 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/TabGui.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/TabGui.java @@ -83,13 +83,13 @@ public interface TabGui extends Gui { * * @param handler The handler to register. */ - void registerTabChangeHandler(@NotNull BiConsumer handler); + void addTabChangeHandler(@NotNull BiConsumer handler); /** * Unregisters a page change handler. * * @param handler The handler to unregister. */ - void unregisterTabChangeHandler(@NotNull BiConsumer handler); + void removeTabChangeHandler(@NotNull BiConsumer handler); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/AbstractGuiBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/AbstractGuiBuilder.java new file mode 100644 index 0000000..314e018 --- /dev/null +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/AbstractGuiBuilder.java @@ -0,0 +1,159 @@ +package xyz.xenondevs.invui.gui.builder; + +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.ShapedRecipe; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import xyz.xenondevs.invui.gui.Gui; +import xyz.xenondevs.invui.gui.SlotElement; +import xyz.xenondevs.invui.gui.structure.Marker; +import xyz.xenondevs.invui.gui.structure.Structure; +import xyz.xenondevs.invui.item.Item; +import xyz.xenondevs.invui.item.ItemProvider; +import xyz.xenondevs.invui.item.ItemWrapper; +import xyz.xenondevs.invui.virtualinventory.VirtualInventory; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import java.util.function.Supplier; + + +/** + * A builder class to easily construct {@link Gui Guis}.
+ * It provides similar functionality to Bukkit's {@link ShapedRecipe}, as it + * allows for a structure String which defines the layout of the {@link Gui}. + */ +public abstract class AbstractGuiBuilder> implements GuiBuilder { + + protected Structure structure; + protected ItemProvider background; + protected List> modifiers; + + @Contract("_, _, _, -> this") + public S setStructure(int width, int height, @NotNull String structureData) { + structure = new Structure(width, height, structureData); + return getThis(); + } + + @Contract("_ -> this") + public S setStructure(@NotNull String... structureData) { + structure = new Structure(structureData); + return getThis(); + } + + @Contract("_ -> this") + public S setStructure(@NotNull Structure structure) { + this.structure = structure; + return getThis(); + } + + @Contract("_, _ -> this") + public S addIngredient(char key, @NotNull ItemStack itemStack) { + structure.addIngredient(key, itemStack); + return getThis(); + } + + @Contract("_, _ -> this") + public S addIngredient(char key, @NotNull ItemProvider itemProvider) { + structure.addIngredient(key, itemProvider); + return getThis(); + } + + @Contract("_, _ -> this") + public S addIngredient(char key, @NotNull Item item) { + structure.addIngredient(key, item); + return getThis(); + } + + @Contract("_, _ -> this") + public S addIngredient(char key, @NotNull VirtualInventory inventory) { + structure.addIngredient(key, inventory); + return getThis(); + } + + @Contract("_, _, _ -> this") + public S addIngredient(char key, @NotNull VirtualInventory inventory, @Nullable ItemProvider background) { + structure.addIngredient(key, inventory, background); + return getThis(); + } + + @Contract("_, _ -> this") + public S addIngredient(char key, @NotNull SlotElement element) { + structure.addIngredient(key, element); + return getThis(); + } + + @Contract("_, _ -> this") + public S addIngredient(char key, @NotNull Marker marker) { + structure.addIngredient(key, marker); + return getThis(); + } + + @Contract("_, _ -> this") + public S addIngredient(char key, @NotNull Supplier itemSupplier) { + structure.addIngredient(key, itemSupplier); + return getThis(); + } + + @Contract("_, _ -> this") + public S addIngredientElementSupplier(char key, @NotNull Supplier elementSupplier) { + structure.addIngredientElementSupplier(key, elementSupplier); + return getThis(); + } + + @Contract("_ -> this") + public S setBackground(@NotNull ItemProvider itemProvider) { + background = itemProvider; + return getThis(); + } + + @Contract("_ -> this") + public S setBackground(@NotNull ItemStack itemStack) { + background = new ItemWrapper(itemStack); + return getThis(); + } + + @Contract("_ -> this") + public S addModifier(@NotNull Consumer<@NotNull Gui> modifier) { + if (modifiers == null) + modifiers = new ArrayList<>(); + + modifiers.add(modifier); + return getThis(); + } + + @Contract("_ -> this") + public S setModifiers(@NotNull List<@NotNull Consumer<@NotNull Gui>> modifiers) { + this.modifiers = modifiers; + return getThis(); + } + + protected void applyModifiers(@NotNull G gui) { + if (background != null) { + gui.setBackground(background); + } + if (modifiers != null) { + modifiers.forEach(modifier -> modifier.accept(gui)); + } + } + + @SuppressWarnings("unchecked") + @Override + public @NotNull AbstractGuiBuilder clone() { + try { + var clone = (AbstractGuiBuilder) super.clone(); + clone.structure = structure.clone(); + if (modifiers != null) + clone.modifiers = new ArrayList<>(modifiers); + return clone; + } catch (CloneNotSupportedException e) { + throw new AssertionError(); + } + } + + @Contract(value = "-> this", pure = true) + protected abstract S getThis(); + +} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/AbstractPagedGuiBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/AbstractPagedGuiBuilder.java new file mode 100644 index 0000000..6d4e995 --- /dev/null +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/AbstractPagedGuiBuilder.java @@ -0,0 +1,60 @@ +package xyz.xenondevs.invui.gui.builder; + +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import xyz.xenondevs.invui.gui.PagedGui; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.BiConsumer; + +public abstract class AbstractPagedGuiBuilder> extends AbstractGuiBuilder, S> { + + protected List content; + protected List> pageChangeHandlers; + + @Contract("_ -> this") + public S setContent(@NotNull List<@NotNull C> content) { + this.content = content; + return getThis(); + } + + @Contract("_ -> this") + public S addContent(@NotNull C content) { + if (this.content == null) + this.content = new ArrayList<>(); + + this.content.add(content); + return getThis(); + } + + @Contract("_ -> this") + public S setPageChangeHandlers(@NotNull List<@NotNull BiConsumer> handlers) { + pageChangeHandlers = handlers; + return getThis(); + } + + @Contract("_ -> this") + public S addPageChangeHandler(@NotNull BiConsumer handler) { + if (pageChangeHandlers == null) + pageChangeHandlers = new ArrayList<>(1); + + pageChangeHandlers.add(handler); + return getThis(); + } + + @Override + protected void applyModifiers(@NotNull PagedGui gui) { + super.applyModifiers(gui); + gui.setPageChangeHandlers(pageChangeHandlers); + } + + @Override + public @NotNull AbstractPagedGuiBuilder clone() { + var clone = (AbstractPagedGuiBuilder) super.clone(); + clone.content = new ArrayList<>(content); + clone.pageChangeHandlers = new ArrayList<>(pageChangeHandlers); + return clone; + } + +} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/AbstractScrollGuiBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/AbstractScrollGuiBuilder.java new file mode 100644 index 0000000..bc7edc1 --- /dev/null +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/AbstractScrollGuiBuilder.java @@ -0,0 +1,60 @@ +package xyz.xenondevs.invui.gui.builder; + +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import xyz.xenondevs.invui.gui.ScrollGui; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.BiConsumer; + +public abstract class AbstractScrollGuiBuilder> extends AbstractGuiBuilder, S>{ + + protected List content; + protected List> scrollHandlers; + + @Contract("_ -> this") + public S setContent(@NotNull List<@NotNull C> content) { + this.content = content; + return getThis(); + } + + @Contract("_ -> this") + public S addContent(@NotNull C content) { + if (this.content == null) + this.content = new ArrayList<>(); + + this.content.add(content); + return getThis(); + } + + @Contract("_ -> this") + public S setScrollHandlers(@NotNull List<@NotNull BiConsumer> handlers) { + scrollHandlers = handlers; + return getThis(); + } + + @Contract("_ -> this") + public S addScrollHandler(@NotNull BiConsumer handler) { + if (scrollHandlers == null) + scrollHandlers = new ArrayList<>(1); + + scrollHandlers.add(handler); + return getThis(); + } + + @Override + protected void applyModifiers(@NotNull ScrollGui gui) { + super.applyModifiers(gui); + gui.setScrollHandlers(scrollHandlers); + } + + @Override + public @NotNull AbstractScrollGuiBuilder clone() { + var clone = (AbstractScrollGuiBuilder) super.clone(); + clone.content = new ArrayList<>(content); + clone.scrollHandlers = new ArrayList<>(scrollHandlers); + return clone; + } + +} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/GuiBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/GuiBuilder.java index 4122446..ae08b63 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/GuiBuilder.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/GuiBuilder.java @@ -1,126 +1,15 @@ package xyz.xenondevs.invui.gui.builder; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.ShapedRecipe; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import xyz.xenondevs.invui.gui.Gui; -import xyz.xenondevs.invui.gui.SlotElement; -import xyz.xenondevs.invui.gui.builder.guitype.GuiType; -import xyz.xenondevs.invui.gui.structure.Marker; -import xyz.xenondevs.invui.gui.structure.Structure; -import xyz.xenondevs.invui.item.Item; -import xyz.xenondevs.invui.item.ItemProvider; -import xyz.xenondevs.invui.item.ItemWrapper; -import xyz.xenondevs.invui.virtualinventory.VirtualInventory; -import java.util.ArrayList; -import java.util.List; -import java.util.function.Supplier; - - -/** - * A builder class to easily construct {@link Gui}s.
- * It provides similar functionality to Bukkit's {@link ShapedRecipe}, as it - * allows for a structure String which defines the layout of the {@link Gui}. - */ -public class GuiBuilder { +public interface GuiBuilder extends Cloneable { - protected final GuiType guiType; - protected final GuiContext context; + @Contract("-> new") + @NotNull G build(); - public GuiBuilder(@NotNull GuiType guiType) { - this.guiType = guiType; - this.context = new GuiContext<>(); - } - - public GuiBuilder setStructure(int width, int height, @NotNull String structureData) { - context.setStructure(new Structure(width, height, structureData)); - return this; - } - - public GuiBuilder setStructure(@NotNull String... structureData) { - return setStructure(new Structure(structureData)); - } - - public GuiBuilder setStructure(@NotNull Structure structure) { - context.setStructure(structure); - return this; - } - - public GuiBuilder addIngredient(char key, @NotNull ItemStack itemStack) { - context.getStructure().addIngredient(key, itemStack); - return this; - } - - public GuiBuilder addIngredient(char key, @NotNull ItemProvider itemProvider) { - context.getStructure().addIngredient(key, itemProvider); - return this; - } - - public GuiBuilder addIngredient(char key, @NotNull Item item) { - context.getStructure().addIngredient(key, item); - return this; - } - - public GuiBuilder addIngredient(char key, @NotNull VirtualInventory inventory) { - context.getStructure().addIngredient(key, inventory); - return this; - } - - public GuiBuilder addIngredient(char key, @NotNull VirtualInventory inventory, @Nullable ItemProvider background) { - context.getStructure().addIngredient(key, inventory, background); - return this; - } - - public GuiBuilder addIngredient(char key, @NotNull SlotElement element) { - context.getStructure().addIngredient(key, element); - return this; - } - - public GuiBuilder addIngredient(char key, @NotNull Marker marker) { - context.getStructure().addIngredient(key, marker); - return this; - } - - public GuiBuilder addIngredient(char key, @NotNull Supplier itemSupplier) { - context.getStructure().addIngredient(key, itemSupplier); - return this; - } - - public GuiBuilder addIngredientElementSupplier(char key, @NotNull Supplier elementSupplier) { - context.getStructure().addIngredientElementSupplier(key, elementSupplier); - return this; - } - - public GuiBuilder setBackground(@NotNull ItemProvider itemProvider) { - context.setBackground(itemProvider); - return this; - } - - public GuiBuilder setBackground(@NotNull ItemStack itemStack) { - context.setBackground(new ItemWrapper(itemStack)); - return this; - } - - public GuiBuilder setContent(@NotNull List content) { - context.setContent(content); - return this; - } - - public GuiBuilder addContent(@NotNull C content) { - if (context.getContent() == null) - context.setContent(new ArrayList<>()); - - context.getContent().add(content); - return this; - } - - public @NotNull G build() { - if (context.getStructure() == null) - throw new IllegalStateException("GuiContext has not been set yet."); - - return guiType.createGui(context); - } + @Contract("-> new") + @NotNull GuiBuilder clone(); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/GuiContext.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/GuiContext.java deleted file mode 100644 index 718e827..0000000 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/GuiContext.java +++ /dev/null @@ -1,45 +0,0 @@ -package xyz.xenondevs.invui.gui.builder; - -import org.jetbrains.annotations.NotNull; -import xyz.xenondevs.invui.gui.Gui; -import xyz.xenondevs.invui.gui.builder.guitype.GuiType; -import xyz.xenondevs.invui.gui.structure.Structure; -import xyz.xenondevs.invui.item.ItemProvider; - -import java.util.List; - -/** - * The {@link GuiContext} contains all information from the {@link GuiBuilder} to be passed to - * an instance of {@link GuiType} to create a new {@link Gui}. - */ -public class GuiContext { - - private Structure structure; - private ItemProvider background; - private List content; - - public Structure getStructure() { - return structure; - } - - public void setStructure(@NotNull Structure structure) { - this.structure = structure; - } - - public ItemProvider getBackground() { - return background; - } - - public void setBackground(ItemProvider background) { - this.background = background; - } - - public void setContent(@NotNull List content) { - this.content = content; - } - - public List getContent() { - return content; - } - -} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/GuiType.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/GuiType.java new file mode 100644 index 0000000..dab85a9 --- /dev/null +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/GuiType.java @@ -0,0 +1,42 @@ +package xyz.xenondevs.invui.gui.builder; + +import org.jetbrains.annotations.NotNull; +import xyz.xenondevs.invui.gui.Gui; +import xyz.xenondevs.invui.gui.PagedGui; +import xyz.xenondevs.invui.gui.ScrollGui; +import xyz.xenondevs.invui.gui.TabGui; +import xyz.xenondevs.invui.item.Item; +import xyz.xenondevs.invui.virtualinventory.VirtualInventory; + +import java.util.function.Consumer; + +public interface GuiType> { + + GuiType NORMAL = NormalGuiBuilder::new; + GuiType, PagedItemsGuiBuilder> PAGED_ITEMS = PagedItemsGuiBuilder::new; + GuiType, PagedNestedGuiBuilder> PAGED_GUIS = PagedNestedGuiBuilder::new; + GuiType TAB = TabGuiBuilder::new; + GuiType, ScrollItemsGuiBuilder> SCROLL_ITEMS = ScrollItemsGuiBuilder::new; + GuiType, ScrollNestedGuiBuilder> SCROLL_GUIS = ScrollNestedGuiBuilder::new; + GuiType, ScrollInventoryGuiBuilder> SCROLL_INVENTORY = ScrollInventoryGuiBuilder::new; + + /** + * Creates a new {@link GuiBuilder} for this {@link GuiType}. + * + * @return The created {@link GuiBuilder}. + */ + @NotNull B builder(); + + /** + * Creates a new {@link Gui} after modifying the {@link GuiBuilder} with the given {@link Consumer}. + * + * @param builderConsumer The {@link Consumer} which modifies the {@link GuiBuilder}. + * @return The new {@link Gui}. + */ + default @NotNull G createGui(@NotNull Consumer builderConsumer) { + var builder = builder(); + builderConsumer.accept(builder); + return builder.build(); + } + +} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/NormalGuiBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/NormalGuiBuilder.java new file mode 100644 index 0000000..c1ca5d3 --- /dev/null +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/NormalGuiBuilder.java @@ -0,0 +1,33 @@ +package xyz.xenondevs.invui.gui.builder; + +import org.jetbrains.annotations.NotNull; +import xyz.xenondevs.invui.gui.Gui; +import xyz.xenondevs.invui.gui.impl.NormalGuiImpl; + +public final class NormalGuiBuilder extends AbstractGuiBuilder { + + NormalGuiBuilder() { + } + + @SuppressWarnings("deprecation") + @Override + public @NotNull Gui build() { + if (structure == null) + throw new IllegalStateException("Structure is not defined."); + + var gui = new NormalGuiImpl(structure); + applyModifiers(gui); + return gui; + } + + @Override + protected NormalGuiBuilder getThis() { + return this; + } + + @Override + public @NotNull NormalGuiBuilder clone() { + return (NormalGuiBuilder) super.clone(); + } + +} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/PagedItemsGuiBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/PagedItemsGuiBuilder.java new file mode 100644 index 0000000..bc980f7 --- /dev/null +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/PagedItemsGuiBuilder.java @@ -0,0 +1,34 @@ +package xyz.xenondevs.invui.gui.builder; + +import org.jetbrains.annotations.NotNull; +import xyz.xenondevs.invui.gui.PagedGui; +import xyz.xenondevs.invui.gui.impl.PagedItemsGuiImpl; +import xyz.xenondevs.invui.item.Item; + +public final class PagedItemsGuiBuilder extends AbstractPagedGuiBuilder { + + PagedItemsGuiBuilder() { + } + + @SuppressWarnings("deprecation") + @Override + public @NotNull PagedGui build() { + if (structure == null) + throw new IllegalStateException("Structure is not defined."); + + var gui = new PagedItemsGuiImpl(content, structure); + applyModifiers(gui); + return gui; + } + + @Override + protected PagedItemsGuiBuilder getThis() { + return this; + } + + @Override + public @NotNull PagedItemsGuiBuilder clone() { + return (PagedItemsGuiBuilder) super.clone(); + } + +} \ No newline at end of file diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/PagedNestedGuiBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/PagedNestedGuiBuilder.java new file mode 100644 index 0000000..3723abb --- /dev/null +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/PagedNestedGuiBuilder.java @@ -0,0 +1,34 @@ +package xyz.xenondevs.invui.gui.builder; + +import org.jetbrains.annotations.NotNull; +import xyz.xenondevs.invui.gui.Gui; +import xyz.xenondevs.invui.gui.PagedGui; +import xyz.xenondevs.invui.gui.impl.PagedNestedGuiImpl; + +public final class PagedNestedGuiBuilder extends AbstractPagedGuiBuilder { + + PagedNestedGuiBuilder() { + } + + @SuppressWarnings("deprecation") + @Override + public @NotNull PagedGui build() { + if (structure == null) + throw new IllegalStateException("Structure is not defined."); + + var gui = new PagedNestedGuiImpl(content, structure); + applyModifiers(gui); + return gui; + } + + @Override + protected PagedNestedGuiBuilder getThis() { + return this; + } + + @Override + public @NotNull PagedNestedGuiBuilder clone() { + return (PagedNestedGuiBuilder) super.clone(); + } + +} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/ScrollInventoryGuiBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/ScrollInventoryGuiBuilder.java new file mode 100644 index 0000000..3af0739 --- /dev/null +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/ScrollInventoryGuiBuilder.java @@ -0,0 +1,34 @@ +package xyz.xenondevs.invui.gui.builder; + +import org.jetbrains.annotations.NotNull; +import xyz.xenondevs.invui.gui.ScrollGui; +import xyz.xenondevs.invui.gui.impl.ScrollInventoryGuiImpl; +import xyz.xenondevs.invui.virtualinventory.VirtualInventory; + +public final class ScrollInventoryGuiBuilder extends AbstractScrollGuiBuilder { + + ScrollInventoryGuiBuilder() { + } + + @SuppressWarnings("deprecation") + @Override + public @NotNull ScrollGui build() { + if (structure == null) + throw new IllegalStateException("Structure is not defined."); + + var gui = new ScrollInventoryGuiImpl(content, structure); + applyModifiers(gui); + return gui; + } + + @Override + protected ScrollInventoryGuiBuilder getThis() { + return this; + } + + @Override + public @NotNull ScrollInventoryGuiBuilder clone() { + return (ScrollInventoryGuiBuilder) super.clone(); + } + +} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/ScrollItemsGuiBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/ScrollItemsGuiBuilder.java new file mode 100644 index 0000000..9144873 --- /dev/null +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/ScrollItemsGuiBuilder.java @@ -0,0 +1,34 @@ +package xyz.xenondevs.invui.gui.builder; + +import org.jetbrains.annotations.NotNull; +import xyz.xenondevs.invui.gui.ScrollGui; +import xyz.xenondevs.invui.gui.impl.ScrollItemsGuiImpl; +import xyz.xenondevs.invui.item.Item; + +public final class ScrollItemsGuiBuilder extends AbstractScrollGuiBuilder { + + ScrollItemsGuiBuilder() { + } + + @SuppressWarnings("deprecation") + @Override + public @NotNull ScrollGui build() { + if (structure == null) + throw new IllegalStateException("Structure is not defined."); + + var gui = new ScrollItemsGuiImpl(content, structure); + applyModifiers(gui); + return gui; + } + + @Override + protected ScrollItemsGuiBuilder getThis() { + return this; + } + + @Override + public @NotNull ScrollItemsGuiBuilder clone() { + return (ScrollItemsGuiBuilder) super.clone(); + } + +} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/ScrollNestedGuiBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/ScrollNestedGuiBuilder.java new file mode 100644 index 0000000..109d47c --- /dev/null +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/ScrollNestedGuiBuilder.java @@ -0,0 +1,34 @@ +package xyz.xenondevs.invui.gui.builder; + +import org.jetbrains.annotations.NotNull; +import xyz.xenondevs.invui.gui.Gui; +import xyz.xenondevs.invui.gui.ScrollGui; +import xyz.xenondevs.invui.gui.impl.ScrollNestedGuiImpl; + +public final class ScrollNestedGuiBuilder extends AbstractScrollGuiBuilder{ + + ScrollNestedGuiBuilder() { + } + + @SuppressWarnings("deprecation") + @Override + public @NotNull ScrollGui build() { + if (structure == null) + throw new IllegalStateException("Structure is not defined."); + + var gui = new ScrollNestedGuiImpl(content, structure); + applyModifiers(gui); + return gui; + } + + @Override + protected ScrollNestedGuiBuilder getThis() { + return this; + } + + @Override + public @NotNull ScrollNestedGuiBuilder clone() { + return (ScrollNestedGuiBuilder) super.clone(); + } + +} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/TabGuiBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/TabGuiBuilder.java new file mode 100644 index 0000000..b9f3d72 --- /dev/null +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/TabGuiBuilder.java @@ -0,0 +1,83 @@ +package xyz.xenondevs.invui.gui.builder; + +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import xyz.xenondevs.invui.gui.Gui; +import xyz.xenondevs.invui.gui.TabGui; +import xyz.xenondevs.invui.gui.impl.TabGuiImpl; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.BiConsumer; + +public final class TabGuiBuilder extends AbstractGuiBuilder { + + private List tabs; + private List> tabChangeHandlers; + + TabGuiBuilder() { + } + + @Contract("_ -> this") + public TabGuiBuilder setTabs(@NotNull List<@Nullable Gui> tabs) { + this.tabs = tabs; + return this; + } + + @Contract("_ -> this") + public TabGuiBuilder addTab(@Nullable Gui tab) { + if (this.tabs == null) + this.tabs = new ArrayList<>(); + + this.tabs.add(tab); + return this; + } + + @Contract("_ -> this") + public TabGuiBuilder addTabChangeHandler(@NotNull BiConsumer handler) { + if (tabChangeHandlers == null) + tabChangeHandlers = new ArrayList<>(1); + + tabChangeHandlers.add(handler); + return this; + } + + @Contract("_ -> this") + public TabGuiBuilder setTabChangeHandlers(@NotNull List<@NotNull BiConsumer> handlers) { + tabChangeHandlers = handlers; + return this; + } + + @Override + protected void applyModifiers(@NotNull TabGui gui) { + super.applyModifiers(gui); + gui.setTabChangeHandlers(tabChangeHandlers); + } + + @SuppressWarnings("deprecation") + @Override + public @NotNull TabGui build() { + if (structure == null) + throw new IllegalStateException("Structure is not defined."); + if (tabs == null) + throw new IllegalStateException("Tabs are not defined."); + var gui = new TabGuiImpl(tabs, structure); + applyModifiers(gui); + return gui; + } + + @Override + protected TabGuiBuilder getThis() { + return this; + } + + @Override + public @NotNull TabGuiBuilder clone() { + var clone = (TabGuiBuilder) super.clone(); + clone.tabs = new ArrayList<>(tabs); + clone.tabChangeHandlers = new ArrayList<>(tabChangeHandlers); + return clone; + } + +} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/GuiType.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/GuiType.java deleted file mode 100644 index 58b05b2..0000000 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/GuiType.java +++ /dev/null @@ -1,40 +0,0 @@ -package xyz.xenondevs.invui.gui.builder.guitype; - -import org.jetbrains.annotations.NotNull; -import xyz.xenondevs.invui.gui.Gui; -import xyz.xenondevs.invui.gui.PagedGui; -import xyz.xenondevs.invui.gui.ScrollGui; -import xyz.xenondevs.invui.gui.TabGui; -import xyz.xenondevs.invui.gui.builder.GuiBuilder; -import xyz.xenondevs.invui.gui.builder.GuiContext; -import xyz.xenondevs.invui.item.Item; -import xyz.xenondevs.invui.virtualinventory.VirtualInventory; - -public interface GuiType { - - GuiType NORMAL = new NormalGuiType(); - GuiType, Item> PAGED_ITEMS = new PagedItemsGuiType(); - GuiType, Gui> PAGED_GUIS = new PagedGuisGuiType(); - GuiType TAB = new TabGuiType(); - GuiType, Item> SCROLL_ITEMS = new ScrollItemsGuiType(); - GuiType, Gui> SCROLL_GUIS = new ScrollGuisGuiType(); - GuiType, VirtualInventory> SCROLL_INVENTORY = new ScrollVIGuiType(); - - /** - * Creates a {@link Gui} of type {@link G} with the given {@link GuiContext}. - * - * @param context The {@link GuiContext} to create the {@link Gui} from. - * @return The created {@link Gui} - */ - @NotNull G createGui(@NotNull GuiContext context); - - /** - * Creates a new {@link GuiBuilder} for this {@link GuiType}. - * - * @return The created {@link GuiBuilder}. - */ - default @NotNull GuiBuilder builder() { - return new GuiBuilder<>(this); - } - -} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/NormalGUIType.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/NormalGUIType.java deleted file mode 100644 index 234b61a..0000000 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/NormalGUIType.java +++ /dev/null @@ -1,17 +0,0 @@ -package xyz.xenondevs.invui.gui.builder.guitype; - -import org.jetbrains.annotations.NotNull; -import xyz.xenondevs.invui.gui.Gui; -import xyz.xenondevs.invui.gui.builder.GuiContext; -import xyz.xenondevs.invui.gui.impl.NormalGuiImpl; - -class NormalGuiType implements GuiType { - - @Override - public @NotNull NormalGuiImpl createGui(@NotNull GuiContext context) { - NormalGuiImpl gui = new NormalGuiImpl(context.getStructure()); - gui.setBackground(context.getBackground()); - return gui; - } - -} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/PagedGUIsGUIType.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/PagedGUIsGUIType.java deleted file mode 100644 index 78c5060..0000000 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/PagedGUIsGUIType.java +++ /dev/null @@ -1,18 +0,0 @@ -package xyz.xenondevs.invui.gui.builder.guitype; - -import org.jetbrains.annotations.NotNull; -import xyz.xenondevs.invui.gui.Gui; -import xyz.xenondevs.invui.gui.PagedGui; -import xyz.xenondevs.invui.gui.builder.GuiContext; -import xyz.xenondevs.invui.gui.impl.PagedNestedGuiImpl; - -class PagedGuisGuiType implements GuiType, Gui> { - - @Override - public @NotNull PagedNestedGuiImpl createGui(@NotNull GuiContext context) { - PagedNestedGuiImpl gui = new PagedNestedGuiImpl(context.getContent(), context.getStructure()); - gui.setBackground(context.getBackground()); - return gui; - } - -} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/PagedItemsGUIType.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/PagedItemsGUIType.java deleted file mode 100644 index 6c90b68..0000000 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/PagedItemsGUIType.java +++ /dev/null @@ -1,18 +0,0 @@ -package xyz.xenondevs.invui.gui.builder.guitype; - -import org.jetbrains.annotations.NotNull; -import xyz.xenondevs.invui.gui.PagedGui; -import xyz.xenondevs.invui.gui.builder.GuiContext; -import xyz.xenondevs.invui.gui.impl.PagedItemsGuiImpl; -import xyz.xenondevs.invui.item.Item; - -class PagedItemsGuiType implements GuiType, Item> { - - @Override - public @NotNull PagedItemsGuiImpl createGui(@NotNull GuiContext context) { - PagedItemsGuiImpl gui = new PagedItemsGuiImpl(context.getContent(), context.getStructure()); - gui.setBackground(context.getBackground()); - return gui; - } - -} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/ScrollGUIsGUIType.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/ScrollGUIsGUIType.java deleted file mode 100644 index d203dca..0000000 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/ScrollGUIsGUIType.java +++ /dev/null @@ -1,18 +0,0 @@ -package xyz.xenondevs.invui.gui.builder.guitype; - -import org.jetbrains.annotations.NotNull; -import xyz.xenondevs.invui.gui.Gui; -import xyz.xenondevs.invui.gui.ScrollGui; -import xyz.xenondevs.invui.gui.builder.GuiContext; -import xyz.xenondevs.invui.gui.impl.ScrollNestedGuiImpl; - -class ScrollGuisGuiType implements GuiType, Gui> { - - @Override - public @NotNull ScrollNestedGuiImpl createGui(@NotNull GuiContext context) { - ScrollNestedGuiImpl gui = new ScrollNestedGuiImpl(context.getContent(), context.getStructure()); - gui.setBackground(gui.getBackground()); - return gui; - } - -} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/ScrollItemsGUIType.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/ScrollItemsGUIType.java deleted file mode 100644 index dd374b2..0000000 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/ScrollItemsGUIType.java +++ /dev/null @@ -1,18 +0,0 @@ -package xyz.xenondevs.invui.gui.builder.guitype; - -import org.jetbrains.annotations.NotNull; -import xyz.xenondevs.invui.gui.ScrollGui; -import xyz.xenondevs.invui.gui.builder.GuiContext; -import xyz.xenondevs.invui.gui.impl.ScrollItemsGuiImpl; -import xyz.xenondevs.invui.item.Item; - -class ScrollItemsGuiType implements GuiType, Item> { - - @Override - public @NotNull ScrollItemsGuiImpl createGui(@NotNull GuiContext context) { - ScrollItemsGuiImpl gui = new ScrollItemsGuiImpl(context.getContent(), context.getStructure()); - gui.setBackground(context.getBackground()); - return gui; - } - -} \ No newline at end of file diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/ScrollVIGUIType.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/ScrollVIGUIType.java deleted file mode 100644 index 5dfa5b9..0000000 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/ScrollVIGUIType.java +++ /dev/null @@ -1,18 +0,0 @@ -package xyz.xenondevs.invui.gui.builder.guitype; - -import org.jetbrains.annotations.NotNull; -import xyz.xenondevs.invui.gui.ScrollGui; -import xyz.xenondevs.invui.gui.builder.GuiContext; -import xyz.xenondevs.invui.gui.impl.ScrollVIGuiImpl; -import xyz.xenondevs.invui.virtualinventory.VirtualInventory; - -class ScrollVIGuiType implements GuiType, VirtualInventory> { - - @Override - public @NotNull ScrollVIGuiImpl createGui(@NotNull GuiContext context) { - ScrollVIGuiImpl gui = new ScrollVIGuiImpl(context.getContent(), context.getStructure()); - gui.setBackground(context.getBackground()); - return gui; - } - -} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/TabGUIType.java b/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/TabGUIType.java deleted file mode 100644 index 8a16d4e..0000000 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/builder/guitype/TabGUIType.java +++ /dev/null @@ -1,18 +0,0 @@ -package xyz.xenondevs.invui.gui.builder.guitype; - -import org.jetbrains.annotations.NotNull; -import xyz.xenondevs.invui.gui.Gui; -import xyz.xenondevs.invui.gui.TabGui; -import xyz.xenondevs.invui.gui.builder.GuiContext; -import xyz.xenondevs.invui.gui.impl.TabGuiImpl; - -class TabGuiType implements GuiType { - - @Override - public @NotNull TabGuiImpl createGui(@NotNull GuiContext context) { - TabGuiImpl gui = new TabGuiImpl(context.getContent(), context.getStructure()); - gui.setBackground(context.getBackground()); - return gui; - } - -} diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/impl/PagedItemsGuiImpl.java b/invui/src/main/java/xyz/xenondevs/invui/gui/impl/PagedItemsGuiImpl.java index 6962863..98ee35a 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/impl/PagedItemsGuiImpl.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/impl/PagedItemsGuiImpl.java @@ -5,7 +5,7 @@ import org.jetbrains.annotations.Nullable; import xyz.xenondevs.invui.gui.AbstractPagedGui; import xyz.xenondevs.invui.gui.PagedGui; import xyz.xenondevs.invui.gui.SlotElement; -import xyz.xenondevs.invui.gui.builder.GuiBuilder; +import xyz.xenondevs.invui.gui.builder.GuiType; import xyz.xenondevs.invui.gui.structure.Structure; import xyz.xenondevs.invui.item.Item; @@ -15,9 +15,9 @@ import java.util.function.BiConsumer; import java.util.stream.Collectors; /** - * A {@link AbstractPagedGui} that is filled with {@link Item}s. + * A {@link AbstractPagedGui} that is filled with {@link Item Items}. * - * @see GuiBuilder + * @see GuiType * @see PagedNestedGuiImpl */ @SuppressWarnings("DeprecatedIsStillUsed") @@ -36,7 +36,7 @@ public final class PagedItemsGuiImpl extends AbstractPagedGui { * @deprecated Use {@link PagedGui#ofItems(int, int, List, int...)} instead. */ @Deprecated - public PagedItemsGuiImpl(int width, int height, @Nullable List items, int... contentListSlots) { + public PagedItemsGuiImpl(int width, int height, @Nullable List<@NotNull Item> items, int... contentListSlots) { super(width, height, false, contentListSlots); setContent(items); } @@ -49,7 +49,7 @@ public final class PagedItemsGuiImpl extends AbstractPagedGui { * @deprecated Use {@link PagedGui#ofItems(Structure, List)} instead. */ @Deprecated - public PagedItemsGuiImpl(@Nullable List items, @NotNull Structure structure) { + public PagedItemsGuiImpl(@Nullable List<@NotNull Item> items, @NotNull Structure structure) { super(structure.getWidth(), structure.getHeight(), false, structure); setContent(items); } @@ -60,7 +60,7 @@ public final class PagedItemsGuiImpl extends AbstractPagedGui { } @Override - public void setContent(List<@Nullable Item> items) { + public void setContent(@Nullable List<@NotNull Item> items) { this.items = items != null ? items : new ArrayList<>(); update(); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/impl/PagedNestedGuiImpl.java b/invui/src/main/java/xyz/xenondevs/invui/gui/impl/PagedNestedGuiImpl.java index 8776a11..71fbfba 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/impl/PagedNestedGuiImpl.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/impl/PagedNestedGuiImpl.java @@ -6,7 +6,7 @@ import xyz.xenondevs.invui.gui.AbstractPagedGui; import xyz.xenondevs.invui.gui.Gui; import xyz.xenondevs.invui.gui.PagedGui; import xyz.xenondevs.invui.gui.SlotElement; -import xyz.xenondevs.invui.gui.builder.GuiBuilder; +import xyz.xenondevs.invui.gui.builder.GuiType; import xyz.xenondevs.invui.gui.structure.Structure; import java.util.ArrayList; @@ -17,7 +17,7 @@ import java.util.stream.IntStream; /** * A {@link AbstractPagedGui} where every page is its own {@link Gui}. * - * @see GuiBuilder + * @see GuiType * @see PagedItemsGuiImpl */ @SuppressWarnings("DeprecatedIsStillUsed") @@ -35,7 +35,7 @@ public final class PagedNestedGuiImpl extends AbstractPagedGui { * @deprecated Use {@link PagedGui#ofGuis(int, int, List, int...)} instead. */ @Deprecated - public PagedNestedGuiImpl(int width, int height, @Nullable List guis, int... contentListSlots) { + public PagedNestedGuiImpl(int width, int height, @Nullable List<@NotNull Gui> guis, int... contentListSlots) { super(width, height, false, contentListSlots); setContent(guis); } @@ -48,7 +48,7 @@ public final class PagedNestedGuiImpl extends AbstractPagedGui { * @deprecated Use {@link PagedGui#ofGuis(Structure, List)} instead. */ @Deprecated - public PagedNestedGuiImpl(@Nullable List guis, @NotNull Structure structure) { + public PagedNestedGuiImpl(@Nullable List<@NotNull Gui> guis, @NotNull Structure structure) { super(structure.getWidth(), structure.getHeight(), false, structure); setContent(guis); } @@ -59,7 +59,7 @@ public final class PagedNestedGuiImpl extends AbstractPagedGui { } @Override - public void setContent(@Nullable List guis) { + public void setContent(@Nullable List<@NotNull Gui> guis) { this.guis = guis == null ? new ArrayList<>() : guis; update(); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/impl/ScrollVIGuiImpl.java b/invui/src/main/java/xyz/xenondevs/invui/gui/impl/ScrollInventoryGuiImpl.java similarity index 73% rename from invui/src/main/java/xyz/xenondevs/invui/gui/impl/ScrollVIGuiImpl.java rename to invui/src/main/java/xyz/xenondevs/invui/gui/impl/ScrollInventoryGuiImpl.java index cb6a2dc..fe4ebe5 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/impl/ScrollVIGuiImpl.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/impl/ScrollInventoryGuiImpl.java @@ -1,23 +1,32 @@ package xyz.xenondevs.invui.gui.impl; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import xyz.xenondevs.invui.gui.AbstractScrollGui; import xyz.xenondevs.invui.gui.ScrollGui; import xyz.xenondevs.invui.gui.SlotElement; +import xyz.xenondevs.invui.gui.builder.GuiType; import xyz.xenondevs.invui.gui.structure.Structure; import xyz.xenondevs.invui.virtualinventory.VirtualInventory; import java.util.ArrayList; import java.util.List; +/** + * A {@link AbstractScrollGui} that uses {@link VirtualInventory VirtualInventories} as content. + * + * @see GuiType + * @see ScrollItemsGuiImpl + * @see ScrollNestedGuiImpl + */ @SuppressWarnings("DeprecatedIsStillUsed") -public final class ScrollVIGuiImpl extends AbstractScrollGui { +public final class ScrollInventoryGuiImpl extends AbstractScrollGui { private List inventories; private List elements; /** - * Creates a new {@link ScrollVIGuiImpl}. + * Creates a new {@link ScrollInventoryGuiImpl}. * * @param width The width of this Gui. * @param height The width of this Gui. @@ -26,26 +35,26 @@ public final class ScrollVIGuiImpl extends AbstractScrollGui { * @deprecated Use {@link ScrollGui#ofInventories(int, int, List, int...)} instead. */ @Deprecated - public ScrollVIGuiImpl(int width, int height, @NotNull List inventories, int... contentListSlots) { + public ScrollInventoryGuiImpl(int width, int height, @Nullable List<@NotNull VirtualInventory> inventories, int... contentListSlots) { super(width, height, false, contentListSlots); setContent(inventories); } /** - * Creates a new {@link ScrollVIGuiImpl}. + * Creates a new {@link ScrollInventoryGuiImpl}. * * @param inventories The {@link VirtualInventory VirtualInventories} to use. * @param structure The {@link Structure} to use. * @deprecated Use {@link ScrollGui#ofInventories(Structure, List)} instead. */ @Deprecated - public ScrollVIGuiImpl(@NotNull List inventories, @NotNull Structure structure) { + public ScrollInventoryGuiImpl(@Nullable List<@NotNull VirtualInventory> inventories, @NotNull Structure structure) { super(structure.getWidth(), structure.getHeight(), false, structure); setContent(inventories); } @Override - public void setContent(@NotNull List inventories) { + public void setContent(@Nullable List<@NotNull VirtualInventory> inventories) { this.inventories = inventories != null ? inventories : new ArrayList<>(); updateElements(); update(); diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/impl/ScrollItemsGuiImpl.java b/invui/src/main/java/xyz/xenondevs/invui/gui/impl/ScrollItemsGuiImpl.java index 5276e9a..d2441dc 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/impl/ScrollItemsGuiImpl.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/impl/ScrollItemsGuiImpl.java @@ -5,6 +5,7 @@ import org.jetbrains.annotations.Nullable; import xyz.xenondevs.invui.gui.AbstractScrollGui; import xyz.xenondevs.invui.gui.ScrollGui; import xyz.xenondevs.invui.gui.SlotElement; +import xyz.xenondevs.invui.gui.builder.GuiType; import xyz.xenondevs.invui.gui.structure.Structure; import xyz.xenondevs.invui.item.Item; @@ -12,6 +13,13 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; +/** + * A {@link AbstractScrollGui} that uses {@link Item Items} as content. + * + * @see GuiType + * @see ScrollInventoryGuiImpl + * @see ScrollNestedGuiImpl + */ @SuppressWarnings("DeprecatedIsStillUsed") public final class ScrollItemsGuiImpl extends AbstractScrollGui { @@ -27,7 +35,7 @@ public final class ScrollItemsGuiImpl extends AbstractScrollGui { * @deprecated Use {@link ScrollGui#ofItems(int, int, List, int...)} instead. */ @Deprecated - public ScrollItemsGuiImpl(int width, int height, @Nullable List items, int... contentListSlots) { + public ScrollItemsGuiImpl(int width, int height, @Nullable List<@NotNull Item> items, int... contentListSlots) { super(width, height, false, contentListSlots); setContent(items); } @@ -40,13 +48,13 @@ public final class ScrollItemsGuiImpl extends AbstractScrollGui { * @deprecated Use {@link ScrollGui#ofItems(Structure, List)} instead. */ @Deprecated - public ScrollItemsGuiImpl(@Nullable List items, @NotNull Structure structure) { + public ScrollItemsGuiImpl(@Nullable List<@NotNull Item> items, @NotNull Structure structure) { super(structure.getWidth(), structure.getHeight(), false, structure); setContent(items); } @Override - public void setContent(@NotNull List items) { + public void setContent(@Nullable List<@NotNull Item> items) { this.items = items != null ? items : new ArrayList<>(); update(); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/impl/ScrollNestedGuiImpl.java b/invui/src/main/java/xyz/xenondevs/invui/gui/impl/ScrollNestedGuiImpl.java index fa9efeb..7e023a6 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/impl/ScrollNestedGuiImpl.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/impl/ScrollNestedGuiImpl.java @@ -6,11 +6,19 @@ import xyz.xenondevs.invui.gui.AbstractScrollGui; import xyz.xenondevs.invui.gui.Gui; import xyz.xenondevs.invui.gui.ScrollGui; import xyz.xenondevs.invui.gui.SlotElement; +import xyz.xenondevs.invui.gui.builder.GuiType; import xyz.xenondevs.invui.gui.structure.Structure; import java.util.ArrayList; import java.util.List; +/** + * A {@link AbstractScrollGui} that uses {@link Gui Guis} as content. + * + * @see GuiType + * @see ScrollItemsGuiImpl + * @see ScrollInventoryGuiImpl + */ @SuppressWarnings("DeprecatedIsStillUsed") public final class ScrollNestedGuiImpl extends AbstractScrollGui { @@ -27,7 +35,7 @@ public final class ScrollNestedGuiImpl extends AbstractScrollGui { * @deprecated Use {@link ScrollGui#ofGuis(int, int, List, int...)} instead. */ @Deprecated - public ScrollNestedGuiImpl(int width, int height, @Nullable List guis, int... contentListSlots) { + public ScrollNestedGuiImpl(int width, int height, @Nullable List<@NotNull Gui> guis, int... contentListSlots) { super(width, height, false, contentListSlots); setContent(guis); } @@ -40,13 +48,13 @@ public final class ScrollNestedGuiImpl extends AbstractScrollGui { * @deprecated Use {@link ScrollGui#ofGuis(Structure, List)} instead. */ @Deprecated - public ScrollNestedGuiImpl(@Nullable List guis, @NotNull Structure structure) { + public ScrollNestedGuiImpl(@Nullable List<@NotNull Gui> guis, @NotNull Structure structure) { super(structure.getWidth(), structure.getHeight(), false, structure); setContent(guis); } @Override - public void setContent(@NotNull List guis) { + public void setContent(@Nullable List<@NotNull Gui> guis) { this.guis = guis != null ? guis : new ArrayList<>(); updateElements(); update(); diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/impl/TabGuiImpl.java b/invui/src/main/java/xyz/xenondevs/invui/gui/impl/TabGuiImpl.java index e25d22c..271df2d 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/impl/TabGuiImpl.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/impl/TabGuiImpl.java @@ -6,7 +6,7 @@ import xyz.xenondevs.invui.gui.AbstractTabGui; import xyz.xenondevs.invui.gui.Gui; import xyz.xenondevs.invui.gui.SlotElement; import xyz.xenondevs.invui.gui.TabGui; -import xyz.xenondevs.invui.gui.builder.GuiBuilder; +import xyz.xenondevs.invui.gui.builder.GuiType; import xyz.xenondevs.invui.gui.structure.Structure; import java.util.ArrayList; @@ -17,7 +17,7 @@ import java.util.stream.Collectors; /** * A {@link Gui} that has multiple tabs with which users can switch between {@link Gui}s. * - * @see GuiBuilder + * @see GuiType */ @SuppressWarnings("DeprecatedIsStillUsed") public final class TabGuiImpl extends AbstractTabGui { diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/AbstractWindow.java b/invui/src/main/java/xyz/xenondevs/invui/window/AbstractWindow.java index b1961e8..4b2c3bc 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/AbstractWindow.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/AbstractWindow.java @@ -42,7 +42,7 @@ public abstract class AbstractWindow implements Window, GuiParent { private final UUID viewerUUID; private final boolean retain; private final SlotElement[] elementsDisplayed; - private final ArrayList closeHandlers = new ArrayList<>(); + private List closeHandlers; private ComponentWrapper title; private boolean closeable; private boolean removed; @@ -180,7 +180,10 @@ public abstract class AbstractWindow implements Window, GuiParent { } handleClosed(); - closeHandlers.forEach(Runnable::run); + + if (closeHandlers != null) { + closeHandlers.forEach(Runnable::run); + } } else { if (player.equals(getViewer())) Bukkit.getScheduler().runTaskLater(InvUI.getInstance().getPlugin(), this::show, 0); @@ -274,14 +277,23 @@ public abstract class AbstractWindow implements Window, GuiParent { changeTitle(TextComponent.fromLegacyText(title)); } + @Override + public void setCloseHandlers(@NotNull List<@NotNull Runnable> closeHandlers) { + this.closeHandlers = closeHandlers; + } + @Override public void addCloseHandler(@NotNull Runnable closeHandler) { + if (closeHandlers == null) + closeHandlers = new ArrayList<>(); + closeHandlers.add(closeHandler); } @Override public void removeCloseHandler(@NotNull Runnable closeHandler) { - closeHandlers.remove(closeHandler); + if (closeHandlers != null) + closeHandlers.remove(closeHandler); } @Override diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/Window.java b/invui/src/main/java/xyz/xenondevs/invui/window/Window.java index 3a5bca9..bb856fa 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/Window.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/Window.java @@ -8,6 +8,7 @@ import org.jetbrains.annotations.Nullable; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; import xyz.xenondevs.invui.window.builder.WindowType; +import java.util.List; import java.util.UUID; /** @@ -103,6 +104,13 @@ public interface Window { */ @NotNull UUID getViewerUUID(); + /** + * Replaces the currently registered close handlers with the given list. + * + * @param closeHandlers The new close handlers + */ + void setCloseHandlers(@NotNull List<@NotNull Runnable> closeHandlers); + /** * Adds a close handler that will be called when this window gets closed. * diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/builder/AbstractSingleWindowBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/window/builder/AbstractSingleWindowBuilder.java index c34662e..8c4eaf2 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/builder/AbstractSingleWindowBuilder.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/builder/AbstractSingleWindowBuilder.java @@ -1,5 +1,6 @@ package xyz.xenondevs.invui.window.builder; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import xyz.xenondevs.invui.gui.Gui; import xyz.xenondevs.invui.gui.builder.GuiBuilder; @@ -11,20 +12,22 @@ public abstract class AbstractSingleWindowBuilder guiSupplier; + @Contract("_ -> this") public S setGui(@NotNull Supplier guiSupplier) { this.guiSupplier = guiSupplier; return getThis(); } + @Contract("_ -> this") public S setGui(@NotNull Gui gui) { this.guiSupplier = () -> gui; return getThis(); } - public S setGui(@NotNull GuiBuilder builder) { + @Contract("_ -> this") + public S setGui(@NotNull GuiBuilder builder) { this.guiSupplier = builder::build; return getThis(); } - } diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/builder/AbstractSplitWindowBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/window/builder/AbstractSplitWindowBuilder.java index 95a497f..c93e435 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/builder/AbstractSplitWindowBuilder.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/builder/AbstractSplitWindowBuilder.java @@ -1,5 +1,6 @@ package xyz.xenondevs.invui.window.builder; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import xyz.xenondevs.invui.gui.Gui; import xyz.xenondevs.invui.gui.builder.GuiBuilder; @@ -12,32 +13,38 @@ public abstract class AbstractSplitWindowBuilder upperGuiSupplier; protected Supplier lowerGuiSupplier; + @Contract("_ -> this") public S setUpperGui(@NotNull Supplier guiSupplier) { this.upperGuiSupplier = guiSupplier; return getThis(); } + @Contract("_ -> this") public S setUpperGui(@NotNull Gui gui) { this.upperGuiSupplier = () -> gui; return getThis(); } - public S setUpperGui(@NotNull GuiBuilder builder) { + @Contract("_ -> this") + public S setUpperGui(@NotNull GuiBuilder builder) { this.upperGuiSupplier = builder::build; return getThis(); } + @Contract("_ -> this") public S setLowerGui(@NotNull Supplier guiSupplier) { this.lowerGuiSupplier = guiSupplier; return getThis(); } + @Contract("_ -> this") public S setLowerGui(@NotNull Gui gui) { this.lowerGuiSupplier = () -> gui; return getThis(); } - public S setLowerGui(@NotNull GuiBuilder builder) { + @Contract("_ -> this") + public S setLowerGui(@NotNull GuiBuilder builder) { this.lowerGuiSupplier = builder::build; return getThis(); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/builder/AbstractWindowBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/window/builder/AbstractWindowBuilder.java index b74645f..27b8eee 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/builder/AbstractWindowBuilder.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/builder/AbstractWindowBuilder.java @@ -2,6 +2,7 @@ package xyz.xenondevs.invui.window.builder; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.TextComponent; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import xyz.xenondevs.inventoryaccess.component.BaseComponentWrapper; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; @@ -9,6 +10,7 @@ import xyz.xenondevs.invui.window.Window; import java.util.ArrayList; import java.util.List; +import java.util.function.Consumer; public abstract class AbstractWindowBuilder> implements WindowBuilder { @@ -17,42 +19,51 @@ public abstract class AbstractWindowBuilder closeHandlers; + protected List> modifiers; + @Contract("_ -> this") public S setViewer(@NotNull V viewer) { this.viewer = viewer; return getThis(); } + @Contract("_ -> this") public S setTitle(@NotNull ComponentWrapper title) { this.title = title; return getThis(); } + @Contract("_ -> this") public S setTitle(@NotNull BaseComponent @NotNull [] title) { this.title = new BaseComponentWrapper(title); return getThis(); } + @Contract("_ -> this") public S setTitle(@NotNull String title) { this.title = new BaseComponentWrapper(TextComponent.fromLegacyText(title)); return getThis(); } + @Contract("_ -> this") public S setCloseable(boolean closeable) { this.closeable = closeable; return getThis(); } + @Contract("_ -> this") public S setRetain(boolean retain) { this.retain = retain; return getThis(); } + @Contract("_ -> this") public S setCloseHandlers(List closeHandlers) { this.closeHandlers = closeHandlers; return getThis(); } + @Contract("_ -> this") public S addCloseHandler(Runnable closeHandler) { if (closeHandlers == null) closeHandlers = new ArrayList<>(); @@ -61,12 +72,47 @@ public abstract class AbstractWindowBuilder this") + public S setModifiers(List> modifiers) { + this.modifiers = modifiers; + return getThis(); + } + + @Contract("_ -> this") + public S addModifier(Consumer modifier) { + if (modifiers == null) + modifiers = new ArrayList<>(); + + modifiers.add(modifier); + return getThis(); + } + + protected void applyModifiers(W window) { + if (closeHandlers != null) + window.setCloseHandlers(closeHandlers); + + if (modifiers != null) + modifiers.forEach(modifier -> modifier.accept(window)); + } + + @SuppressWarnings("unchecked") + @Override + public @NotNull AbstractWindowBuilder clone() { + try { + var clone = (AbstractWindowBuilder) super.clone(); + if (title != null) + clone.title = title.clone(); + if (closeHandlers != null) + clone.closeHandlers = new ArrayList<>(closeHandlers); + if (modifiers != null) + clone.modifiers = new ArrayList<>(modifiers); + return clone; + } catch (CloneNotSupportedException e) { + throw new AssertionError(); } } + @Contract(value = "-> this", pure = true) protected abstract S getThis(); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/builder/AnvilSingleWindowBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/window/builder/AnvilSingleWindowBuilder.java index aea0df3..041d134 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/builder/AnvilSingleWindowBuilder.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/builder/AnvilSingleWindowBuilder.java @@ -1,36 +1,55 @@ package xyz.xenondevs.invui.window.builder; import org.bukkit.entity.Player; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import xyz.xenondevs.invui.gui.AbstractGui; import xyz.xenondevs.invui.window.AnvilWindow; import xyz.xenondevs.invui.window.impl.AnvilSingleWindowImpl; +import java.util.ArrayList; +import java.util.List; import java.util.function.Consumer; public final class AnvilSingleWindowBuilder extends AbstractSingleWindowBuilder { - private Consumer renameHandler; + private List> renameHandlers; AnvilSingleWindowBuilder() { } - public void setRenameHandler(@NotNull Consumer renameHandler) { - this.renameHandler = renameHandler; + @Contract("_ -> this") + public AnvilSingleWindowBuilder setRenameHandlers(@NotNull List<@NotNull Consumer> renameHandlers) { + this.renameHandlers = renameHandlers; + return this; + } + + @Contract("_ -> this") + public AnvilSingleWindowBuilder addRenameHandler(@NotNull Consumer renameHandler) { + if (renameHandlers == null) + renameHandlers = new ArrayList<>(); + + renameHandlers.add(renameHandler); + return this; } @Override public @NotNull AnvilWindow build() { + if (viewer == null) + throw new IllegalStateException("Viewer is not defined."); + if (guiSupplier == null) + throw new IllegalStateException("Gui is not defined."); + var window = new AnvilSingleWindowImpl( viewer, title, (AbstractGui) guiSupplier.get(), - renameHandler, + renameHandlers, closeable, retain ); - applyChanges(window); + applyModifiers(window); return window; } @@ -40,4 +59,9 @@ public final class AnvilSingleWindowBuilder extends AbstractSingleWindowBuilder< return null; } + @Override + public @NotNull AnvilSingleWindowBuilder clone() { + return (AnvilSingleWindowBuilder) super.clone(); + } + } diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/builder/AnvilSplitWindowBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/window/builder/AnvilSplitWindowBuilder.java index 563ec74..5cc0e81 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/builder/AnvilSplitWindowBuilder.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/builder/AnvilSplitWindowBuilder.java @@ -1,38 +1,58 @@ package xyz.xenondevs.invui.window.builder; import org.bukkit.entity.Player; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import xyz.xenondevs.invui.gui.AbstractGui; import xyz.xenondevs.invui.window.AnvilWindow; import xyz.xenondevs.invui.window.impl.AnvilSplitWindowImpl; +import java.util.ArrayList; +import java.util.List; import java.util.function.Consumer; public final class AnvilSplitWindowBuilder extends AbstractSplitWindowBuilder { - private Consumer renameHandler; + private List> renameHandlers; AnvilSplitWindowBuilder() { } - public AnvilSplitWindowBuilder setRenameHandler(@NotNull Consumer renameHandler) { - this.renameHandler = renameHandler; + @Contract("_ -> this") + public AnvilSplitWindowBuilder setRenameHandlers(@NotNull List<@NotNull Consumer> renameHandlers) { + this.renameHandlers = renameHandlers; + return this; + } + + @Contract("_ -> this") + public AnvilSplitWindowBuilder addRenameHandler(@NotNull Consumer renameHandler) { + if (renameHandlers == null) + renameHandlers = new ArrayList<>(); + + renameHandlers.add(renameHandler); return this; } @Override public @NotNull AnvilWindow build() { + if (viewer == null) + throw new IllegalStateException("Viewer is not defined."); + if (upperGuiSupplier == null) + throw new IllegalStateException("Upper Gui is not defined."); + if (lowerGuiSupplier == null) + throw new IllegalStateException("Lower Gui is not defined."); + var window = new AnvilSplitWindowImpl( viewer, title, (AbstractGui) upperGuiSupplier.get(), (AbstractGui) lowerGuiSupplier.get(), - renameHandler, + renameHandlers, closeable, retain ); - applyChanges(window); + applyModifiers(window); return window; } @@ -42,4 +62,9 @@ public final class AnvilSplitWindowBuilder extends AbstractSplitWindowBuilder this") public NormalSingleWindowBuilder setViewer(@NotNull OfflinePlayer player) { setViewer(player.getUniqueId()); return this; @@ -20,6 +22,11 @@ public final class NormalSingleWindowBuilder extends AbstractSingleWindowBuilder @Override public @NotNull Window build() { + if (viewer == null) + throw new IllegalStateException("Viewer is not defined."); + if (guiSupplier == null) + throw new IllegalStateException("Gui is not defined."); + var window = new NormalSingleWindowImpl( viewer, title, @@ -28,7 +35,7 @@ public final class NormalSingleWindowBuilder extends AbstractSingleWindowBuilder retain ); - applyChanges(window); + applyModifiers(window); return window; } @@ -38,4 +45,9 @@ public final class NormalSingleWindowBuilder extends AbstractSingleWindowBuilder return this; } + @Override + public @NotNull NormalSingleWindowBuilder clone() { + return (NormalSingleWindowBuilder) super.clone(); + } + } diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/builder/NormalSplitWindowBuilder.java b/invui/src/main/java/xyz/xenondevs/invui/window/builder/NormalSplitWindowBuilder.java index 53473e6..dcf76e7 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/builder/NormalSplitWindowBuilder.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/builder/NormalSplitWindowBuilder.java @@ -13,6 +13,13 @@ public final class NormalSplitWindowBuilder extends AbstractSplitWindowBuilder { +public interface WindowBuilder extends Cloneable { + @Contract("-> new") @NotNull W build(); + @Contract("-> new") + @NotNull WindowBuilder clone(); + } diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/impl/AnvilSingleWindowImpl.java b/invui/src/main/java/xyz/xenondevs/invui/window/impl/AnvilSingleWindowImpl.java index 47fb168..7b3ee3a 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/impl/AnvilSingleWindowImpl.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/impl/AnvilSingleWindowImpl.java @@ -2,6 +2,8 @@ package xyz.xenondevs.invui.window.impl; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import xyz.xenondevs.inventoryaccess.InventoryAccess; import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; @@ -9,15 +11,23 @@ import xyz.xenondevs.invui.gui.AbstractGui; import xyz.xenondevs.invui.window.AbstractSingleWindow; import xyz.xenondevs.invui.window.AnvilWindow; +import java.util.List; import java.util.function.Consumer; public final class AnvilSingleWindowImpl extends AbstractSingleWindow implements AnvilWindow { private final AnvilInventory anvilInventory; - public AnvilSingleWindowImpl(Player player, ComponentWrapper title, AbstractGui gui, Consumer renameHandler, boolean closable, boolean retain) { + public AnvilSingleWindowImpl( + @NotNull Player player, + @Nullable ComponentWrapper title, + @NotNull AbstractGui gui, + @Nullable List<@NotNull Consumer<@NotNull String>> renameHandlers, + boolean closable, + boolean retain + ) { super(player.getUniqueId(), title, gui, null, false, closable, retain); - anvilInventory = InventoryAccess.createAnvilInventory(player, title, renameHandler); + anvilInventory = InventoryAccess.createAnvilInventory(player, title, renameHandlers); inventory = anvilInventory.getBukkitInventory(); initItems(); diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/impl/AnvilSplitWindowImpl.java b/invui/src/main/java/xyz/xenondevs/invui/window/impl/AnvilSplitWindowImpl.java index 18cc7d1..22e0159 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/impl/AnvilSplitWindowImpl.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/impl/AnvilSplitWindowImpl.java @@ -2,6 +2,8 @@ package xyz.xenondevs.invui.window.impl; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import xyz.xenondevs.inventoryaccess.InventoryAccess; import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; @@ -9,16 +11,25 @@ import xyz.xenondevs.invui.gui.AbstractGui; import xyz.xenondevs.invui.window.AbstractSplitWindow; import xyz.xenondevs.invui.window.AnvilWindow; +import java.util.List; import java.util.function.Consumer; public final class AnvilSplitWindowImpl extends AbstractSplitWindow implements AnvilWindow { private final AnvilInventory anvilInventory; - public AnvilSplitWindowImpl(Player player, ComponentWrapper title, AbstractGui upperGui, AbstractGui lowerGui, Consumer renameHandler, boolean closeable, boolean retain) { + public AnvilSplitWindowImpl( + @NotNull Player player, + @Nullable ComponentWrapper title, + @NotNull AbstractGui upperGui, + @NotNull AbstractGui lowerGui, + @Nullable List<@NotNull Consumer<@NotNull String>> renameHandlers, + boolean closeable, + boolean retain + ) { super(player, title, upperGui, lowerGui, null, false, closeable, retain); - anvilInventory = InventoryAccess.createAnvilInventory(player, title, renameHandler); + anvilInventory = InventoryAccess.createAnvilInventory(player, title, renameHandlers); upperInventory = anvilInventory.getBukkitInventory(); initUpperItems(); diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/impl/CartographySingleWindowImpl.java b/invui/src/main/java/xyz/xenondevs/invui/window/impl/CartographySingleWindowImpl.java index 5d52549..5608093 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/impl/CartographySingleWindowImpl.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/impl/CartographySingleWindowImpl.java @@ -5,6 +5,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.MapMeta; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import xyz.xenondevs.inventoryaccess.InventoryAccess; import xyz.xenondevs.inventoryaccess.abstraction.inventory.CartographyInventory; @@ -18,13 +19,18 @@ import xyz.xenondevs.invui.window.CartographyWindow; import java.util.List; -@SuppressWarnings("deprecation") public final class CartographySingleWindowImpl extends AbstractSingleWindow implements CartographyWindow { private final CartographyInventory cartographyInventory; private int mapId; - public CartographySingleWindowImpl(Player player, ComponentWrapper title, AbstractGui gui, boolean closeable, boolean retain) { + public CartographySingleWindowImpl( + @NotNull Player player, + @Nullable ComponentWrapper title, + @NotNull AbstractGui gui, + boolean closeable, + boolean retain + ) { super(player.getUniqueId(), title, gui, null, false, closeable, retain); if (gui.getWidth() != 2 || gui.getHeight() != 1) throw new IllegalArgumentException("Gui has to be 2x1"); @@ -41,7 +47,7 @@ public final class CartographySingleWindowImpl extends AbstractSingleWindow impl InventoryAccess.getPlayerUtils().sendMapUpdate(getViewer(), mapId, (byte) 0, false, patch, icons); } - @SuppressWarnings("deprecation") + @SuppressWarnings({"deprecation", "DuplicatedCode"}) @Override public void resetMap() { mapId = -MathUtils.RANDOM.nextInt(Integer.MAX_VALUE); diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/impl/CartographySplitWindowImpl.java b/invui/src/main/java/xyz/xenondevs/invui/window/impl/CartographySplitWindowImpl.java index b4c47cd..45ce4b9 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/impl/CartographySplitWindowImpl.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/impl/CartographySplitWindowImpl.java @@ -4,6 +4,7 @@ import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.MapMeta; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import xyz.xenondevs.inventoryaccess.InventoryAccess; import xyz.xenondevs.inventoryaccess.abstraction.inventory.CartographyInventory; @@ -24,7 +25,14 @@ public final class CartographySplitWindowImpl extends AbstractSplitWindow implem private final CartographyInventory cartographyInventory; private int mapId; - public CartographySplitWindowImpl(Player player, ComponentWrapper title, AbstractGui upperGui, AbstractGui lowerGui, boolean closeable, boolean retain) { + public CartographySplitWindowImpl( + @NotNull Player player, + @Nullable ComponentWrapper title, + @NotNull AbstractGui upperGui, + @NotNull AbstractGui lowerGui, + boolean closeable, + boolean retain + ) { super(player, title, createWrappingGui(upperGui), lowerGui, null, false, closeable, retain); cartographyInventory = InventoryAccess.createCartographyInventory(player, title); @@ -35,6 +43,7 @@ public final class CartographySplitWindowImpl extends AbstractSplitWindow implem register(); } + @SuppressWarnings("deprecation") private static AbstractGui createWrappingGui(Gui upperGui) { if (upperGui.getWidth() != 2 || upperGui.getHeight() != 1) throw new IllegalArgumentException("Gui has to be 2x1"); @@ -50,7 +59,7 @@ public final class CartographySplitWindowImpl extends AbstractSplitWindow implem } @Override - @SuppressWarnings("deprecation") + @SuppressWarnings({"deprecation", "DuplicatedCode"}) public void resetMap() { mapId = -MathUtils.RANDOM.nextInt(Integer.MAX_VALUE); ItemStack map = new ItemStack(Material.FILLED_MAP); diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/impl/NormalMergedWindowImpl.java b/invui/src/main/java/xyz/xenondevs/invui/window/impl/NormalMergedWindowImpl.java index 0e0f50d..373f05f 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/impl/NormalMergedWindowImpl.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/impl/NormalMergedWindowImpl.java @@ -3,6 +3,8 @@ package xyz.xenondevs.invui.window.impl; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; import xyz.xenondevs.invui.gui.AbstractGui; import xyz.xenondevs.invui.gui.Gui; @@ -10,7 +12,13 @@ import xyz.xenondevs.invui.window.AbstractMergedWindow; public final class NormalMergedWindowImpl extends AbstractMergedWindow { - public NormalMergedWindowImpl(Player player, ComponentWrapper title, AbstractGui gui, boolean closeable, boolean retain) { + public NormalMergedWindowImpl( + @NotNull Player player, + @Nullable ComponentWrapper title, + @NotNull AbstractGui gui, + boolean closeable, + boolean retain + ) { super(player, title, gui, createInventory(gui), closeable, retain); register(); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/impl/NormalSingleWindowImpl.java b/invui/src/main/java/xyz/xenondevs/invui/window/impl/NormalSingleWindowImpl.java index 653e42b..1d7b33a 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/impl/NormalSingleWindowImpl.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/impl/NormalSingleWindowImpl.java @@ -1,5 +1,7 @@ package xyz.xenondevs.invui.window.impl; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; import xyz.xenondevs.invui.gui.AbstractGui; import xyz.xenondevs.invui.util.InventoryUtils; @@ -9,7 +11,13 @@ import java.util.UUID; public final class NormalSingleWindowImpl extends AbstractSingleWindow { - public NormalSingleWindowImpl(UUID viewerUUID, ComponentWrapper title, AbstractGui gui, boolean closeable, boolean retain) { + public NormalSingleWindowImpl( + @NotNull UUID viewerUUID, + @Nullable ComponentWrapper title, + @NotNull AbstractGui gui, + boolean closeable, + boolean retain + ) { super(viewerUUID, title, gui, InventoryUtils.createMatchingInventory(gui, ""), true, closeable, retain); register(); } diff --git a/invui/src/main/java/xyz/xenondevs/invui/window/impl/NormalSplitWindowImpl.java b/invui/src/main/java/xyz/xenondevs/invui/window/impl/NormalSplitWindowImpl.java index 9021231..2a2e7fb 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/window/impl/NormalSplitWindowImpl.java +++ b/invui/src/main/java/xyz/xenondevs/invui/window/impl/NormalSplitWindowImpl.java @@ -1,6 +1,8 @@ package xyz.xenondevs.invui.window.impl; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; import xyz.xenondevs.invui.gui.AbstractGui; import xyz.xenondevs.invui.util.InventoryUtils; @@ -8,7 +10,14 @@ import xyz.xenondevs.invui.window.AbstractSplitWindow; public final class NormalSplitWindowImpl extends AbstractSplitWindow { - public NormalSplitWindowImpl(Player player, ComponentWrapper title, AbstractGui upperGui, AbstractGui lowerGui, boolean closeable, boolean retain) { + public NormalSplitWindowImpl( + @NotNull Player player, + @Nullable ComponentWrapper title, + @NotNull AbstractGui upperGui, + @NotNull AbstractGui lowerGui, + boolean closeable, + boolean retain + ) { super(player, title, upperGui, lowerGui, InventoryUtils.createMatchingInventory(upperGui, ""), true, closeable, retain); register(); }