Only send chunks if required

This commit is contained in:
themode 2020-10-31 00:23:52 +01:00
parent f334df0028
commit a6939b3695
2 changed files with 90 additions and 39 deletions

View File

@ -55,6 +55,7 @@ import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.callback.OptionalCallback;
import net.minestom.server.utils.chunk.ChunkCallback;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.instance.InstanceUtils;
import net.minestom.server.utils.validate.Check;
import net.minestom.server.world.DimensionType;
import org.jetbrains.annotations.NotNull;
@ -604,6 +605,11 @@ public class Player extends LivingEntity implements CommandSender {
Check.argCondition(this.instance == instance, "Instance should be different than the current one");
final boolean firstSpawn = this.instance == null; // TODO: Handle player reconnections, must be false in that case too
// true if the chunks need to be send to the client, can be false if the instances share the same chunks (eg SharedInstance)
final boolean needWorldRefresh = !InstanceUtils.areLinked(this.instance, instance);
if (needWorldRefresh) {
// Remove all previous viewable chunks (from the previous instance)
for (Chunk viewableChunk : viewableChunks) {
viewableChunk.removeViewer(this);
@ -632,10 +638,7 @@ public class Player extends LivingEntity implements CommandSender {
final boolean isLast = counter.get() == length - 1;
if (isLast) {
// This is the last chunk to be loaded , spawn player
this.viewableEntities.forEach(entity -> entity.removeViewer(this));
super.setInstance(instance);
PlayerSpawnEvent spawnEvent = new PlayerSpawnEvent(this, instance, firstSpawn);
callEvent(PlayerSpawnEvent.class, spawnEvent);
spawnPlayer(instance, firstSpawn);
} else {
// Increment the counter of current loaded chunks
counter.incrementAndGet();
@ -645,6 +648,25 @@ public class Player extends LivingEntity implements CommandSender {
// WARNING: if auto load is disabled and no chunks are loaded beforehand, player will be stuck.
instance.loadOptionalChunk(chunkX, chunkZ, callback);
}
} else {
spawnPlayer(instance, firstSpawn);
}
}
/**
* Used to spawn the player once the client has all the required chunks.
* <p>
* Does add the player to {@code instance}, remove all viewable entities and call {@link PlayerSpawnEvent}.
* <p>
* UNSAFE: only called with {@link #setInstance(Instance)}.
*
* @param firstSpawn true if this is the player first spawn
*/
private void spawnPlayer(Instance instance, boolean firstSpawn) {
this.viewableEntities.forEach(entity -> entity.removeViewer(this));
super.setInstance(instance);
PlayerSpawnEvent spawnEvent = new PlayerSpawnEvent(this, instance, firstSpawn);
callEvent(PlayerSpawnEvent.class, spawnEvent);
}
@NotNull

View File

@ -0,0 +1,29 @@
package net.minestom.server.utils.instance;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.instance.SharedInstance;
public final class InstanceUtils {
/**
* Gets if two instances share the same chunks.
*
* @param instance1 the first instance
* @param instance2 the second instance
* @return true if the two instances share the same chunks
*/
public static boolean areLinked(Instance instance1, Instance instance2) {
if (instance1 instanceof InstanceContainer && instance2 instanceof SharedInstance) {
return ((SharedInstance) instance2).getInstanceContainer().equals(instance1);
} else if (instance2 instanceof InstanceContainer && instance1 instanceof SharedInstance) {
return ((SharedInstance) instance1).getInstanceContainer().equals(instance2);
} else if (instance1 instanceof SharedInstance && instance2 instanceof SharedInstance) {
final InstanceContainer container1 = ((SharedInstance) instance1).getInstanceContainer();
final InstanceContainer container2 = ((SharedInstance) instance2).getInstanceContainer();
return container1.equals(container2);
}
return false;
}
}