Added InstanceManager#unregisterInstance

This commit is contained in:
themode 2020-09-27 19:02:05 +02:00
parent 5ff56212a2
commit 670887f7b3
3 changed files with 23 additions and 5 deletions

View File

@ -1606,9 +1606,6 @@ public class Player extends LivingEntity implements CommandSender {
PlayerPositionAndLookPacket positionAndLookPacket = new PlayerPositionAndLookPacket();
positionAndLookPacket.position = position.clone();
positionAndLookPacket.flags = 0x00;
if (teleportId == Integer.MAX_VALUE) {
teleportId = 0;
}
positionAndLookPacket.teleportId = teleportId++;
playerConnection.sendPacket(positionAndLookPacket);
}

View File

@ -54,7 +54,7 @@ public class InstanceContainer extends Instance {
private ChunkGenerator chunkGenerator;
private final ConcurrentHashMap<Long, Chunk> chunks = new ConcurrentHashMap<>();
private final Set<Chunk> scheduledChunksToRemove = new HashSet<>();
protected final Set<Chunk> scheduledChunksToRemove = new HashSet<>();
private ReadWriteLock changingBlockLock = new ReentrantReadWriteLock();
private Map<BlockPosition, Block> currentlyChangingBlocks = new HashMap<>();
@ -657,7 +657,7 @@ public class InstanceContainer extends Instance {
* <p>
* Unsafe because it has to be done on the same thread as the instance/chunks tick update
*/
private void UNSAFE_unloadChunks() {
protected void UNSAFE_unloadChunks() {
synchronized (this.scheduledChunksToRemove) {
for (Chunk chunk : scheduledChunksToRemove) {
final int chunkX = chunk.getChunkX();

View File

@ -101,6 +101,27 @@ public final class InstanceManager {
return registerSharedInstance(sharedInstance);
}
/**
* Unregister the {@link Instance} internally
* <p>
* If {@code instance} is an {@link InstanceContainer} all chunks are unloaded
*
* @param instance the {@link Instance} to unregister
*/
public void unregisterInstance(Instance instance) {
Check.stateCondition(!instance.getPlayers().isEmpty(), "You cannot unregister an instance with players");
// Unload all chunks
if (instance instanceof InstanceContainer) {
InstanceContainer instanceContainer = (InstanceContainer) instance;
instanceContainer.scheduledChunksToRemove.addAll(instanceContainer.getChunks());
instanceContainer.UNSAFE_unloadChunks();
}
instance.setRegistered(false);
this.instances.remove(instance);
}
/**
* Get all the registered instances
*