From 0af7a4c63dad0c82e8c62f17c52695f53ab022d3 Mon Sep 17 00:00:00 2001 From: Nan1t Date: Sun, 11 Dec 2022 13:07:06 +0300 Subject: [PATCH] Removed useless spawnPosition from config. Send spawn position and look snapshot depending on client version --- .../limbo/configuration/LimboConfig.java | 10 +- .../limbo/connection/ClientConnection.java | 7 +- .../limbo/connection/PacketSnapshots.java | 31 ++--- .../protocol/packets/play/PacketJoinGame.java | 2 +- .../play/PacketPlayerPositionAndLook.java | 26 +---- .../packets/play/PacketSpawnPosition.java | 14 +-- .../ru/nanit/limbo/server/LimboServer.java | 2 +- .../world/{dimension => }/Dimension.java | 2 +- .../{dimension => }/DimensionRegistry.java | 2 +- .../java/ru/nanit/limbo/world/Location.java | 106 ------------------ src/main/resources/settings.yml | 8 -- 11 files changed, 35 insertions(+), 175 deletions(-) rename src/main/java/ru/nanit/limbo/world/{dimension => }/Dimension.java (96%) rename src/main/java/ru/nanit/limbo/world/{dimension => }/DimensionRegistry.java (99%) delete mode 100644 src/main/java/ru/nanit/limbo/world/Location.java diff --git a/src/main/java/ru/nanit/limbo/configuration/LimboConfig.java b/src/main/java/ru/nanit/limbo/configuration/LimboConfig.java index ac27ff1..78cdb12 100644 --- a/src/main/java/ru/nanit/limbo/configuration/LimboConfig.java +++ b/src/main/java/ru/nanit/limbo/configuration/LimboConfig.java @@ -23,7 +23,6 @@ import org.spongepowered.configurate.serialize.TypeSerializerCollection; import org.spongepowered.configurate.yaml.YamlConfigurationLoader; import ru.nanit.limbo.server.data.*; import ru.nanit.limbo.util.Colors; -import ru.nanit.limbo.world.Location; import java.io.BufferedReader; import java.io.FileNotFoundException; @@ -43,7 +42,6 @@ public final class LimboConfig { private PingData pingData; private String dimensionType; - private Location spawnPosition; private int gameMode; private boolean useBrandName; @@ -86,14 +84,13 @@ public final class LimboConfig { address = conf.node("bind").get(SocketAddress.class); maxPlayers = conf.node("maxPlayers").getInt(); pingData = conf.node("ping").get(PingData.class); - dimensionType = conf.node("dimension").getString(); + dimensionType = conf.node("dimension").getString("the_end"); if (dimensionType.equalsIgnoreCase("nether")) { dimensionType = "the_nether"; } if (dimensionType.equalsIgnoreCase("end")) { dimensionType = "the_end"; } - spawnPosition = conf.node("spawnPosition").get(Location.class); gameMode = conf.node("gameMode").getInt(); useBrandName = conf.node("brandName", "enable").getBoolean(); useJoinMessage = conf.node("joinMessage", "enable").getBoolean(); @@ -152,7 +149,6 @@ public final class LimboConfig { .register(PingData.class, new PingData.Serializer()) .register(BossBar.class, new BossBar.Serializer()) .register(Title.class, new Title.Serializer()) - .register(Location.class, new Location.Serializer()) .build(); } @@ -172,10 +168,6 @@ public final class LimboConfig { return dimensionType; } - public Location getSpawnPosition() { - return spawnPosition; - } - public int getGameMode() { return gameMode; } diff --git a/src/main/java/ru/nanit/limbo/connection/ClientConnection.java b/src/main/java/ru/nanit/limbo/connection/ClientConnection.java index 75cbc74..aca01ec 100644 --- a/src/main/java/ru/nanit/limbo/connection/ClientConnection.java +++ b/src/main/java/ru/nanit/limbo/connection/ClientConnection.java @@ -131,7 +131,12 @@ public class ClientConnection extends ChannelInboundHandlerAdapter { Runnable sendPlayPackets = () -> { writePacket(PacketSnapshots.PACKET_JOIN_GAME); writePacket(PacketSnapshots.PACKET_PLAYER_ABILITIES); - writePacket(PacketSnapshots.PACKET_PLAYER_POS); + + if (clientVersion.less(Version.V1_9)) { + writePacket(PacketSnapshots.PACKET_PLAYER_POS_AND_LOOK_LEGACY); + } else { + writePacket(PacketSnapshots.PACKET_PLAYER_POS_AND_LOOK); + } if (clientVersion.moreOrEqual(Version.V1_19_3)) writePacket(PacketSnapshots.PACKET_SPAWN_POSITION); diff --git a/src/main/java/ru/nanit/limbo/connection/PacketSnapshots.java b/src/main/java/ru/nanit/limbo/connection/PacketSnapshots.java index 6c993bd..c1ab3bd 100644 --- a/src/main/java/ru/nanit/limbo/connection/PacketSnapshots.java +++ b/src/main/java/ru/nanit/limbo/connection/PacketSnapshots.java @@ -38,11 +38,14 @@ public final class PacketSnapshots { public static PacketSnapshot PACKET_PLAYER_ABILITIES; public static PacketSnapshot PACKET_PLAYER_INFO; public static PacketSnapshot PACKET_DECLARE_COMMANDS; - public static PacketSnapshot PACKET_PLAYER_POS; public static PacketSnapshot PACKET_JOIN_MESSAGE; public static PacketSnapshot PACKET_BOSS_BAR; public static PacketSnapshot PACKET_HEADER_AND_FOOTER; + public static PacketSnapshot PACKET_PLAYER_POS_AND_LOOK_LEGACY; + // For 1.19 we need to spawn player outside world to avoid stuck in terrain loading + public static PacketSnapshot PACKET_PLAYER_POS_AND_LOOK; + public static PacketSnapshot PACKET_TITLE_TITLE; public static PacketSnapshot PACKET_TITLE_SUBTITLE; public static PacketSnapshot PACKET_TITLE_TIMES; @@ -63,6 +66,7 @@ public final class PacketSnapshots { loginSuccess.setUuid(uuid); PacketJoinGame joinGame = new PacketJoinGame(); + String worldName = "minecraft:" + server.getConfig().getDimensionType().toLowerCase(); joinGame.setEntityId(0); joinGame.setEnableRespawnScreen(true); joinGame.setFlat(false); @@ -73,7 +77,6 @@ public final class PacketSnapshots { joinGame.setReducedDebugInfo(true); joinGame.setDebug(false); joinGame.setViewDistance(0); - String worldName = "minecraft:" + server.getConfig().getDimensionType().toLowerCase(); joinGame.setWorldName(worldName); joinGame.setWorldNames(worldName); joinGame.setHashedSeed(0); @@ -84,13 +87,15 @@ public final class PacketSnapshots { playerAbilities.setFlags(0x02); playerAbilities.setFieldOfView(0.1F); - PacketPlayerPositionAndLook positionAndLook = new PacketPlayerPositionAndLook(); - positionAndLook.setX(server.getConfig().getSpawnPosition().getX()); - positionAndLook.setY(server.getConfig().getSpawnPosition().getY()); - positionAndLook.setZ(server.getConfig().getSpawnPosition().getZ()); - positionAndLook.setYaw(server.getConfig().getSpawnPosition().getYaw()); - positionAndLook.setPitch(server.getConfig().getSpawnPosition().getPitch()); - positionAndLook.setTeleportId(ThreadLocalRandom.current().nextInt()); + int teleportId = ThreadLocalRandom.current().nextInt(); + + PacketPlayerPositionAndLook positionAndLookLegacy + = new PacketPlayerPositionAndLook(0, 64, 0, 0, 0, teleportId); + + PacketPlayerPositionAndLook positionAndLook + = new PacketPlayerPositionAndLook(0, 400, 0, 0, 0, teleportId); + + PacketSpawnPosition packetSpawnPosition = new PacketSpawnPosition(0, 400, 0); PacketDeclareCommands declareCommands = new PacketDeclareCommands(); declareCommands.setCommands(Collections.emptyList()); @@ -100,16 +105,12 @@ public final class PacketSnapshots { info.setGameMode(server.getConfig().getGameMode()); info.setUuid(uuid); - PacketSpawnPosition packetSpawnPosition = new PacketSpawnPosition(); - packetSpawnPosition.setX((long) server.getConfig().getSpawnPosition().getX()); - packetSpawnPosition.setY((long) server.getConfig().getSpawnPosition().getY()); - packetSpawnPosition.setZ((long) server.getConfig().getSpawnPosition().getZ()); - PACKET_LOGIN_SUCCESS = PacketSnapshot.of(loginSuccess); PACKET_JOIN_GAME = PacketSnapshot.of(joinGame); + PACKET_PLAYER_POS_AND_LOOK_LEGACY = PacketSnapshot.of(positionAndLookLegacy); + PACKET_PLAYER_POS_AND_LOOK = PacketSnapshot.of(positionAndLook); PACKET_SPAWN_POSITION = PacketSnapshot.of(packetSpawnPosition); PACKET_PLAYER_ABILITIES = PacketSnapshot.of(playerAbilities); - PACKET_PLAYER_POS = PacketSnapshot.of(positionAndLook); PACKET_PLAYER_INFO = PacketSnapshot.of(info); PACKET_DECLARE_COMMANDS = PacketSnapshot.of(declareCommands); diff --git a/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketJoinGame.java b/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketJoinGame.java index afec119..3017ce6 100644 --- a/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketJoinGame.java +++ b/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketJoinGame.java @@ -20,7 +20,7 @@ package ru.nanit.limbo.protocol.packets.play; import ru.nanit.limbo.protocol.ByteMessage; import ru.nanit.limbo.protocol.PacketOut; import ru.nanit.limbo.protocol.registry.Version; -import ru.nanit.limbo.world.dimension.DimensionRegistry; +import ru.nanit.limbo.world.DimensionRegistry; public class PacketJoinGame implements PacketOut { diff --git a/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketPlayerPositionAndLook.java b/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketPlayerPositionAndLook.java index eb24dea..defec5d 100644 --- a/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketPlayerPositionAndLook.java +++ b/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketPlayerPositionAndLook.java @@ -28,34 +28,16 @@ public class PacketPlayerPositionAndLook implements PacketOut { private double z; private float yaw; private float pitch; - private byte flags = 0x08; private int teleportId; - public void setX(double x) { + public PacketPlayerPositionAndLook() {} + + public PacketPlayerPositionAndLook(double x, double y, double z, float yaw, float pitch, int teleportId) { this.x = x; - } - - public void setY(double y) { this.y = y; - } - - public void setZ(double z) { this.z = z; - } - - public void setYaw(float yaw) { this.yaw = yaw; - } - - public void setPitch(float pitch) { this.pitch = pitch; - } - - public void setFlags(byte flags) { - this.flags = flags; - } - - public void setTeleportId(int teleportId) { this.teleportId = teleportId; } @@ -68,7 +50,7 @@ public class PacketPlayerPositionAndLook implements PacketOut { msg.writeFloat(pitch); if (version.moreOrEqual(Version.V1_8)) { - msg.writeByte(flags); + msg.writeByte(0x08); } else { msg.writeBoolean(true); } diff --git a/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketSpawnPosition.java b/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketSpawnPosition.java index 559cdc3..0c464ae 100644 --- a/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketSpawnPosition.java +++ b/src/main/java/ru/nanit/limbo/protocol/packets/play/PacketSpawnPosition.java @@ -6,19 +6,13 @@ import ru.nanit.limbo.protocol.registry.Version; public class PacketSpawnPosition implements PacketOut { - private long x; - private long y; - private long z; + private final long x; + private final long y; + private final long z; - public void setX(long x) { + public PacketSpawnPosition(long x, long y, long z) { this.x = x; - } - - public void setY(long y) { this.y = y; - } - - public void setZ(long z) { this.z = z; } diff --git a/src/main/java/ru/nanit/limbo/server/LimboServer.java b/src/main/java/ru/nanit/limbo/server/LimboServer.java index a9ada1c..6d8ab3c 100644 --- a/src/main/java/ru/nanit/limbo/server/LimboServer.java +++ b/src/main/java/ru/nanit/limbo/server/LimboServer.java @@ -32,7 +32,7 @@ import ru.nanit.limbo.connection.ClientChannelInitializer; import ru.nanit.limbo.connection.ClientConnection; import ru.nanit.limbo.connection.PacketHandler; import ru.nanit.limbo.connection.PacketSnapshots; -import ru.nanit.limbo.world.dimension.DimensionRegistry; +import ru.nanit.limbo.world.DimensionRegistry; import java.nio.file.Paths; import java.util.concurrent.ScheduledFuture; diff --git a/src/main/java/ru/nanit/limbo/world/dimension/Dimension.java b/src/main/java/ru/nanit/limbo/world/Dimension.java similarity index 96% rename from src/main/java/ru/nanit/limbo/world/dimension/Dimension.java rename to src/main/java/ru/nanit/limbo/world/Dimension.java index 4198688..5b3c815 100644 --- a/src/main/java/ru/nanit/limbo/world/dimension/Dimension.java +++ b/src/main/java/ru/nanit/limbo/world/Dimension.java @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -package ru.nanit.limbo.world.dimension; +package ru.nanit.limbo.world; import net.kyori.adventure.nbt.CompoundBinaryTag; diff --git a/src/main/java/ru/nanit/limbo/world/dimension/DimensionRegistry.java b/src/main/java/ru/nanit/limbo/world/DimensionRegistry.java similarity index 99% rename from src/main/java/ru/nanit/limbo/world/dimension/DimensionRegistry.java rename to src/main/java/ru/nanit/limbo/world/DimensionRegistry.java index c7fe75f..eaaa1ac 100644 --- a/src/main/java/ru/nanit/limbo/world/dimension/DimensionRegistry.java +++ b/src/main/java/ru/nanit/limbo/world/DimensionRegistry.java @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -package ru.nanit.limbo.world.dimension; +package ru.nanit.limbo.world; import net.kyori.adventure.nbt.CompoundBinaryTag; import net.kyori.adventure.nbt.ListBinaryTag; diff --git a/src/main/java/ru/nanit/limbo/world/Location.java b/src/main/java/ru/nanit/limbo/world/Location.java deleted file mode 100644 index 5315609..0000000 --- a/src/main/java/ru/nanit/limbo/world/Location.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2020 Nan1t - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package ru.nanit.limbo.world; - -import org.checkerframework.checker.nullness.qual.Nullable; -import org.spongepowered.configurate.ConfigurationNode; -import org.spongepowered.configurate.serialize.TypeSerializer; - -import java.lang.reflect.Type; - -public class Location { - - private final double x; - private final double y; - private final double z; - private final float yaw; - private final float pitch; - - Location(double x, double y, double z, float yaw, float pitch) { - this.x = x; - this.y = y; - this.z = z; - this.yaw = yaw; - this.pitch = pitch; - } - - Location(double x, double y, double z) { - this(x, y, z, 0.0F, 0.0F); - } - - public double getX() { - return x; - } - - public int getBlockX() { - return (int) x; - } - - public double getY() { - return y; - } - - public int getBlockY() { - return (int) y; - } - - public double getZ() { - return z; - } - - public int getBlockZ() { - return (int) z; - } - - public float getYaw() { - return yaw; - } - - public float getPitch() { - return pitch; - } - - public static Location of(double x, double y, double z) { - return new Location(x, y, z); - } - - public static Location of(double x, double y, double z, float yaw, float pitch) { - return new Location(x, y, z, yaw, pitch); - } - - public static Location pos(int x, int y, int z) { - return new Location(x, y, z); - } - - public static class Serializer implements TypeSerializer { - - @Override - public Location deserialize(Type type, ConfigurationNode node) { - double x = node.node("x").getDouble(0); - double y = node.node("y").getDouble(0); - double z = node.node("z").getDouble(0); - float yaw = node.node("yaw").getFloat(0.0F); - float pitch = node.node("pitch").getFloat(0.0F); - - return new Location(x, y, z, yaw, pitch); - } - - @Override - public void serialize(Type type, @Nullable Location obj, ConfigurationNode node) { } - } -} diff --git a/src/main/resources/settings.yml b/src/main/resources/settings.yml index ab465e3..531b50d 100644 --- a/src/main/resources/settings.yml +++ b/src/main/resources/settings.yml @@ -32,14 +32,6 @@ headerAndFooter: header: '{"text": "&eWelcome!"}' footer: '{"text": "&9NanoLimbo"}' -# Spawn position in the world -spawnPosition: - 'x': 0.0 - 'y': 400.0 - 'z': 0.0 - 'yaw': 0.0 - 'pitch': 0.0 - # Setup player's game mode # 0 - Survival # 1 - Creative (hide HP and food bar)