`Player#UNSAFE_init` now set the instance

This commit is contained in:
TheMode 2021-08-15 00:52:07 +02:00
parent e7cbe49a42
commit af50bbb440
3 changed files with 12 additions and 11 deletions

View File

@ -33,7 +33,6 @@ import net.minestom.server.potion.TimedPotion;
import net.minestom.server.tag.Tag;
import net.minestom.server.tag.TagHandler;
import net.minestom.server.thread.ThreadProvider;
import net.minestom.server.utils.async.AsyncUtils;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.entity.EntityUtils;
import net.minestom.server.utils.player.PlayerUtils;
@ -713,9 +712,9 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
public CompletableFuture<Void> setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) {
Check.stateCondition(!instance.isRegistered(),
"Instances need to be registered, please use InstanceManager#registerInstance or InstanceManager#registerSharedInstance");
if (isRemoved()) return AsyncUtils.VOID_FUTURE;
if (this.instance != null) {
this.instance.UNSAFE_removeEntity(this);
final Instance previousInstance = this.instance;
if (previousInstance != null) {
previousInstance.UNSAFE_removeEntity(this);
}
this.position = spawnPosition;
this.isActive = true;

View File

@ -282,6 +282,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
this.playerConnection.sendPacket(getPropertiesPacket()); // Send default properties
refreshHealth(); // Heal and send health packet
refreshAbilities(); // Send abilities packet
setInstance(spawnInstance);
}
/**
@ -423,7 +425,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
// Runnable called when teleportation is successful (after loading and sending necessary chunk)
teleport(respawnEvent.getRespawnPosition()).thenRun(this::refreshAfterTeleport);
}
/**
* Refreshes the command list for this player. This checks the
* {@link net.minestom.server.command.builder.condition.CommandCondition}s
@ -497,12 +499,13 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
*/
@Override
public CompletableFuture<Void> setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) {
Check.argCondition(this.instance == instance, "Instance should be different than the current one");
final Instance currentInstance = this.instance;
Check.argCondition(currentInstance == instance, "Instance should be different than the current one");
// true if the chunks need to be sent to the client, can be false if the instances share the same chunks (e.g. SharedInstance)
if (!InstanceUtils.areLinked(this.instance, instance) || !spawnPosition.sameChunk(this.position)) {
if (!InstanceUtils.areLinked(currentInstance, instance) || !spawnPosition.sameChunk(this.position)) {
final boolean firstSpawn = currentInstance == null;
return instance.loadOptionalChunk(spawnPosition)
.thenRun(() -> spawnPlayer(instance, spawnPosition,
this.instance == null,
.thenRun(() -> spawnPlayer(instance, spawnPosition, firstSpawn,
!Objects.equals(dimensionType, instance.getDimensionType()), true));
} else {
// The player already has the good version of all the chunks.

View File

@ -372,9 +372,8 @@ public final class ConnectionManager {
EventDispatcher.call(loginEvent);
final Instance spawningInstance = loginEvent.getSpawningInstance();
Check.notNull(spawningInstance, "You need to specify a spawning instance in the PlayerLoginEvent");
// Spawn the player at Player#getRespawnPoint during the next instance tick
// Spawn the player at Player#getRespawnPoint
waitingPlayer.UNSAFE_init(spawningInstance);
spawningInstance.scheduleNextTick(waitingPlayer::setInstance);
}
}