From 48b637cbc85f2b28aa74cb3ab07495e2aef2a9bf Mon Sep 17 00:00:00 2001 From: themode Date: Sat, 21 Nov 2020 20:56:32 +0100 Subject: [PATCH] Fixed player spawn when a respawn point is not specified --- src/main/java/net/minestom/server/entity/Player.java | 11 ++++++++--- src/test/java/demo/MainDemo.java | 10 +++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index ea262ff39..0239d189e 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -698,7 +698,12 @@ public class Player extends LivingEntity implements CommandSender { // true if the chunks need to be sent 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 && !firstSpawn) { + // true if the player needs every chunk around its position + final boolean needChunkLoad = !firstSpawn || firstSpawn && + ChunkUtils.getChunkCoordinate((int) getRespawnPoint().getX()) == 0 && + ChunkUtils.getChunkCoordinate((int) getRespawnPoint().getZ()) == 0; + + if (needWorldRefresh && needChunkLoad) { // Remove all previous viewable chunks (from the previous instance) for (Chunk viewableChunk : viewableChunks) { viewableChunk.removeViewer(this); @@ -729,7 +734,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 - spawnPlayer(instance, false); + spawnPlayer(instance, firstSpawn); } else { // Increment the counter of current loaded chunks counter.incrementAndGet(); @@ -739,7 +744,7 @@ 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 if (firstSpawn) { + } else if (!needChunkLoad) { // The player always believe that his position is 0;0 so this is a pretty hacky fix instance.loadOptionalChunk(0, 0, chunk -> spawnPlayer(instance, true)); } else { diff --git a/src/test/java/demo/MainDemo.java b/src/test/java/demo/MainDemo.java index 6c0fcd6d6..e4cf7156f 100644 --- a/src/test/java/demo/MainDemo.java +++ b/src/test/java/demo/MainDemo.java @@ -29,14 +29,14 @@ public class MainDemo { // Add event listeners ConnectionManager connectionManager = MinecraftServer.getConnectionManager(); connectionManager.addPlayerInitialization(player -> { - // Set the spawning instance + // Set the spawning instance and spawn position player.addEventCallback(PlayerLoginEvent.class, event -> { event.setSpawningInstance(instanceContainer); - player.setRespawnPoint(new Position(0, 45, 0)); + player.setRespawnPoint(new Position(0, 42, 0)); }); }); - // Start the server + // Start the server on port 25565 minecraftServer.start("localhost", 25565); } @@ -55,7 +55,7 @@ public class MainDemo { @Override public void fillBiomes(Biome[] biomes, int chunkX, int chunkZ) { - Arrays.fill(biomes, MinecraftServer.getBiomeManager().getById(0)); + Arrays.fill(biomes, Biome.PLAINS); } @Override @@ -64,4 +64,4 @@ public class MainDemo { } } -} +} \ No newline at end of file