diff --git a/src/main/java/net/minestom/server/event/instance/InstanceRegisterEvent.java b/src/main/java/net/minestom/server/event/instance/InstanceRegisterEvent.java new file mode 100644 index 000000000..628f9e9b9 --- /dev/null +++ b/src/main/java/net/minestom/server/event/instance/InstanceRegisterEvent.java @@ -0,0 +1,21 @@ +package net.minestom.server.event.instance; + +import net.minestom.server.event.trait.InstanceEvent; +import net.minestom.server.instance.Instance; +import org.jetbrains.annotations.NotNull; + +/** + * Called when an instance is registered + */ +public class InstanceRegisterEvent implements InstanceEvent { + private final Instance instance; + + public InstanceRegisterEvent(@NotNull Instance instance) { + this.instance = instance; + } + + @Override + public @NotNull Instance getInstance() { + return instance; + } +} diff --git a/src/main/java/net/minestom/server/event/instance/InstanceUnregisterEvent.java b/src/main/java/net/minestom/server/event/instance/InstanceUnregisterEvent.java new file mode 100644 index 000000000..6952bd2a3 --- /dev/null +++ b/src/main/java/net/minestom/server/event/instance/InstanceUnregisterEvent.java @@ -0,0 +1,21 @@ +package net.minestom.server.event.instance; + +import net.minestom.server.event.trait.InstanceEvent; +import net.minestom.server.instance.Instance; +import org.jetbrains.annotations.NotNull; + +/** + * Called when an instance is unregistered + */ +public class InstanceUnregisterEvent implements InstanceEvent { + private final Instance instance; + + public InstanceUnregisterEvent(@NotNull Instance instance) { + this.instance = instance; + } + + @Override + public @NotNull Instance getInstance() { + return instance; + } +} diff --git a/src/main/java/net/minestom/server/instance/InstanceManager.java b/src/main/java/net/minestom/server/instance/InstanceManager.java index f25229611..2a0bc6769 100644 --- a/src/main/java/net/minestom/server/instance/InstanceManager.java +++ b/src/main/java/net/minestom/server/instance/InstanceManager.java @@ -1,6 +1,9 @@ package net.minestom.server.instance; import net.minestom.server.MinecraftServer; +import net.minestom.server.event.EventDispatcher; +import net.minestom.server.event.instance.InstanceRegisterEvent; +import net.minestom.server.event.instance.InstanceUnregisterEvent; import net.minestom.server.utils.validate.Check; import net.minestom.server.world.DimensionType; import org.jetbrains.annotations.ApiStatus; @@ -109,6 +112,9 @@ public final class InstanceManager { public void unregisterInstance(@NotNull Instance instance) { Check.stateCondition(!instance.getPlayers().isEmpty(), "You cannot unregister an instance with players inside."); synchronized (instance) { + InstanceUnregisterEvent event = new InstanceUnregisterEvent(instance); + EventDispatcher.call(event); + // Unload all chunks if (instance instanceof InstanceContainer) { instance.getChunks().forEach(instance::unloadChunk); @@ -156,5 +162,7 @@ public final class InstanceManager { this.instances.add(instance); var dispatcher = MinecraftServer.process().dispatcher(); instance.getChunks().forEach(dispatcher::createPartition); + InstanceRegisterEvent event = new InstanceRegisterEvent(instance); + EventDispatcher.call(event); } } diff --git a/src/test/java/net/minestom/server/instance/InstanceEventsIntegrationTest.java b/src/test/java/net/minestom/server/instance/InstanceEventsIntegrationTest.java new file mode 100644 index 000000000..8f2404e5a --- /dev/null +++ b/src/test/java/net/minestom/server/instance/InstanceEventsIntegrationTest.java @@ -0,0 +1,22 @@ +package net.minestom.server.instance; + +import net.minestom.server.event.instance.InstanceRegisterEvent; +import net.minestom.server.event.instance.InstanceUnregisterEvent; +import net.minestom.testing.Env; +import net.minestom.testing.EnvTest; +import org.junit.jupiter.api.Test; + +@EnvTest +public class InstanceEventsIntegrationTest { + @Test + public void registerAndUnregisterInstance(Env env) { + var registerListener = env.listen(InstanceRegisterEvent.class); + var unregisterListener = env.listen(InstanceUnregisterEvent.class); + + registerListener.followup(); + Instance instance = env.process().instance().createInstanceContainer(); + + unregisterListener.followup(); + env.destroyInstance(instance); + } +}