Fix shared instance unregistering

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-02-07 23:39:57 +01:00
parent 0f352a8f92
commit b26abc11b0
3 changed files with 41 additions and 2 deletions

View File

@ -114,12 +114,12 @@ public final class InstanceManager {
// Unload all chunks
if (instance instanceof InstanceContainer) {
instance.getChunks().forEach(instance::unloadChunk);
var dispatcher = MinecraftServer.process().dispatcher();
instance.getChunks().forEach(dispatcher::deletePartition);
}
// Unregister
instance.setRegistered(false);
this.instances.remove(instance);
var dispatcher = MinecraftServer.process().dispatcher();
instance.getChunks().forEach(dispatcher::deletePartition);
}
}

View File

@ -11,6 +11,12 @@ public interface FlexibleListener<E extends Event> {
*/
void followup(@NotNull Consumer<E> handler);
default void followup() {
followup(event -> {
// Empty
});
}
/**
* Fails if an event is received. Valid until the next followup call.
*/

View File

@ -0,0 +1,33 @@
package net.minestom.server.instance;
import net.minestom.server.api.Env;
import net.minestom.server.api.EnvTest;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.event.player.PlayerTickEvent;
import org.junit.jupiter.api.Test;
@EnvTest
public class InstanceUnregisterIntegrationTest {
@Test
public void sharedInstance(Env env) {
// Ensure that unregistering a shared instance does not unload the container chunks
var instanceManager = env.process().instance();
var instance = instanceManager.createInstanceContainer();
var shared1 = instanceManager.createSharedInstance(instance);
var connection = env.createConnection();
var player = connection.connect(shared1, new Pos(0, 40, 0)).join();
var listener = env.listen(PlayerTickEvent.class);
listener.followup();
env.tick();
player.setInstance(instanceManager.createSharedInstance(instance)).join();
listener.followup();
env.tick();
instanceManager.unregisterInstance(shared1);
listener.followup();
env.tick();
}
}