Fixed respawn button disconnecting the client

This commit is contained in:
jglrxavpok 2020-08-25 16:05:57 +02:00
parent f26501c5fa
commit b7b95e0ebe
6 changed files with 20 additions and 19 deletions

View File

@ -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);
});
});

View File

@ -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

View File

@ -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);

View File

@ -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());

View File

@ -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())

View File

@ -53,7 +53,7 @@ public class DimensionTypeManager {
dimensions.setString("type", "minecraft:dimension_type");
NBTList<NBTCompound> dimensionList = new NBTList<>(NBTTypes.TAG_Compound);
for (DimensionType dimensionType : dimensionTypes) {
dimensionList.add(dimensionType.toNBT());
dimensionList.add(dimensionType.toIndexedNBT());
}
dimensions.set("value", dimensionList);
return dimensions;