From b7b95e0ebe42976cf99b08cc3065fa3fea7dd3c1 Mon Sep 17 00:00:00 2001 From: jglrxavpok Date: Tue, 25 Aug 2020 16:05:57 +0200 Subject: [PATCH] Fixed respawn button disconnecting the client --- src/main/java/fr/themode/demo/MainDemo.java | 3 +++ .../net/minestom/server/entity/Player.java | 11 +++++++++++ .../packet/server/login/JoinGamePacket.java | 2 +- .../packet/server/play/RespawnPacket.java | 2 +- .../minestom/server/world/DimensionType.java | 19 +++---------------- .../server/world/DimensionTypeManager.java | 2 +- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/main/java/fr/themode/demo/MainDemo.java b/src/main/java/fr/themode/demo/MainDemo.java index cb7a61b9e..eea8578ec 100644 --- a/src/main/java/fr/themode/demo/MainDemo.java +++ b/src/main/java/fr/themode/demo/MainDemo.java @@ -1,6 +1,7 @@ package fr.themode.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.*; @@ -33,11 +34,13 @@ 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); }); }); diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 3c47f2887..531e0be57 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -506,6 +506,17 @@ public class Player extends LivingEntity implements CommandSender { // Runnable called when teleportation is successful (after loading and sending necessary chunk) teleport(respawnEvent.getRespawnPosition(), this::refreshAfterTeleport); + + resendVisibleChunks(); + } + + private void resendVisibleChunks() { + for (Chunk chunk : viewableChunks) { + chunk.removeViewer(this); + } + viewableChunks.clear(); + BlockPosition pos = position.toBlockPosition(); + onChunkChange(instance.getChunk(pos.getX() >> 4, pos.getZ() >> 4)); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/login/JoinGamePacket.java b/src/main/java/net/minestom/server/network/packet/server/login/JoinGamePacket.java index a02f264c1..acac7ccd1 100644 --- a/src/main/java/net/minestom/server/network/packet/server/login/JoinGamePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/login/JoinGamePacket.java @@ -44,7 +44,7 @@ public class JoinGamePacket implements ServerPacket { nbt.set("minecraft:worldgen/biome", biomes); writer.writeNBT("", nbt); - writer.writeNBT("", dimensionType.toNBT2()); + writer.writeNBT("", dimensionType.toNBT()); writer.writeSizedString(dimensionType.getName().toString()); writer.writeLong(hashedSeed); diff --git a/src/main/java/net/minestom/server/network/packet/server/play/RespawnPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/RespawnPacket.java index 05f398c84..6e4e089e1 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/RespawnPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/RespawnPacket.java @@ -17,7 +17,7 @@ public class RespawnPacket implements ServerPacket { @Override public void write(BinaryWriter writer) { //TODO add api - writer.writeNBT("", dimensionType.toNBT2()); + writer.writeNBT("", dimensionType.toNBT()); // Warning: must be different for each dimension type! Otherwise the client seems to cache the world name writer.writeSizedString(dimensionType.getName().toString()); diff --git a/src/main/java/net/minestom/server/world/DimensionType.java b/src/main/java/net/minestom/server/world/DimensionType.java index 34293fd72..c8cdd098a 100644 --- a/src/main/java/net/minestom/server/world/DimensionType.java +++ b/src/main/java/net/minestom/server/world/DimensionType.java @@ -58,29 +58,16 @@ public class DimensionType { return hiddenBuilder().name(name); } - public NBTCompound toNBT() { + public NBTCompound toIndexedNBT() { NBTCompound nbt = new NBTCompound(); - NBTCompound element = new NBTCompound() - .setFloat("ambient_light", ambientLight) - .setString("infiniburn", infiniburn.toString()) - .setByte("natural", (byte) (natural ? 0x01 : 0x00)) - .setByte("has_ceiling", (byte) (ceilingEnabled ? 0x01 : 0x00)) - .setByte("has_skylight", (byte) (skylightEnabled ? 0x01 : 0x00)) - .setByte("ultrawarm", (byte) (ultrawarm ? 0x01 : 0x00)) - .setByte("has_raids", (byte) (raidCapable ? 0x01 : 0x00)) - .setByte("respawn_anchor_works", (byte) (respawnAnchorSafe ? 0x01 : 0x00)) - .setByte("bed_works", (byte) (bedSafe ? 0x01 : 0x00)) - .setByte("piglin_safe", (byte) (piglinSafe ? 0x01 : 0x00)) - .setInt("logical_height", logicalHeight) - .setInt("coordinate_scale", coordinateScale); - fixedTime.ifPresent(time -> element.setLong("fixed_time", time)); + NBTCompound element = toNBT(); nbt.setString("name", name.toString()); nbt.setInt("id", id); nbt.set("element", element); return nbt; } - public NBTCompound toNBT2() { + public NBTCompound toNBT() { NBTCompound nbt = new NBTCompound() .setFloat("ambient_light", ambientLight) .setString("infiniburn", infiniburn.toString()) diff --git a/src/main/java/net/minestom/server/world/DimensionTypeManager.java b/src/main/java/net/minestom/server/world/DimensionTypeManager.java index 768a68db8..f45dc2900 100644 --- a/src/main/java/net/minestom/server/world/DimensionTypeManager.java +++ b/src/main/java/net/minestom/server/world/DimensionTypeManager.java @@ -53,7 +53,7 @@ public class DimensionTypeManager { dimensions.setString("type", "minecraft:dimension_type"); NBTList dimensionList = new NBTList<>(NBTTypes.TAG_Compound); for (DimensionType dimensionType : dimensionTypes) { - dimensionList.add(dimensionType.toNBT()); + dimensionList.add(dimensionType.toIndexedNBT()); } dimensions.set("value", dimensionList); return dimensions;