From 7c917c396fcde3edaa59fdf22b7f4fcd15d00da5 Mon Sep 17 00:00:00 2001 From: ProfElements <43350117+ProfElements@users.noreply.github.com> Date: Thu, 18 Jan 2024 08:07:18 -0600 Subject: [PATCH] :sparkles: feat(api): Introduce SlimefunItemRegistryFinalizedEvent (#4099) Co-authored-by: JustAHuman-xD <65748158+JustAHuman-xD@users.noreply.github.com> Co-authored-by: Daniel Walsh --- .../SlimefunItemRegistryFinalizedEvent.java | 35 ++++++++++++++++ .../implementation/setup/PostSetup.java | 3 ++ .../TestSlimefunRegistryFinalizedEvent.java | 42 +++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemRegistryFinalizedEvent.java create mode 100644 src/test/java/io/github/thebusybiscuit/slimefun4/api/events/TestSlimefunRegistryFinalizedEvent.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemRegistryFinalizedEvent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemRegistryFinalizedEvent.java new file mode 100644 index 0000000000..12943c82bf --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/SlimefunItemRegistryFinalizedEvent.java @@ -0,0 +1,35 @@ +package io.github.thebusybiscuit.slimefun4.api.events; + +import javax.annotation.Nonnull; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; +import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; + + +/** + * This {@link Event} is fired after {@link Slimefun} finishes loading the + * {@link SlimefunItem} registry. We recommend listening to this event if you + * want to register recipes using items from other addons. + * + * @author ProfElements + */ +public class SlimefunItemRegistryFinalizedEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + + public SlimefunItemRegistryFinalizedEvent() {} + + @Nonnull + public static HandlerList getHandlerList() { + return handlers; + } + + @Nonnull + @Override + public HandlerList getHandlers() { + return getHandlerList(); + } +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/PostSetup.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/PostSetup.java index 13e7e7ef5b..ce8694bd31 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/PostSetup.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/PostSetup.java @@ -24,6 +24,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import io.github.thebusybiscuit.slimefun4.api.events.SlimefunItemRegistryFinalizedEvent; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; @@ -77,6 +78,8 @@ public static void loadItems() { } } + Bukkit.getPluginManager().callEvent(new SlimefunItemRegistryFinalizedEvent()); + loadOreGrinderRecipes(); loadSmelteryRecipes(); diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/api/events/TestSlimefunRegistryFinalizedEvent.java b/src/test/java/io/github/thebusybiscuit/slimefun4/api/events/TestSlimefunRegistryFinalizedEvent.java new file mode 100644 index 0000000000..bef63828c9 --- /dev/null +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/api/events/TestSlimefunRegistryFinalizedEvent.java @@ -0,0 +1,42 @@ +package io.github.thebusybiscuit.slimefun4.api.events; + +import org.junit.jupiter.api.Assertions; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import be.seeseemelk.mockbukkit.MockBukkit; +import be.seeseemelk.mockbukkit.ServerMock; +import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; +import io.github.thebusybiscuit.slimefun4.implementation.setup.PostSetup; + +class TestSlimefunRegistryFinalizedEvent { + + private static ServerMock server; + private static Slimefun plugin; + + @BeforeAll + public static void load() { + server = MockBukkit.mock(); + plugin = MockBukkit.load(Slimefun.class); + } + + @AfterAll + public static void unload() { + MockBukkit.unmock(); + } + + @Test + @DisplayName("Test that SlimefunRegistryFinalizedEvent is fired") + void testEventIsFired() { + // Make sure post setup does not throw + Assertions.assertDoesNotThrow(() -> PostSetup.loadItems()); + + // Make sure post setup sent the event + server.getPluginManager().assertEventFired(SlimefunItemRegistryFinalizedEvent.class, ignored -> true); + + server.getPluginManager().clearEvents(); + } +}