Instance register and unregister events (#608)

* Added InstanceRegisterEvent and InstanceUnregisterEvent

* Add unit test
This commit is contained in:
EpicPlayerA10 2023-03-30 23:35:24 +02:00 committed by GitHub
parent 0b77fb805b
commit 4f3c35ee87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}