Prevent infinite load screen when not teleporting the player on spawning

This commit is contained in:
themode 2020-11-14 21:45:30 +01:00
parent ea6981c4b0
commit 1d1e82aeb3
6 changed files with 22 additions and 13 deletions

View File

@ -616,6 +616,16 @@ public class Player extends LivingEntity implements CommandSender {
return result;
}
/**
* Changes the player instance and load surrounding chunks if needed.
* <p>
* Be aware that because chunk operations are expensive,
* it is possible for this method to be non-blocking when retrieving chunks is required.
* <p>
* When this method is called for the first time (during player login), the player will be teleport at {@link #getRespawnPoint()}.
*
* @param instance the new instance of the player
*/
@Override
public void setInstance(@NotNull Instance instance) {
Check.notNull(instance, "instance cannot be null!");
@ -683,6 +693,11 @@ public class Player extends LivingEntity implements CommandSender {
private void spawnPlayer(Instance instance, boolean firstSpawn) {
this.viewableEntities.forEach(entity -> entity.removeViewer(this));
super.setInstance(instance);
if (firstSpawn) {
teleport(getRespawnPoint());
}
PlayerSpawnEvent spawnEvent = new PlayerSpawnEvent(this, instance, firstSpawn);
callEvent(PlayerSpawnEvent.class, spawnEvent);
}

View File

@ -35,7 +35,7 @@ public class PlayerLoginEvent extends Event {
* <p>
* WARNING: this must NOT be null, otherwise the player cannot spawn.
*
* @return the spawning instance
* @return the spawning instance, null if not already defined
*/
@Nullable
public Instance getSpawningInstance() {

View File

@ -18,6 +18,8 @@ public final class OptifineSupport {
/**
* Enables optifine support by registering the required biomes.
*
* @throws IllegalStateException if optifine support is already enabled
*/
public static void enable() {
Check.stateCondition(enabled, "Optifine support is already enabled!");

View File

@ -17,7 +17,7 @@ import java.util.concurrent.atomic.AtomicInteger;
@EqualsAndHashCode
public class Biome {
public static final AtomicInteger idCounter = new AtomicInteger(0);
public static final AtomicInteger ID_COUNTER = new AtomicInteger(0);
private static final BiomeEffects DEFAULT_EFFECTS = BiomeEffects.builder()
.fog_color(0xC0D8FF)
@ -37,7 +37,7 @@ public class Biome {
.effects(DEFAULT_EFFECTS)
.build();
private final int id = idCounter.getAndIncrement();
private final int id = ID_COUNTER.getAndIncrement();
private final NamespaceID name;
@Builder.Default

View File

@ -15,7 +15,7 @@ import java.util.Collections;
* <p>
* Contains {@link Biome#PLAINS} by default but can be removed.
*/
public class BiomeManager {
public final class BiomeManager {
private final Int2ObjectMap<Biome> biomes = new Int2ObjectOpenHashMap<>();

View File

@ -1,9 +1,7 @@
package demo;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.GameMode;
import net.minestom.server.event.player.PlayerLoginEvent;
import net.minestom.server.event.player.PlayerSpawnEvent;
import net.minestom.server.instance.*;
import net.minestom.server.instance.batch.ChunkBatch;
import net.minestom.server.instance.block.Block;
@ -34,13 +32,7 @@ public class MainDemo {
// Set the spawning instance
player.addEventCallback(PlayerLoginEvent.class, event -> {
event.setSpawningInstance(instanceContainer);
player.setRespawnPoint(new Position(0,45,0));
});
// Teleport the player at spawn
player.addEventCallback(PlayerSpawnEvent.class, event -> {
player.teleport(new Position(0, 45, 0));
player.setGameMode(GameMode.CREATIVE);
player.setRespawnPoint(new Position(0, 45, 0));
});
});