mirror of
https://github.com/Minestom/Minestom.git
synced 2024-11-15 15:16:46 +01:00
Instance register and unregister events (#608)
* Added InstanceRegisterEvent and InstanceUnregisterEvent * Add unit test
This commit is contained in:
parent
0b77fb805b
commit
4f3c35ee87
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user