From 09ce45f5c9be3ca03bc5c64abc9e31e88ef5d234 Mon Sep 17 00:00:00 2001 From: hsgamer Date: Tue, 12 Nov 2024 14:36:35 +0700 Subject: [PATCH] make gui as a static ui --- .../bukkit/gui/button/impl/InputButton.java | 2 +- .../bukkit/gui/button/impl/OutputButton.java | 2 +- .../hsgamer/hscore/bukkit/gui/BukkitGUI.java | 2 +- .../bukkit/gui/event/BukkitClickEvent.java | 12 -- .../gui/advanced/AdvancedButtonMap.java | 22 ++- .../minecraft/gui/button}/ActionItem.java | 3 +- .../hscore/minecraft/gui/button/Button.java | 1 - .../gui/button/impl/AnimatedButton.java | 2 +- .../gui/button/impl/DummyButton.java | 2 +- .../minecraft/gui/button/impl/ListButton.java | 2 +- .../minecraft/gui/button/impl/NullButton.java | 2 +- .../gui/button/impl/PredicateButton.java | 2 +- .../gui/button/impl/SimpleButton.java | 2 +- .../minecraft/gui/simple/SimpleButtonMap.java | 22 ++- .../me/hsgamer/hscore/minecraft/gui/GUI.java | 129 +++--------------- .../minecraft/gui/event/ClickEvent.java | 14 -- .../hscore/minestom/gui/MinestomGUI.java | 2 +- .../gui/event/MinestomClickEvent.java | 12 -- .../java/me/hsgamer/hscore/ui/BaseUI.java | 51 +++++-- ui/src/main/java/me/hsgamer/hscore/ui/UI.java | 3 +- 20 files changed, 110 insertions(+), 179 deletions(-) rename minecraft/{gui/src/main/java/me/hsgamer/hscore/minecraft/gui/object => gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button}/ActionItem.java (94%) diff --git a/bukkit/gui-button/src/main/java/me/hsgamer/hscore/bukkit/gui/button/impl/InputButton.java b/bukkit/gui-button/src/main/java/me/hsgamer/hscore/bukkit/gui/button/impl/InputButton.java index f8594ae851..f49ba76021 100644 --- a/bukkit/gui-button/src/main/java/me/hsgamer/hscore/bukkit/gui/button/impl/InputButton.java +++ b/bukkit/gui-button/src/main/java/me/hsgamer/hscore/bukkit/gui/button/impl/InputButton.java @@ -3,7 +3,7 @@ import me.hsgamer.hscore.bukkit.gui.event.BukkitClickEvent; import me.hsgamer.hscore.bukkit.gui.object.BukkitItem; import me.hsgamer.hscore.minecraft.gui.button.Button; -import me.hsgamer.hscore.minecraft.gui.object.ActionItem; +import me.hsgamer.hscore.minecraft.gui.button.ActionItem; import org.bukkit.Material; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.ItemStack; diff --git a/bukkit/gui-button/src/main/java/me/hsgamer/hscore/bukkit/gui/button/impl/OutputButton.java b/bukkit/gui-button/src/main/java/me/hsgamer/hscore/bukkit/gui/button/impl/OutputButton.java index 6191208bf5..6a8d17b44a 100644 --- a/bukkit/gui-button/src/main/java/me/hsgamer/hscore/bukkit/gui/button/impl/OutputButton.java +++ b/bukkit/gui-button/src/main/java/me/hsgamer/hscore/bukkit/gui/button/impl/OutputButton.java @@ -3,7 +3,7 @@ import me.hsgamer.hscore.bukkit.gui.event.BukkitClickEvent; import me.hsgamer.hscore.bukkit.gui.object.BukkitItem; import me.hsgamer.hscore.minecraft.gui.button.Button; -import me.hsgamer.hscore.minecraft.gui.object.ActionItem; +import me.hsgamer.hscore.minecraft.gui.button.ActionItem; import org.bukkit.Material; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.ItemStack; diff --git a/bukkit/gui/src/main/java/me/hsgamer/hscore/bukkit/gui/BukkitGUI.java b/bukkit/gui/src/main/java/me/hsgamer/hscore/bukkit/gui/BukkitGUI.java index e538487550..dd2cd46a61 100644 --- a/bukkit/gui/src/main/java/me/hsgamer/hscore/bukkit/gui/BukkitGUI.java +++ b/bukkit/gui/src/main/java/me/hsgamer/hscore/bukkit/gui/BukkitGUI.java @@ -50,7 +50,7 @@ public Inventory getInventory() { } @Override - protected void setItem(int slot, @Nullable Item item) { + public void setItem(int slot, @Nullable Item item) { if (item == null) { inventory.setItem(slot, new ItemStack(Material.AIR)); } else if (item instanceof BukkitItem) { diff --git a/bukkit/gui/src/main/java/me/hsgamer/hscore/bukkit/gui/event/BukkitClickEvent.java b/bukkit/gui/src/main/java/me/hsgamer/hscore/bukkit/gui/event/BukkitClickEvent.java index 0c81f6fc26..16b2ce69fb 100644 --- a/bukkit/gui/src/main/java/me/hsgamer/hscore/bukkit/gui/event/BukkitClickEvent.java +++ b/bukkit/gui/src/main/java/me/hsgamer/hscore/bukkit/gui/event/BukkitClickEvent.java @@ -7,8 +7,6 @@ * The Bukkit implementation of {@link ClickEvent} */ public class BukkitClickEvent extends BukkitInventoryEvent implements BukkitCancellableEvent, ClickEvent { - private boolean buttonExecute = true; - /** * Create a new event * @@ -22,14 +20,4 @@ public BukkitClickEvent(InventoryClickEvent event) { public int getSlot() { return event.getRawSlot(); } - - @Override - public boolean isButtonExecute() { - return buttonExecute; - } - - @Override - public void setButtonExecute(boolean buttonExecute) { - this.buttonExecute = buttonExecute; - } } diff --git a/minecraft/gui-advanced/src/main/java/me/hsgamer/hscore/minecraft/gui/advanced/AdvancedButtonMap.java b/minecraft/gui-advanced/src/main/java/me/hsgamer/hscore/minecraft/gui/advanced/AdvancedButtonMap.java index ede2ad0195..d9d4057a02 100644 --- a/minecraft/gui-advanced/src/main/java/me/hsgamer/hscore/minecraft/gui/advanced/AdvancedButtonMap.java +++ b/minecraft/gui-advanced/src/main/java/me/hsgamer/hscore/minecraft/gui/advanced/AdvancedButtonMap.java @@ -2,13 +2,17 @@ import me.hsgamer.hscore.minecraft.gui.GUI; import me.hsgamer.hscore.minecraft.gui.button.Button; +import me.hsgamer.hscore.minecraft.gui.event.ClickEvent; +import me.hsgamer.hscore.minecraft.gui.event.ViewerEvent; import me.hsgamer.hscore.minecraft.gui.mask.Mask; -import me.hsgamer.hscore.minecraft.gui.object.ActionItem; +import me.hsgamer.hscore.minecraft.gui.button.ActionItem; import me.hsgamer.hscore.minecraft.gui.object.InventorySize; +import me.hsgamer.hscore.minecraft.gui.object.Item; import me.hsgamer.hscore.ui.property.Initializable; import org.jetbrains.annotations.NotNull; import java.util.*; +import java.util.function.Consumer; import java.util.stream.Collectors; /** @@ -109,6 +113,20 @@ public void stop() { * @param gui the GUI */ public void apply(UUID uuid, GUI gui) { - gui.updateItems(getButtons(uuid, gui.getInventorySize())); + Map buttons = getButtons(uuid, gui.getInventorySize()); + Map items = buttons.entrySet().stream() + .filter(entry -> entry.getValue().getItem() != null) + .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getItem())); + gui.setItems(items); + gui.setViewerEventConsumer(event -> { + if (event instanceof ClickEvent) { + ClickEvent clickEvent = (ClickEvent) event; + ActionItem actionItem = buttons.get(clickEvent.getSlot()); + if (actionItem == null) return; + Consumer action = actionItem.getAction(); + if (action == null) return; + action.accept(event); + } + }); } } diff --git a/minecraft/gui/src/main/java/me/hsgamer/hscore/minecraft/gui/object/ActionItem.java b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/ActionItem.java similarity index 94% rename from minecraft/gui/src/main/java/me/hsgamer/hscore/minecraft/gui/object/ActionItem.java rename to minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/ActionItem.java index 2fe37aee42..22dba9be23 100644 --- a/minecraft/gui/src/main/java/me/hsgamer/hscore/minecraft/gui/object/ActionItem.java +++ b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/ActionItem.java @@ -1,7 +1,8 @@ -package me.hsgamer.hscore.minecraft.gui.object; +package me.hsgamer.hscore.minecraft.gui.button; import me.hsgamer.hscore.minecraft.gui.event.ClickEvent; import me.hsgamer.hscore.minecraft.gui.event.ViewerEvent; +import me.hsgamer.hscore.minecraft.gui.object.Item; import org.jetbrains.annotations.Nullable; import java.util.function.Consumer; diff --git a/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/Button.java b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/Button.java index 93dc07d9c5..fce3ecbcdc 100644 --- a/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/Button.java +++ b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/Button.java @@ -1,6 +1,5 @@ package me.hsgamer.hscore.minecraft.gui.button; -import me.hsgamer.hscore.minecraft.gui.object.ActionItem; import me.hsgamer.hscore.ui.property.Initializable; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/AnimatedButton.java b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/AnimatedButton.java index c41ca2f33a..789fa13bc9 100644 --- a/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/AnimatedButton.java +++ b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/AnimatedButton.java @@ -2,8 +2,8 @@ import me.hsgamer.hscore.animate.Animation; import me.hsgamer.hscore.minecraft.gui.GUIProperties; +import me.hsgamer.hscore.minecraft.gui.button.ActionItem; import me.hsgamer.hscore.minecraft.gui.button.Button; -import me.hsgamer.hscore.minecraft.gui.object.ActionItem; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; diff --git a/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/DummyButton.java b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/DummyButton.java index 0f05acc439..6c6548239e 100644 --- a/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/DummyButton.java +++ b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/DummyButton.java @@ -1,7 +1,7 @@ package me.hsgamer.hscore.minecraft.gui.button.impl; +import me.hsgamer.hscore.minecraft.gui.button.ActionItem; import me.hsgamer.hscore.minecraft.gui.button.Button; -import me.hsgamer.hscore.minecraft.gui.object.ActionItem; import me.hsgamer.hscore.minecraft.gui.object.Item; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/ListButton.java b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/ListButton.java index 43e32e8253..9640ec408a 100644 --- a/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/ListButton.java +++ b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/ListButton.java @@ -1,7 +1,7 @@ package me.hsgamer.hscore.minecraft.gui.button.impl; +import me.hsgamer.hscore.minecraft.gui.button.ActionItem; import me.hsgamer.hscore.minecraft.gui.button.Button; -import me.hsgamer.hscore.minecraft.gui.object.ActionItem; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; diff --git a/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/NullButton.java b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/NullButton.java index bd15574cc9..ba4bc4f655 100644 --- a/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/NullButton.java +++ b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/NullButton.java @@ -1,8 +1,8 @@ package me.hsgamer.hscore.minecraft.gui.button.impl; +import me.hsgamer.hscore.minecraft.gui.button.ActionItem; import me.hsgamer.hscore.minecraft.gui.button.Button; import me.hsgamer.hscore.minecraft.gui.event.ClickEvent; -import me.hsgamer.hscore.minecraft.gui.object.ActionItem; import org.jetbrains.annotations.NotNull; import java.util.UUID; diff --git a/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/PredicateButton.java b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/PredicateButton.java index a54341de9c..1d3aedf597 100644 --- a/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/PredicateButton.java +++ b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/PredicateButton.java @@ -1,9 +1,9 @@ package me.hsgamer.hscore.minecraft.gui.button.impl; +import me.hsgamer.hscore.minecraft.gui.button.ActionItem; import me.hsgamer.hscore.minecraft.gui.button.Button; import me.hsgamer.hscore.minecraft.gui.event.ClickEvent; import me.hsgamer.hscore.minecraft.gui.event.ViewerEvent; -import me.hsgamer.hscore.minecraft.gui.object.ActionItem; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; diff --git a/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/SimpleButton.java b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/SimpleButton.java index 2de8493440..36bc9c457e 100644 --- a/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/SimpleButton.java +++ b/minecraft/gui-button/src/main/java/me/hsgamer/hscore/minecraft/gui/button/impl/SimpleButton.java @@ -1,8 +1,8 @@ package me.hsgamer.hscore.minecraft.gui.button.impl; +import me.hsgamer.hscore.minecraft.gui.button.ActionItem; import me.hsgamer.hscore.minecraft.gui.button.Button; import me.hsgamer.hscore.minecraft.gui.event.ClickEvent; -import me.hsgamer.hscore.minecraft.gui.object.ActionItem; import me.hsgamer.hscore.minecraft.gui.object.Item; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/minecraft/gui-simple/src/main/java/me/hsgamer/hscore/minecraft/gui/simple/SimpleButtonMap.java b/minecraft/gui-simple/src/main/java/me/hsgamer/hscore/minecraft/gui/simple/SimpleButtonMap.java index 0a44af108b..1d76bb18d8 100644 --- a/minecraft/gui-simple/src/main/java/me/hsgamer/hscore/minecraft/gui/simple/SimpleButtonMap.java +++ b/minecraft/gui-simple/src/main/java/me/hsgamer/hscore/minecraft/gui/simple/SimpleButtonMap.java @@ -2,12 +2,16 @@ import me.hsgamer.hscore.minecraft.gui.GUI; import me.hsgamer.hscore.minecraft.gui.button.Button; -import me.hsgamer.hscore.minecraft.gui.object.ActionItem; +import me.hsgamer.hscore.minecraft.gui.button.ActionItem; +import me.hsgamer.hscore.minecraft.gui.event.ClickEvent; +import me.hsgamer.hscore.minecraft.gui.event.ViewerEvent; import me.hsgamer.hscore.minecraft.gui.object.InventorySize; +import me.hsgamer.hscore.minecraft.gui.object.Item; import me.hsgamer.hscore.ui.property.Initializable; import org.jetbrains.annotations.NotNull; import java.util.*; +import java.util.function.Consumer; import java.util.function.IntFunction; import java.util.stream.Collectors; @@ -128,6 +132,20 @@ public void stop() { * @param gui the GUI */ public void apply(UUID uuid, GUI gui) { - gui.updateItems(getButtons(uuid, gui.getInventorySize())); + Map buttons = getButtons(uuid, gui.getInventorySize()); + Map items = buttons.entrySet().stream() + .filter(entry -> entry.getValue().getItem() != null) + .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getItem())); + gui.setItems(items); + gui.setViewerEventConsumer(event -> { + if (event instanceof ClickEvent) { + ClickEvent clickEvent = (ClickEvent) event; + ActionItem actionItem = buttons.get(clickEvent.getSlot()); + if (actionItem == null) return; + Consumer action = actionItem.getAction(); + if (action == null) return; + action.accept(event); + } + }); } } diff --git a/minecraft/gui/src/main/java/me/hsgamer/hscore/minecraft/gui/GUI.java b/minecraft/gui/src/main/java/me/hsgamer/hscore/minecraft/gui/GUI.java index bf4f9d2cff..42967a521b 100644 --- a/minecraft/gui/src/main/java/me/hsgamer/hscore/minecraft/gui/GUI.java +++ b/minecraft/gui/src/main/java/me/hsgamer/hscore/minecraft/gui/GUI.java @@ -1,32 +1,26 @@ package me.hsgamer.hscore.minecraft.gui; -import me.hsgamer.hscore.minecraft.gui.event.ClickEvent; import me.hsgamer.hscore.minecraft.gui.event.CloseEvent; -import me.hsgamer.hscore.minecraft.gui.event.OpenEvent; import me.hsgamer.hscore.minecraft.gui.event.ViewerEvent; -import me.hsgamer.hscore.minecraft.gui.object.ActionItem; import me.hsgamer.hscore.minecraft.gui.object.InventorySize; import me.hsgamer.hscore.minecraft.gui.object.Item; import me.hsgamer.hscore.ui.BaseUI; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Collections; -import java.util.HashMap; import java.util.Map; import java.util.UUID; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; -import java.util.function.UnaryOperator; /** * The UI for Minecraft */ public abstract class GUI extends BaseUI { /** - * The buttons reference + * The {@link ViewerEvent} consumer reference */ - private final AtomicReference> itemsRef = new AtomicReference<>(Collections.emptyMap()); + private final AtomicReference> viewerEventConsumerRef = new AtomicReference<>(null); /** * Set the item to the slot @@ -34,7 +28,7 @@ public abstract class GUI extends BaseUI { * @param slot the slot * @param item the item */ - protected abstract void setItem(int slot, @Nullable Item item); + public abstract void setItem(int slot, @Nullable Item item); /** * Get the size of the inventory @@ -51,67 +45,22 @@ public abstract class GUI extends BaseUI { public abstract void open(UUID uuid); /** - * Handle open event + * Set the {@link ViewerEvent} consumer * - * @param event the event + * @param consumer the consumer */ - protected void onOpen(@NotNull final OpenEvent event) { - // EMPTY + public void setViewerEventConsumer(@Nullable final Consumer consumer) { + viewerEventConsumerRef.set(consumer); } /** - * Handle click event - * - * @param event the event - */ - protected void onClick(@NotNull final ClickEvent event) { - // EMPTY - } - - /** - * Handle close event - * - * @param event the event - */ - protected void onClose(@NotNull final CloseEvent event) { - // EMPTY - } - - /** - * Modify the items - * - * @param operator the operator - */ - public void modifyItems(UnaryOperator> operator) { - this.itemsRef.updateAndGet(buttons -> { - Map newButtons = new HashMap<>(buttons); - return operator.apply(newButtons); - }); - } - - /** - * Add an item - * - * @param slot the slot - * @param item the item - */ - public void addItem(int slot, ActionItem item) { - modifyItems(items -> { - items.put(slot, item); - return items; - }); - } - - /** - * Remove an item + * Set the items * - * @param slot the slot + * @param items the items */ - public void removeItem(int slot) { - modifyItems(items -> { - items.remove(slot); - return items; - }); + public void setItems(Map items) { + InventorySize size = getInventorySize(); + size.getSlots().forEach(slot -> setItem(slot, items.get(slot))); } /** @@ -121,69 +70,25 @@ public void clearItems() { setItems(Collections.emptyMap()); } - /** - * Get the items - * - * @return the items - */ - public Map getItems() { - return this.itemsRef.get(); - } - - /** - * Set the items - * - * @param items the items - */ - public void setItems(Map items) { - this.itemsRef.set(items); - } - - /** - * Set the items and update the GUI - * - * @param items the items - */ - public void updateItems(Map items) { - setItems(items); - update(); - } - @Override public void init() { - addEventConsumer(OpenEvent.class, this::onOpen); - - addEventConsumer(ClickEvent.class, this::onClick); - addEventConsumer(ClickEvent.class, event -> { - if (event.isButtonExecute()) { - int slot = event.getSlot(); - ActionItem item = getItems().get(slot); - if (item == null) return; - Consumer action = item.getAction(); - if (action == null) return; - action.accept(event); + addEventConsumer(ViewerEvent.class, event -> { + Consumer consumer = viewerEventConsumerRef.get(); + if (consumer != null) { + consumer.accept(event); } }); - addEventConsumer(CloseEvent.class, this::onClose); addEventConsumer(CloseEvent.class, event -> { if (event.isRemoveDisplay()) { stop(); } }); - - update(); } @Override public void stop() { + super.stop(); clearItems(); } - - @Override - public void update() { - InventorySize size = getInventorySize(); - Map items = getItems(); - size.getSlots().forEach(slot -> setItem(slot, items.getOrDefault(slot, ActionItem.EMPTY).getItem())); - } } diff --git a/minecraft/gui/src/main/java/me/hsgamer/hscore/minecraft/gui/event/ClickEvent.java b/minecraft/gui/src/main/java/me/hsgamer/hscore/minecraft/gui/event/ClickEvent.java index 7ffacd7d0d..de7ca4fc16 100644 --- a/minecraft/gui/src/main/java/me/hsgamer/hscore/minecraft/gui/event/ClickEvent.java +++ b/minecraft/gui/src/main/java/me/hsgamer/hscore/minecraft/gui/event/ClickEvent.java @@ -10,18 +10,4 @@ public interface ClickEvent extends ViewerEvent, CancellableEvent { * @return the slot */ int getSlot(); - - /** - * Check if the buttons can be executed - * - * @return true if the buttons can be executed - */ - boolean isButtonExecute(); - - /** - * Set if the buttons can be executed - * - * @param buttonExecute true if the buttons can be executed - */ - void setButtonExecute(boolean buttonExecute); } diff --git a/minestom/gui/src/main/java/me/hsgamer/hscore/minestom/gui/MinestomGUI.java b/minestom/gui/src/main/java/me/hsgamer/hscore/minestom/gui/MinestomGUI.java index 8dc1037a84..527468e889 100644 --- a/minestom/gui/src/main/java/me/hsgamer/hscore/minestom/gui/MinestomGUI.java +++ b/minestom/gui/src/main/java/me/hsgamer/hscore/minestom/gui/MinestomGUI.java @@ -70,7 +70,7 @@ public void stop() { } @Override - protected void setItem(int slot, @Nullable Item item) { + public void setItem(int slot, @Nullable Item item) { if (item == null) { inventory.setItemStack(slot, net.minestom.server.item.ItemStack.AIR); } else if (item instanceof MinestomItem minestomItem) { diff --git a/minestom/gui/src/main/java/me/hsgamer/hscore/minestom/gui/event/MinestomClickEvent.java b/minestom/gui/src/main/java/me/hsgamer/hscore/minestom/gui/event/MinestomClickEvent.java index 48a6d2e42c..44b86030cc 100644 --- a/minestom/gui/src/main/java/me/hsgamer/hscore/minestom/gui/event/MinestomClickEvent.java +++ b/minestom/gui/src/main/java/me/hsgamer/hscore/minestom/gui/event/MinestomClickEvent.java @@ -7,8 +7,6 @@ * The click event for Minestom */ public class MinestomClickEvent extends MinestomEvent implements ClickEvent, MinestomCancellableEvent, MinestomViewerEvent { - private boolean buttonExecute = true; - /** * Create a new event * @@ -22,14 +20,4 @@ public MinestomClickEvent(InventoryPreClickEvent event) { public int getSlot() { return event.getSlot(); } - - @Override - public boolean isButtonExecute() { - return buttonExecute; - } - - @Override - public void setButtonExecute(boolean buttonExecute) { - this.buttonExecute = buttonExecute; - } } diff --git a/ui/src/main/java/me/hsgamer/hscore/ui/BaseUI.java b/ui/src/main/java/me/hsgamer/hscore/ui/BaseUI.java index 3f4482882b..199d7dbedb 100644 --- a/ui/src/main/java/me/hsgamer/hscore/ui/BaseUI.java +++ b/ui/src/main/java/me/hsgamer/hscore/ui/BaseUI.java @@ -5,12 +5,22 @@ import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; +import java.util.function.Predicate; /** * The base implementation for {@link UI} */ public abstract class BaseUI implements UI { - private final List> eventConsumerList = new ArrayList<>(); + private final List> eventConsumerList = new ArrayList<>(); + + /** + * Add an event consumer + * + * @param eventConsumer the consumer, return true to continue the event handling + */ + public void addEventConsumer(@NotNull Predicate eventConsumer) { + eventConsumerList.add(eventConsumer); + } /** * Add an event consumer @@ -18,32 +28,47 @@ public abstract class BaseUI implements UI { * @param eventConsumer the consumer */ public void addEventConsumer(@NotNull Consumer eventConsumer) { - eventConsumerList.add(eventConsumer); + addEventConsumer(event -> { + eventConsumer.accept(event); + return true; + }); } /** * Add an event consumer * * @param eventClass the class of the event - * @param eventConsumer the consumer + * @param eventConsumer the consumer, return true to continue the event handling * @param the type of the event + * + * @return the registered consumer */ - public Consumer addEventConsumer(@NotNull Class eventClass, @NotNull Consumer eventConsumer) { - Consumer consumer = event -> { - if (eventClass.isInstance(event)) { - eventConsumer.accept(eventClass.cast(event)); - } - }; + public Predicate addEventConsumer(@NotNull Class eventClass, @NotNull Predicate eventConsumer) { + Predicate consumer = event -> !eventClass.isInstance(event) || eventConsumer.test(eventClass.cast(event)); addEventConsumer(consumer); return consumer; } + /** + * Add an event consumer + * + * @param eventClass the class of the event + * @param eventConsumer the consumer + * @param the type of the event + */ + public Predicate addEventConsumer(@NotNull Class eventClass, @NotNull Consumer eventConsumer) { + return addEventConsumer(eventClass, event -> { + eventConsumer.accept(event); + return true; + }); + } + /** * Remove an event consumer * * @param eventConsumer the consumer */ - public void removeEventConsumer(@NotNull Consumer eventConsumer) { + public void removeEventConsumer(@NotNull Predicate eventConsumer) { eventConsumerList.remove(eventConsumer); } @@ -61,6 +86,10 @@ public void stop() { @Override public void handleEvent(@NotNull Object event) { - eventConsumerList.forEach(consumer -> consumer.accept(event)); + for (Predicate eventConsumer : eventConsumerList) { + if (!eventConsumer.test(event)) { + break; + } + } } } diff --git a/ui/src/main/java/me/hsgamer/hscore/ui/UI.java b/ui/src/main/java/me/hsgamer/hscore/ui/UI.java index c2eca2c99c..66f3f6fba5 100644 --- a/ui/src/main/java/me/hsgamer/hscore/ui/UI.java +++ b/ui/src/main/java/me/hsgamer/hscore/ui/UI.java @@ -1,12 +1,11 @@ package me.hsgamer.hscore.ui; import me.hsgamer.hscore.ui.property.Initializable; -import me.hsgamer.hscore.ui.property.Updatable; /** * The User Interface */ -public interface UI extends Initializable, Updatable { +public interface UI extends Initializable { /** * Handle the event *