From 85f2ac6d511ca3075b56d6620a326d5132df70a3 Mon Sep 17 00:00:00 2001 From: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com> Date: Thu, 6 Apr 2023 18:53:17 +0200 Subject: [PATCH] Gui freezing --- .../xyz/xenondevs/invui/gui/AbstractGui.java | 44 ++++++++++++++----- .../java/xyz/xenondevs/invui/gui/Gui.java | 24 ++++++++++ 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractGui.java b/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractGui.java index 5bf612a..3aca109 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractGui.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/AbstractGui.java @@ -39,10 +39,10 @@ public abstract class AbstractGui implements Gui, GuiParent { private final SlotElement[] slotElements; private final Set parents = new HashSet<>(); - private SlotElement[] animationElements; - private Animation animation; - + private boolean frozen; private ItemProvider background; + private Animation animation; + private SlotElement[] animationElements; public AbstractGui(int width, int height) { this.width = width; @@ -52,8 +52,8 @@ public abstract class AbstractGui implements Gui, GuiParent { } public void handleClick(int slotNumber, Player player, ClickType clickType, InventoryClickEvent event) { - if (animation != null) { - // cancel all clicks if an animation is running + // cancel all clicks if the gui is frozen or an animation is running + if (frozen || animation != null) { event.setCancelled(true); return; } @@ -287,6 +287,10 @@ public abstract class AbstractGui implements Gui, GuiParent { } public boolean handleItemDrag(UpdateReason updateReason, int slot, ItemStack oldStack, ItemStack newStack) { + // cancel all clicks if the gui is frozen or an animation is running + if (frozen || animation != null) + return false; + SlotElement element = getSlotElement(slot); if (element != null) element = element.getHoldingElement(); if (element instanceof SlotElement.VISlotElement) { @@ -304,7 +308,9 @@ public abstract class AbstractGui implements Gui, GuiParent { public void handleItemShift(InventoryClickEvent event) { event.setCancelled(true); - if (animation != null) return; // cancel all clicks if an animation is running + // cancel all clicks if the gui is frozen or an animation is running + if (frozen || animation != null) + return; Player player = (Player) event.getWhoClicked(); ItemStack clicked = event.getCurrentItem(); @@ -570,6 +576,16 @@ public abstract class AbstractGui implements Gui, GuiParent { return size; } + @Override + public void setFrozen(boolean frozen) { + this.frozen = frozen; + } + + @Override + public boolean isFrozen() { + return frozen; + } + // region coordinate-based methods @Override public void setSlotElement(int x, int y, SlotElement slotElement) { @@ -693,6 +709,7 @@ public abstract class AbstractGui implements Gui, GuiParent { protected Structure structure; protected ItemProvider background; protected List> modifiers; + protected boolean frozen; @Override public @NotNull S setStructure(int width, int height, @NotNull String structureData) { @@ -778,6 +795,12 @@ public abstract class AbstractGui implements Gui, GuiParent { return (S) this; } + @Override + public @NotNull S setFrozen(boolean frozen) { + this.frozen = frozen; + return (S) this; + } + @Override public @NotNull S addModifier(@NotNull Consumer<@NotNull Gui> modifier) { if (modifiers == null) @@ -794,12 +817,9 @@ public abstract class AbstractGui implements Gui, GuiParent { } protected void applyModifiers(@NotNull G gui) { - if (background != null) { - gui.setBackground(background); - } - if (modifiers != null) { - modifiers.forEach(modifier -> modifier.accept(gui)); - } + gui.setFrozen(frozen); + if (background != null) gui.setBackground(background); + if (modifiers != null) modifiers.forEach(modifier -> modifier.accept(gui)); } @SuppressWarnings("unchecked") 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 01ebb39..b325679 100644 --- a/invui/src/main/java/xyz/xenondevs/invui/gui/Gui.java +++ b/invui/src/main/java/xyz/xenondevs/invui/gui/Gui.java @@ -284,6 +284,21 @@ public interface Gui { */ void cancelAnimation(); + /** + * Freezes or unfreezes the {@link Gui}. + * A frozen {@link Gui} will not allow any clicks. + * + * @param frozen If the {@link Gui} should be frozen or not. + */ + void setFrozen(boolean frozen); + + /** + * Gets if the {@link Gui} is frozen. + * + * @return If the {@link Gui} is frozen. + */ + boolean isFrozen(); + // /** @@ -524,6 +539,15 @@ public interface Gui { @Contract("_ -> this") @NotNull S setBackground(@NotNull ItemStack itemStack); + /** + * Sets whether the {@link Gui} should be frozen. + * + * @param frozen Whether the {@link Gui} should be frozen + * @return This {@link Builder Gui Builder} + */ + @Contract("_ -> this") + @NotNull S setFrozen(boolean frozen); + /** * Sets the background of the {@link Gui}. *