Skip to content

Commit

Permalink
✨ feat(api): Introduce SlimefunItemRegistryFinalizedEvent (#4099)
Browse files Browse the repository at this point in the history
Co-authored-by: JustAHuman-xD <[email protected]>
Co-authored-by: Daniel Walsh <[email protected]>
  • Loading branch information
3 people authored Jan 18, 2024
1 parent bcdde8c commit 7c917c3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,6 +78,8 @@ public static void loadItems() {
}
}

Bukkit.getPluginManager().callEvent(new SlimefunItemRegistryFinalizedEvent());

loadOreGrinderRecipes();
loadSmelteryRecipes();

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 7c917c3

Please sign in to comment.