From 911b55f2e0a5e5fdb4571ff92b8ee938b8f134bd Mon Sep 17 00:00:00 2001 From: Lorenzo <69311874+Lorenzo0111@users.noreply.github.com> Date: Sun, 26 Dec 2021 17:41:54 +0100 Subject: [PATCH 1/6] Added animations --- .../triumphteam/gui/animations/Animation.java | 45 +++++++++++ .../gui/animations/AnimationRunner.java | 26 +++++++ .../dev/triumphteam/gui/animations/Frame.java | 27 +++++++ .../gui/animations/impl/BaseAnimation.java | 77 +++++++++++++++++++ .../gui/animations/impl/CustomAnimation.java | 18 +++++ .../animations/impl/InfiniteAnimation.java | 18 +++++ 6 files changed, 211 insertions(+) create mode 100644 core/src/main/java/dev/triumphteam/gui/animations/Animation.java create mode 100644 core/src/main/java/dev/triumphteam/gui/animations/AnimationRunner.java create mode 100644 core/src/main/java/dev/triumphteam/gui/animations/Frame.java create mode 100644 core/src/main/java/dev/triumphteam/gui/animations/impl/BaseAnimation.java create mode 100644 core/src/main/java/dev/triumphteam/gui/animations/impl/CustomAnimation.java create mode 100644 core/src/main/java/dev/triumphteam/gui/animations/impl/InfiniteAnimation.java diff --git a/core/src/main/java/dev/triumphteam/gui/animations/Animation.java b/core/src/main/java/dev/triumphteam/gui/animations/Animation.java new file mode 100644 index 00000000..5a027e2f --- /dev/null +++ b/core/src/main/java/dev/triumphteam/gui/animations/Animation.java @@ -0,0 +1,45 @@ +package dev.triumphteam.gui.animations; + +import dev.triumphteam.gui.animations.impl.CustomAnimation; +import dev.triumphteam.gui.animations.impl.InfiniteAnimation; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; + +public interface Animation { + + /** + * Reset to the initial frame + */ + void reset(); + + /** + * Go to the next frame + * @return the next frame or null if it is the last frame + */ + @Nullable Frame nextFrame(); + + /** + * Start the animation + */ + void start(); + + /** + * @return the delay between frames + */ + int getDelay(); + + /** + * @return the bukkit runnable that handles frame updates + */ + AnimationRunner getRunnable(); + + static @NotNull Animation of(int delay, Frame... frames) { + return new CustomAnimation(delay, Arrays.asList(frames)); + } + + static @NotNull Animation infinite(int delay, Frame... frames) { + return new InfiniteAnimation(delay, Arrays.asList(frames)); + } +} diff --git a/core/src/main/java/dev/triumphteam/gui/animations/AnimationRunner.java b/core/src/main/java/dev/triumphteam/gui/animations/AnimationRunner.java new file mode 100644 index 00000000..bc0209f3 --- /dev/null +++ b/core/src/main/java/dev/triumphteam/gui/animations/AnimationRunner.java @@ -0,0 +1,26 @@ +package dev.triumphteam.gui.animations; + +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; +import org.jetbrains.annotations.NotNull; + +public class AnimationRunner extends BukkitRunnable { + private static final Plugin plugin = JavaPlugin.getProvidingPlugin(AnimationRunner.class); + private final Animation animation; + + public AnimationRunner(@NotNull Animation animation) { + this.animation = animation; + + this.runTaskTimerAsynchronously(plugin,0,animation.getDelay()); + } + + @Override + public void run() { + Frame frame = animation.nextFrame(); + if (frame == null) { + this.cancel(); + } + } + +} diff --git a/core/src/main/java/dev/triumphteam/gui/animations/Frame.java b/core/src/main/java/dev/triumphteam/gui/animations/Frame.java new file mode 100644 index 00000000..c9071479 --- /dev/null +++ b/core/src/main/java/dev/triumphteam/gui/animations/Frame.java @@ -0,0 +1,27 @@ +package dev.triumphteam.gui.animations; + +import dev.triumphteam.gui.guis.BaseGui; +import dev.triumphteam.gui.guis.GuiItem; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public class Frame { + private final Map items; + + /** + * @param items A map with the slot and the item to display in this frame + */ + public Frame(Map items) { + this.items = items; + } + + /** + * @param gui The gui to display this frame + * @return The same frame instance + */ + public Frame apply(@NotNull BaseGui gui) { + items.forEach(gui::updateItem); + return this; + } +} diff --git a/core/src/main/java/dev/triumphteam/gui/animations/impl/BaseAnimation.java b/core/src/main/java/dev/triumphteam/gui/animations/impl/BaseAnimation.java new file mode 100644 index 00000000..6743f82f --- /dev/null +++ b/core/src/main/java/dev/triumphteam/gui/animations/impl/BaseAnimation.java @@ -0,0 +1,77 @@ +package dev.triumphteam.gui.animations.impl; + +import dev.triumphteam.gui.animations.Animation; +import dev.triumphteam.gui.animations.AnimationRunner; +import dev.triumphteam.gui.animations.Frame; +import dev.triumphteam.gui.guis.BaseGui; +import org.apache.commons.lang3.Validate; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public abstract class BaseAnimation implements Animation { + protected int current; + protected BaseGui gui; + protected AnimationRunner runner; + protected final int delay; + protected final List frames; + + public BaseAnimation(int delay, List frames) { + this.delay = delay; + this.frames = frames; + + this.reset(); + } + + public void to(BaseGui gui) { + Validate.notNull(gui, "Gui cannot be null"); + this.gui = gui; + } + + @Override + public void reset() { + current = 0; + } + + @Override + public @Nullable Frame nextFrame() { + Validate.notNull(gui, "Gui has not been linked to this animation. Please link it by calling the to(BaseGui) method"); + current++; + + if (frames.size() == current) { + return this.onFinish(); + } + + return frames.get(current).apply(gui); + } + + @Override + public void start() { + Validate.notNull(gui, "Gui has not been linked to this animation. Please link it by calling the to(BaseGui) method"); + + this.reset(); + frames.get(current).apply(gui); + + if (runner != null) { + runner.cancel(); + runner = null; + } + + this.runner = new AnimationRunner(this); + } + + @Override + public int getDelay() { + return delay; + } + + @Override + public AnimationRunner getRunnable() { + return runner; + } + + /** + * @return The frame to return when the animation is finished + */ + public abstract Frame onFinish(); +} diff --git a/core/src/main/java/dev/triumphteam/gui/animations/impl/CustomAnimation.java b/core/src/main/java/dev/triumphteam/gui/animations/impl/CustomAnimation.java new file mode 100644 index 00000000..24c56589 --- /dev/null +++ b/core/src/main/java/dev/triumphteam/gui/animations/impl/CustomAnimation.java @@ -0,0 +1,18 @@ +package dev.triumphteam.gui.animations.impl; + +import dev.triumphteam.gui.animations.Frame; + +import java.util.List; + +public class CustomAnimation extends BaseAnimation { + + public CustomAnimation(int delay, List frames) { + super(delay, frames); + } + + @Override + public Frame onFinish() { + return null; + } + +} diff --git a/core/src/main/java/dev/triumphteam/gui/animations/impl/InfiniteAnimation.java b/core/src/main/java/dev/triumphteam/gui/animations/impl/InfiniteAnimation.java new file mode 100644 index 00000000..3a42706a --- /dev/null +++ b/core/src/main/java/dev/triumphteam/gui/animations/impl/InfiniteAnimation.java @@ -0,0 +1,18 @@ +package dev.triumphteam.gui.animations.impl; + +import dev.triumphteam.gui.animations.Frame; + +import java.util.List; + +public class InfiniteAnimation extends BaseAnimation { + + public InfiniteAnimation(int delay, List frames) { + super(delay, frames); + } + + @Override + public Frame onFinish() { + this.reset(); + return this.frames.get(current); + } +} From aeb39c70a5def5226f625e03c856460d390ea2b7 Mon Sep 17 00:00:00 2001 From: Lorenzo <69311874+Lorenzo0111@users.noreply.github.com> Date: Sun, 26 Dec 2021 19:07:37 +0100 Subject: [PATCH 2/6] Some fixes and added an example plugin module. --- .../triumphteam/gui/animations/Animation.java | 54 ++++++++++++- .../gui/animations/AnimationRunner.java | 26 +++++- .../dev/triumphteam/gui/animations/Frame.java | 51 ++++++++++++ .../gui/animations/impl/BaseAnimation.java | 53 +++++++++--- .../gui/animations/impl/CustomAnimation.java | 18 ----- .../animations/impl/InfiniteAnimation.java | 27 ++++++- .../gui/animations/impl/SimpleAnimation.java | 44 ++++++++++ .../dev/triumphteam/gui/guis/BaseGui.java | 20 +++++ .../dev/triumphteam/gui/guis/GuiListener.java | 3 + example-plugin/build.gradle.kts | 22 +++++ .../gui/example/ExamplePlugin.java | 40 +++++++++ .../gui/example/command/ExampleCommand.java | 81 +++++++++++++++++++ example-plugin/src/main/resources/plugin.yml | 8 ++ settings.gradle.kts | 1 + 14 files changed, 416 insertions(+), 32 deletions(-) delete mode 100644 core/src/main/java/dev/triumphteam/gui/animations/impl/CustomAnimation.java create mode 100644 core/src/main/java/dev/triumphteam/gui/animations/impl/SimpleAnimation.java create mode 100644 example-plugin/build.gradle.kts create mode 100644 example-plugin/src/main/java/dev/triumphteam/gui/example/ExamplePlugin.java create mode 100644 example-plugin/src/main/java/dev/triumphteam/gui/example/command/ExampleCommand.java create mode 100644 example-plugin/src/main/resources/plugin.yml diff --git a/core/src/main/java/dev/triumphteam/gui/animations/Animation.java b/core/src/main/java/dev/triumphteam/gui/animations/Animation.java index 5a027e2f..30841eb6 100644 --- a/core/src/main/java/dev/triumphteam/gui/animations/Animation.java +++ b/core/src/main/java/dev/triumphteam/gui/animations/Animation.java @@ -1,7 +1,32 @@ +/* + * MIT License + * + * Copyright (c) 2021 TriumphTeam + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package dev.triumphteam.gui.animations; -import dev.triumphteam.gui.animations.impl.CustomAnimation; +import dev.triumphteam.gui.animations.impl.SimpleAnimation; import dev.triumphteam.gui.animations.impl.InfiniteAnimation; +import dev.triumphteam.gui.guis.BaseGui; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -22,8 +47,14 @@ public interface Animation { /** * Start the animation + * @param gui The gui to apply the animation to + */ + void start(BaseGui gui); + + /** + * Stops the animation */ - void start(); + void stop(); /** * @return the delay between frames @@ -35,10 +66,27 @@ public interface Animation { */ AnimationRunner getRunnable(); + /** + * @return the gui that this animation is applyed to + */ + BaseGui getGui(); + + /** + * Create a simple animation that stop at the end of the frames + * @param delay the delay between frames + * @param frames the frames + * @return the animation + */ static @NotNull Animation of(int delay, Frame... frames) { - return new CustomAnimation(delay, Arrays.asList(frames)); + return new SimpleAnimation(delay, Arrays.asList(frames)); } + /** + * Create a loop animation + * @param delay the delay between frames + * @param frames the frames + * @return the animation + */ static @NotNull Animation infinite(int delay, Frame... frames) { return new InfiniteAnimation(delay, Arrays.asList(frames)); } diff --git a/core/src/main/java/dev/triumphteam/gui/animations/AnimationRunner.java b/core/src/main/java/dev/triumphteam/gui/animations/AnimationRunner.java index bc0209f3..760fc945 100644 --- a/core/src/main/java/dev/triumphteam/gui/animations/AnimationRunner.java +++ b/core/src/main/java/dev/triumphteam/gui/animations/AnimationRunner.java @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2021 TriumphTeam + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package dev.triumphteam.gui.animations; import org.bukkit.plugin.Plugin; @@ -12,7 +36,7 @@ public class AnimationRunner extends BukkitRunnable { public AnimationRunner(@NotNull Animation animation) { this.animation = animation; - this.runTaskTimerAsynchronously(plugin,0,animation.getDelay()); + this.runTaskTimerAsynchronously(plugin,animation.getDelay(),animation.getDelay()); } @Override diff --git a/core/src/main/java/dev/triumphteam/gui/animations/Frame.java b/core/src/main/java/dev/triumphteam/gui/animations/Frame.java index c9071479..e2d5ad2e 100644 --- a/core/src/main/java/dev/triumphteam/gui/animations/Frame.java +++ b/core/src/main/java/dev/triumphteam/gui/animations/Frame.java @@ -1,12 +1,39 @@ +/* + * MIT License + * + * Copyright (c) 2021 TriumphTeam + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package dev.triumphteam.gui.animations; import dev.triumphteam.gui.guis.BaseGui; import dev.triumphteam.gui.guis.GuiItem; import org.jetbrains.annotations.NotNull; +import java.util.HashMap; import java.util.Map; public class Frame { + private boolean saved = false; + private final Map defaultItems; private final Map items; /** @@ -14,6 +41,7 @@ public class Frame { */ public Frame(Map items) { this.items = items; + this.defaultItems = new HashMap<>(); } /** @@ -21,7 +49,30 @@ public Frame(Map items) { * @return The same frame instance */ public Frame apply(@NotNull BaseGui gui) { + if (!saved) { + for (Map.Entry entry : items.entrySet()) { + defaultItems.put(entry.getKey(), gui.getGuiItem(entry.getKey())); + } + + saved = true; + } + items.forEach(gui::updateItem); return this; } + + public void fallback(@NotNull BaseGui gui) { + defaultItems.forEach(gui::updateItem); + } + + public Map getItems() { + return items; + } + + @Override + public String toString() { + return "Frame{" + + "items=" + items + + '}'; + } } diff --git a/core/src/main/java/dev/triumphteam/gui/animations/impl/BaseAnimation.java b/core/src/main/java/dev/triumphteam/gui/animations/impl/BaseAnimation.java index 6743f82f..13b3604d 100644 --- a/core/src/main/java/dev/triumphteam/gui/animations/impl/BaseAnimation.java +++ b/core/src/main/java/dev/triumphteam/gui/animations/impl/BaseAnimation.java @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2021 TriumphTeam + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package dev.triumphteam.gui.animations.impl; import dev.triumphteam.gui.animations.Animation; @@ -14,7 +38,7 @@ public abstract class BaseAnimation implements Animation { protected BaseGui gui; protected AnimationRunner runner; protected final int delay; - protected final List frames; + protected List frames; public BaseAnimation(int delay, List frames) { this.delay = delay; @@ -23,11 +47,6 @@ public BaseAnimation(int delay, List frames) { this.reset(); } - public void to(BaseGui gui) { - Validate.notNull(gui, "Gui cannot be null"); - this.gui = gui; - } - @Override public void reset() { current = 0; @@ -35,7 +54,7 @@ public void reset() { @Override public @Nullable Frame nextFrame() { - Validate.notNull(gui, "Gui has not been linked to this animation. Please link it by calling the to(BaseGui) method"); + Validate.notNull(gui, "Gui has not been linked to this animation."); current++; if (frames.size() == current) { @@ -46,8 +65,9 @@ public void reset() { } @Override - public void start() { - Validate.notNull(gui, "Gui has not been linked to this animation. Please link it by calling the to(BaseGui) method"); + public void start(BaseGui gui) { + Validate.notNull(gui, "Gui cannot be null."); + this.gui = gui; this.reset(); frames.get(current).apply(gui); @@ -65,11 +85,26 @@ public int getDelay() { return delay; } + @Override + public BaseGui getGui() { + return gui; + } + @Override public AnimationRunner getRunnable() { return runner; } + @Override + public void stop() { + Validate.notNull(gui, "Gui has not been linked to this animation. Please link it by calling the to(BaseGui) method"); + + if (this.runner != null && !this.runner.isCancelled()) { + this.runner.cancel(); + } + frames.get(0).fallback(gui); + } + /** * @return The frame to return when the animation is finished */ diff --git a/core/src/main/java/dev/triumphteam/gui/animations/impl/CustomAnimation.java b/core/src/main/java/dev/triumphteam/gui/animations/impl/CustomAnimation.java deleted file mode 100644 index 24c56589..00000000 --- a/core/src/main/java/dev/triumphteam/gui/animations/impl/CustomAnimation.java +++ /dev/null @@ -1,18 +0,0 @@ -package dev.triumphteam.gui.animations.impl; - -import dev.triumphteam.gui.animations.Frame; - -import java.util.List; - -public class CustomAnimation extends BaseAnimation { - - public CustomAnimation(int delay, List frames) { - super(delay, frames); - } - - @Override - public Frame onFinish() { - return null; - } - -} diff --git a/core/src/main/java/dev/triumphteam/gui/animations/impl/InfiniteAnimation.java b/core/src/main/java/dev/triumphteam/gui/animations/impl/InfiniteAnimation.java index 3a42706a..4cb3ee21 100644 --- a/core/src/main/java/dev/triumphteam/gui/animations/impl/InfiniteAnimation.java +++ b/core/src/main/java/dev/triumphteam/gui/animations/impl/InfiniteAnimation.java @@ -1,3 +1,27 @@ +/* + * MIT License + * + * Copyright (c) 2021 TriumphTeam + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package dev.triumphteam.gui.animations.impl; import dev.triumphteam.gui.animations.Frame; @@ -13,6 +37,7 @@ public InfiniteAnimation(int delay, List frames) { @Override public Frame onFinish() { this.reset(); - return this.frames.get(current); + + return this.frames.get(current).apply(gui); } } diff --git a/core/src/main/java/dev/triumphteam/gui/animations/impl/SimpleAnimation.java b/core/src/main/java/dev/triumphteam/gui/animations/impl/SimpleAnimation.java new file mode 100644 index 00000000..166e587c --- /dev/null +++ b/core/src/main/java/dev/triumphteam/gui/animations/impl/SimpleAnimation.java @@ -0,0 +1,44 @@ +/* + * MIT License + * + * Copyright (c) 2021 TriumphTeam + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package dev.triumphteam.gui.animations.impl; + +import dev.triumphteam.gui.animations.Frame; + +import java.util.List; + +public class SimpleAnimation extends BaseAnimation { + + public SimpleAnimation(int delay, List frames) { + super(delay, frames); + } + + @Override + public Frame onFinish() { + this.stop(); + + return null; + } + +} diff --git a/core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java b/core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java index 703a6bc8..e5ab5e63 100644 --- a/core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java +++ b/core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java @@ -23,6 +23,7 @@ */ package dev.triumphteam.gui.guis; +import dev.triumphteam.gui.animations.Animation; import dev.triumphteam.gui.components.GuiAction; import dev.triumphteam.gui.components.GuiType; import dev.triumphteam.gui.components.InteractionModifier; @@ -87,6 +88,9 @@ public abstract class BaseGui implements InventoryHolder { // Contains all items the GUI will have. private final Map guiItems; + private final List animations = new ArrayList<>(); + private boolean animationsStarted = false; + // Actions for specific slots. private final Map> slotActions; // Interaction modifiers. @@ -302,6 +306,10 @@ public void addItem(final boolean expandIfFull, @NotNull final GuiItem... items) this.addItem(true, notAddedItems.toArray(new GuiItem[0])); } + public void addAnimation(Animation animation) { + this.animations.add(animation); + } + /** * Sets the {@link GuiAction} of a default click on any item. * See {@link InventoryClickEvent}. @@ -432,6 +440,11 @@ public void open(@NotNull final HumanEntity player) { inventory.clear(); populateGui(); player.openInventory(inventory); + + if (!animationsStarted) { + animations.forEach((animation) -> animation.start(this)); + animationsStarted = true; + } } /** @@ -452,6 +465,8 @@ public void close(@NotNull final HumanEntity player) { public void close(@NotNull final HumanEntity player, final boolean runCloseAction) { Bukkit.getScheduler().runTaskLater(plugin, () -> { this.runCloseAction = runCloseAction; + this.animations.forEach(Animation::stop); + this.animationsStarted = false; player.closeInventory(); this.runCloseAction = true; }, 2L); @@ -873,6 +888,11 @@ GuiAction getSlotAction(final int slot) { return slotActions.get(slot); } + @NotNull + List getAnimations() { + return animations; + } + /** * Populates the GUI with it's items. */ diff --git a/core/src/main/java/dev/triumphteam/gui/guis/GuiListener.java b/core/src/main/java/dev/triumphteam/gui/guis/GuiListener.java index f7e1df8f..ffbe7c5f 100644 --- a/core/src/main/java/dev/triumphteam/gui/guis/GuiListener.java +++ b/core/src/main/java/dev/triumphteam/gui/guis/GuiListener.java @@ -23,6 +23,7 @@ */ package dev.triumphteam.gui.guis; +import dev.triumphteam.gui.animations.Animation; import dev.triumphteam.gui.components.GuiAction; import dev.triumphteam.gui.components.util.ItemNbt; import org.bukkit.event.EventHandler; @@ -136,6 +137,8 @@ public void onGuiClose(final InventoryCloseEvent event) { ((PersistentPaginatedGui) gui).savePage(); } + gui.getAnimations().forEach(Animation::stop); + // The GUI action for closing final GuiAction closeAction = gui.getCloseGuiAction(); diff --git a/example-plugin/build.gradle.kts b/example-plugin/build.gradle.kts new file mode 100644 index 00000000..28e9cca4 --- /dev/null +++ b/example-plugin/build.gradle.kts @@ -0,0 +1,22 @@ +plugins { + id("com.github.johnrengelman.shadow") version "7.1.1" + id("xyz.jpenilla.run-paper") version "1.0.6" +} + +dependencies { + implementation(project(":triumph-gui")) +} + +tasks { + shadowJar { + archiveClassifier.set("") + } + + build { + dependsOn("shadowJar") + } + + runServer { + minecraftVersion("1.18.1") + } +} \ No newline at end of file diff --git a/example-plugin/src/main/java/dev/triumphteam/gui/example/ExamplePlugin.java b/example-plugin/src/main/java/dev/triumphteam/gui/example/ExamplePlugin.java new file mode 100644 index 00000000..063c1e37 --- /dev/null +++ b/example-plugin/src/main/java/dev/triumphteam/gui/example/ExamplePlugin.java @@ -0,0 +1,40 @@ +/* + * MIT License + * + * Copyright (c) 2021 TriumphTeam + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package dev.triumphteam.gui.example; + +import dev.triumphteam.gui.example.command.ExampleCommand; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.Objects; + +public class ExamplePlugin extends JavaPlugin { + + @Override + public void onEnable() { + Objects.requireNonNull(this.getCommand("opengui")).setExecutor(new ExampleCommand()); + } + + +} diff --git a/example-plugin/src/main/java/dev/triumphteam/gui/example/command/ExampleCommand.java b/example-plugin/src/main/java/dev/triumphteam/gui/example/command/ExampleCommand.java new file mode 100644 index 00000000..1d4b1417 --- /dev/null +++ b/example-plugin/src/main/java/dev/triumphteam/gui/example/command/ExampleCommand.java @@ -0,0 +1,81 @@ +/* + * MIT License + * + * Copyright (c) 2021 TriumphTeam + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package dev.triumphteam.gui.example.command; + +import dev.triumphteam.gui.animations.Animation; +import dev.triumphteam.gui.animations.Frame; +import dev.triumphteam.gui.builder.item.ItemBuilder; +import dev.triumphteam.gui.components.InteractionModifier; +import dev.triumphteam.gui.guis.Gui; +import dev.triumphteam.gui.guis.GuiItem; +import net.kyori.adventure.text.Component; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.EnumSet; +import java.util.HashMap; +import java.util.Map; + +public class ExampleCommand implements CommandExecutor { + private final Frame first; + private final Frame second; + + public ExampleCommand() { + Map firstItems = new HashMap<>(); + Map secondItems = new HashMap<>(); + + firstItems.put(1, ItemBuilder.from(Material.DIAMOND) + .name(Component.text(ChatColor.GOLD + "" + ChatColor.BOLD + "Hello World")) + .asGuiItem()); + secondItems.put(1, ItemBuilder.from(Material.DIAMOND) + .name(Component.text(ChatColor.WHITE + "" + ChatColor.BOLD + "Hello World")) + .asGuiItem()); + + first = new Frame(firstItems); + second = new Frame(secondItems); + } + + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (!(sender instanceof Player)) return false; + + Player player = (Player) sender; + Gui gui = new Gui(3, ChatColor.GOLD + "" + ChatColor.BOLD + "Example Gui", EnumSet.noneOf(InteractionModifier.class)); + gui.setItem(1, ItemBuilder.from(Material.DIAMOND).asGuiItem()); + + Animation animation = Animation.infinite(5,first,second); + gui.addAnimation(animation); + + gui.open(player); + + return true; + } + +} diff --git a/example-plugin/src/main/resources/plugin.yml b/example-plugin/src/main/resources/plugin.yml new file mode 100644 index 00000000..9afe3e83 --- /dev/null +++ b/example-plugin/src/main/resources/plugin.yml @@ -0,0 +1,8 @@ +name: ExampleGuiPlugin +description: Example GUI plugin +version: 1.0 +api-version: 1.13 +main: dev.triumphteam.gui.example.ExamplePlugin +commands: + opengui: + description: Open the GUI \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index f4b738d8..100fd30a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,5 +1,6 @@ rootProject.name = "triumph-gui" +include("example-plugin") include("core") findProject(":core")?.name = "triumph-gui" From a2e4bfdeaabe356b8e7ad107530145830a4d881f Mon Sep 17 00:00:00 2001 From: Lorenzo <69311874+Lorenzo0111@users.noreply.github.com> Date: Mon, 27 Dec 2021 10:39:08 +0100 Subject: [PATCH 3/6] Removed the example plugin module --- example-plugin/build.gradle.kts | 22 ----- .../gui/example/ExamplePlugin.java | 40 --------- .../gui/example/command/ExampleCommand.java | 81 ------------------- example-plugin/src/main/resources/plugin.yml | 8 -- settings.gradle.kts | 1 - 5 files changed, 152 deletions(-) delete mode 100644 example-plugin/build.gradle.kts delete mode 100644 example-plugin/src/main/java/dev/triumphteam/gui/example/ExamplePlugin.java delete mode 100644 example-plugin/src/main/java/dev/triumphteam/gui/example/command/ExampleCommand.java delete mode 100644 example-plugin/src/main/resources/plugin.yml diff --git a/example-plugin/build.gradle.kts b/example-plugin/build.gradle.kts deleted file mode 100644 index 28e9cca4..00000000 --- a/example-plugin/build.gradle.kts +++ /dev/null @@ -1,22 +0,0 @@ -plugins { - id("com.github.johnrengelman.shadow") version "7.1.1" - id("xyz.jpenilla.run-paper") version "1.0.6" -} - -dependencies { - implementation(project(":triumph-gui")) -} - -tasks { - shadowJar { - archiveClassifier.set("") - } - - build { - dependsOn("shadowJar") - } - - runServer { - minecraftVersion("1.18.1") - } -} \ No newline at end of file diff --git a/example-plugin/src/main/java/dev/triumphteam/gui/example/ExamplePlugin.java b/example-plugin/src/main/java/dev/triumphteam/gui/example/ExamplePlugin.java deleted file mode 100644 index 063c1e37..00000000 --- a/example-plugin/src/main/java/dev/triumphteam/gui/example/ExamplePlugin.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2021 TriumphTeam - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package dev.triumphteam.gui.example; - -import dev.triumphteam.gui.example.command.ExampleCommand; -import org.bukkit.plugin.java.JavaPlugin; - -import java.util.Objects; - -public class ExamplePlugin extends JavaPlugin { - - @Override - public void onEnable() { - Objects.requireNonNull(this.getCommand("opengui")).setExecutor(new ExampleCommand()); - } - - -} diff --git a/example-plugin/src/main/java/dev/triumphteam/gui/example/command/ExampleCommand.java b/example-plugin/src/main/java/dev/triumphteam/gui/example/command/ExampleCommand.java deleted file mode 100644 index 1d4b1417..00000000 --- a/example-plugin/src/main/java/dev/triumphteam/gui/example/command/ExampleCommand.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2021 TriumphTeam - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package dev.triumphteam.gui.example.command; - -import dev.triumphteam.gui.animations.Animation; -import dev.triumphteam.gui.animations.Frame; -import dev.triumphteam.gui.builder.item.ItemBuilder; -import dev.triumphteam.gui.components.InteractionModifier; -import dev.triumphteam.gui.guis.Gui; -import dev.triumphteam.gui.guis.GuiItem; -import net.kyori.adventure.text.Component; -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; - -import java.util.EnumSet; -import java.util.HashMap; -import java.util.Map; - -public class ExampleCommand implements CommandExecutor { - private final Frame first; - private final Frame second; - - public ExampleCommand() { - Map firstItems = new HashMap<>(); - Map secondItems = new HashMap<>(); - - firstItems.put(1, ItemBuilder.from(Material.DIAMOND) - .name(Component.text(ChatColor.GOLD + "" + ChatColor.BOLD + "Hello World")) - .asGuiItem()); - secondItems.put(1, ItemBuilder.from(Material.DIAMOND) - .name(Component.text(ChatColor.WHITE + "" + ChatColor.BOLD + "Hello World")) - .asGuiItem()); - - first = new Frame(firstItems); - second = new Frame(secondItems); - } - - @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - if (!(sender instanceof Player)) return false; - - Player player = (Player) sender; - Gui gui = new Gui(3, ChatColor.GOLD + "" + ChatColor.BOLD + "Example Gui", EnumSet.noneOf(InteractionModifier.class)); - gui.setItem(1, ItemBuilder.from(Material.DIAMOND).asGuiItem()); - - Animation animation = Animation.infinite(5,first,second); - gui.addAnimation(animation); - - gui.open(player); - - return true; - } - -} diff --git a/example-plugin/src/main/resources/plugin.yml b/example-plugin/src/main/resources/plugin.yml deleted file mode 100644 index 9afe3e83..00000000 --- a/example-plugin/src/main/resources/plugin.yml +++ /dev/null @@ -1,8 +0,0 @@ -name: ExampleGuiPlugin -description: Example GUI plugin -version: 1.0 -api-version: 1.13 -main: dev.triumphteam.gui.example.ExamplePlugin -commands: - opengui: - description: Open the GUI \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 100fd30a..f4b738d8 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,6 +1,5 @@ rootProject.name = "triumph-gui" -include("example-plugin") include("core") findProject(":core")?.name = "triumph-gui" From 1ddda73ffdd04862a833ba89564fcad512bbaf2c Mon Sep 17 00:00:00 2001 From: Lorenzo <69311874+Lorenzo0111@users.noreply.github.com> Date: Mon, 27 Dec 2021 10:47:05 +0100 Subject: [PATCH 4/6] Added frame builder --- .../dev/triumphteam/gui/animations/Frame.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/core/src/main/java/dev/triumphteam/gui/animations/Frame.java b/core/src/main/java/dev/triumphteam/gui/animations/Frame.java index e2d5ad2e..fb68f6ff 100644 --- a/core/src/main/java/dev/triumphteam/gui/animations/Frame.java +++ b/core/src/main/java/dev/triumphteam/gui/animations/Frame.java @@ -26,6 +26,7 @@ import dev.triumphteam.gui.guis.BaseGui; import dev.triumphteam.gui.guis.GuiItem; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import java.util.HashMap; @@ -75,4 +76,23 @@ public String toString() { "items=" + items + '}'; } + + @Contract(value = " -> new", pure = true) + public static @NotNull Builder builder() { + return new Builder(); + } + + public static class Builder { + private final Map items = new HashMap<>(); + + public Builder addItem(int slot, GuiItem item) { + items.put(slot, item); + return this; + } + + @Contract(value = " -> new", pure = true) + public @NotNull Frame build() { + return new Frame(items); + } + } } From 756b013eb24d6df4b1333df7b98397be8bc68095 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Tue, 27 Jun 2023 13:24:01 +0200 Subject: [PATCH 5/6] Fixed merge --- .../src/main/java/dev/triumphteam/gui/guis/GuiListener.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/core/src/main/java/dev/triumphteam/gui/guis/GuiListener.java b/core/src/main/java/dev/triumphteam/gui/guis/GuiListener.java index ffbe7c5f..8ec988ea 100644 --- a/core/src/main/java/dev/triumphteam/gui/guis/GuiListener.java +++ b/core/src/main/java/dev/triumphteam/gui/guis/GuiListener.java @@ -131,12 +131,6 @@ public void onGuiClose(final InventoryCloseEvent event) { // GUI final BaseGui gui = (BaseGui) event.getInventory().getHolder(); - - // If it's a persistent paginated gui saves the current page modifications - if (gui instanceof PersistentPaginatedGui) { - ((PersistentPaginatedGui) gui).savePage(); - } - gui.getAnimations().forEach(Animation::stop); // The GUI action for closing From 7b693a55d5877f85a49b806fdbaecd27548c712c Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 6 Dec 2025 10:23:16 +0100 Subject: [PATCH 6/6] Fixed collections import --- core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java b/core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java index e5e4f6ff..1bb8fdeb 100644 --- a/core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java +++ b/core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java @@ -51,12 +51,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.EnumSet; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; /**