diff --git a/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java b/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java index 152aae5f3..96360503a 100644 --- a/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java +++ b/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java @@ -36,7 +36,8 @@ public enum ProtocolVersion { MINECRAFT_1_15_1(575, "1.15.1"), MINECRAFT_1_15_2(578, "1.15.2"), MINECRAFT_1_16(735, "1.16"), - MINECRAFT_1_16_1(736, "1.16.1"); + MINECRAFT_1_16_1(736, "1.16.1"), + MINECRAFT_1_16_2(751, "1.16.2"); private final int protocol; private final String name; diff --git a/proxy/build.gradle b/proxy/build.gradle index 91a434a11..ceda94504 100644 --- a/proxy/build.gradle +++ b/proxy/build.gradle @@ -63,7 +63,7 @@ dependencies { compile 'it.unimi.dsi:fastutil:8.2.2' compile 'net.kyori:event-method-asm:3.0.0' - compile 'net.kyori:nbt:1.12-1.0.0-SNAPSHOT' + compile 'net.kyori:adventure-nbt:4.0.0-SNAPSHOT' compile 'com.mojang:brigadier:1.0.17' diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index f583d76a5..cbcb95366 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -341,15 +341,16 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { int tempDim = joinGame.getDimension() == 0 ? -1 : 0; player.getMinecraftConnection().delayedWrite( new Respawn(tempDim, joinGame.getPartialHashedSeed(), joinGame.getDifficulty(), - joinGame.getGamemode(), joinGame.getLevelType(), - false, joinGame.getDimensionInfo(), joinGame.getPreviousGamemode())); + joinGame.getGamemode(), joinGame.getLevelType(), + false, joinGame.getDimensionInfo(), joinGame.getPreviousGamemode(), + joinGame.getCurrentDimensionData())); } player.getMinecraftConnection().delayedWrite( new Respawn(joinGame.getDimension(), joinGame.getPartialHashedSeed(), joinGame.getDifficulty(), joinGame.getGamemode(), joinGame.getLevelType(), - false, joinGame.getDimensionInfo(), joinGame.getPreviousGamemode())); - + false, joinGame.getDimensionInfo(), joinGame.getPreviousGamemode(), + joinGame.getCurrentDimensionData())); destination.setActiveDimensionRegistry(joinGame.getDimensionRegistry()); // 1.16 } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java index b5c7193c4..d55463995 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java @@ -1,11 +1,15 @@ package com.velocitypowered.proxy.connection.registry; import com.google.common.base.Preconditions; -import net.kyori.nbt.CompoundTag; +import com.velocitypowered.api.network.ProtocolVersion; +import net.kyori.adventure.nbt.CompoundBinaryTag; import org.checkerframework.checker.nullness.qual.Nullable; public final class DimensionData { + private static final String UNKNOWN_DIMENSION_ID = "velocity:unknown_dimension"; + private final String registryIdentifier; + private final @Nullable Integer dimensionId; private final boolean isNatural; private final float ambientLight; private final boolean isShrunk; @@ -20,10 +24,13 @@ public final class DimensionData { private final String burningBehaviourIdentifier; private final @Nullable Long fixedTime; private final @Nullable Boolean createDragonFight; + private final @Nullable Double coordinateScale; + private final @Nullable String effects; /** * Initializes a new {@link DimensionData} instance. * @param registryIdentifier the identifier for the dimension from the registry. + * @param dimensionId the dimension ID contained in the registry (the "id" tag) * @param isNatural indicates if the dimension use natural world generation (e.g. overworld) * @param ambientLight the light level the client sees without external lighting * @param isShrunk indicates if the world is shrunk, aka not the full 256 blocks (e.g. nether) @@ -38,24 +45,31 @@ public final class DimensionData { * @param burningBehaviourIdentifier the identifier for how burning blocks work in the dimension * @param fixedTime optional. If set to any game daytime value will deactivate time cycle * @param createDragonFight optional. Internal flag used in the end dimension + * @param coordinateScale optional, unknown purpose + * @param effects optional, unknown purpose */ - public DimensionData(String registryIdentifier, boolean isNatural, - float ambientLight, boolean isShrunk, boolean isUltrawarm, - boolean hasCeiling, boolean hasSkylight, - boolean isPiglinSafe, boolean doBedsWork, - boolean doRespawnAnchorsWork, boolean hasRaids, - int logicalHeight, String burningBehaviourIdentifier, - @Nullable Long fixedTime, @Nullable Boolean createDragonFight) { + public DimensionData(String registryIdentifier, + @Nullable Integer dimensionId, + boolean isNatural, + float ambientLight, boolean isShrunk, boolean isUltrawarm, + boolean hasCeiling, boolean hasSkylight, + boolean isPiglinSafe, boolean doBedsWork, + boolean doRespawnAnchorsWork, boolean hasRaids, + int logicalHeight, String burningBehaviourIdentifier, + @Nullable Long fixedTime, @Nullable Boolean createDragonFight, + @Nullable Double coordinateScale, + @Nullable String effects) { Preconditions.checkNotNull( - registryIdentifier, "registryIdentifier cannot be null"); + registryIdentifier, "registryIdentifier cannot be null"); Preconditions.checkArgument(registryIdentifier.length() > 0, - "registryIdentifier cannot be empty"); + "registryIdentifier cannot be empty"); Preconditions.checkArgument(logicalHeight >= 0, "localHeight must be >= 0"); Preconditions.checkNotNull( - burningBehaviourIdentifier, "burningBehaviourIdentifier cannot be null"); + burningBehaviourIdentifier, "burningBehaviourIdentifier cannot be null"); Preconditions.checkArgument(burningBehaviourIdentifier.length() > 0, "burningBehaviourIdentifier cannot be empty"); this.registryIdentifier = registryIdentifier; + this.dimensionId = dimensionId; this.isNatural = isNatural; this.ambientLight = ambientLight; this.isShrunk = isShrunk; @@ -70,12 +84,18 @@ public final class DimensionData { this.burningBehaviourIdentifier = burningBehaviourIdentifier; this.fixedTime = fixedTime; this.createDragonFight = createDragonFight; + this.coordinateScale = coordinateScale; + this.effects = effects; } public String getRegistryIdentifier() { return registryIdentifier; } + public @Nullable Integer getDimensionId() { + return dimensionId; + } + public boolean isNatural() { return isNatural; } @@ -132,61 +152,143 @@ public final class DimensionData { return createDragonFight; } + public @Nullable Double getCoordinateScale() { + return coordinateScale; + } + /** - * Parses a given CompoundTag to a DimensionData instance. - * @param toRead the compound from the registry to read + * Returns a fresh {@link DimensionData} with the specified {@code registryIdentifier} + * and {@code dimensionId}. + * + * @param registryIdentifier the identifier for the dimension from the registry + * @param dimensionId optional, dimension ID + * @return a new {@link DimensionData} + */ + public DimensionData annotateWith(String registryIdentifier, + @Nullable Integer dimensionId) { + return new DimensionData(registryIdentifier, dimensionId, isNatural, ambientLight, isShrunk, + isUltrawarm, hasCeiling, hasSkylight, isPiglinSafe, doBedsWork, doRespawnAnchorsWork, + hasRaids, logicalHeight, burningBehaviourIdentifier, fixedTime, createDragonFight, + coordinateScale, effects); + } + + public boolean isUnannotated() { + return this.registryIdentifier.equalsIgnoreCase(UNKNOWN_DIMENSION_ID); + } + + /** + * Parses a given CompoundTag to a DimensionData instance. Assumes the data only contains + * dimension details. + * + * @param details the compound from the registry to read + * @param version the protocol version * @return game dimension data */ - public static DimensionData decodeCompoundTag(CompoundTag toRead) { - Preconditions.checkNotNull(toRead, "CompoundTag cannot be null"); - String registryIdentifier = toRead.getString("name"); - boolean isNatural = toRead.getBoolean("natural"); - float ambientLight = toRead.getFloat("ambient_light"); - boolean isShrunk = toRead.getBoolean("shrunk"); - boolean isUltrawarm = toRead.getBoolean("ultrawarm"); - boolean hasCeiling = toRead.getBoolean("has_ceiling"); - boolean hasSkylight = toRead.getBoolean("has_skylight"); - boolean isPiglinSafe = toRead.getBoolean("piglin_safe"); - boolean doBedsWork = toRead.getBoolean("bed_works"); - boolean doRespawnAnchorsWork = toRead.getBoolean("respawn_anchor_works"); - boolean hasRaids = toRead.getBoolean("has_raids"); - int logicalHeight = toRead.getInt("logical_height"); - String burningBehaviourIdentifier = toRead.getString("infiniburn"); - Long fixedTime = toRead.contains("fixed_time") - ? toRead.getLong("fixed_time") : null; - Boolean hasEnderdragonFight = toRead.contains("has_enderdragon_fight") - ? toRead.getBoolean("has_enderdragon_fight") : null; + public static DimensionData decodeBaseCompoundTag(CompoundBinaryTag details, + ProtocolVersion version) { + boolean isNatural = details.getByte("natural") >= 1; + float ambientLight = details.getFloat("ambient_light"); + boolean isShrunk = details.getByte("shrunk") >= 1; + boolean isUltrawarm = details.getByte("ultrawarm") >= 1; + boolean hasCeiling = details.getByte("has_ceiling") >= 1; + boolean hasSkylight = details.getByte("has_skylight") >= 1; + boolean isPiglinSafe = details.getByte("piglin_safe") >= 1; + boolean doBedsWork = details.getByte("bed_works") >= 1; + boolean doRespawnAnchorsWork = details.getByte("respawn_anchor_works") >= 1; + boolean hasRaids = details.getByte("has_raids") >= 1; + int logicalHeight = details.getInt("logical_height"); + String burningBehaviourIdentifier = details.getString("infiniburn"); + Long fixedTime = details.keySet().contains("fixed_time") + ? details.getLong("fixed_time") : null; + Boolean hasEnderdragonFight = details.keySet().contains("has_enderdragon_fight") + ? details.getByte("has_enderdragon_fight") >= 1 : null; + Double coordinateScale = details.keySet().contains("coordinate_scale") + ? details.getDouble("coordinate_scale") : null; + String effects = details.keySet().contains("effects") ? details.getString("effects") + : null; return new DimensionData( - registryIdentifier, isNatural, ambientLight, isShrunk, - isUltrawarm, hasCeiling, hasSkylight, isPiglinSafe, doBedsWork, doRespawnAnchorsWork, - hasRaids, logicalHeight, burningBehaviourIdentifier, fixedTime, hasEnderdragonFight); + UNKNOWN_DIMENSION_ID, null, isNatural, ambientLight, isShrunk, + isUltrawarm, hasCeiling, hasSkylight, isPiglinSafe, doBedsWork, doRespawnAnchorsWork, + hasRaids, logicalHeight, burningBehaviourIdentifier, fixedTime, hasEnderdragonFight, + coordinateScale, effects); + } + + /** + * Parses a given CompoundTag to a DimensionData instance. Assumes the data is part of a + * dimension registry. + * @param dimTag the compound from the registry to read + * @param version the protocol version + * @return game dimension data + */ + public static DimensionData decodeRegistryEntry(CompoundBinaryTag dimTag, + ProtocolVersion version) { + String registryIdentifier = dimTag.getString("name"); + CompoundBinaryTag details; + Integer dimensionId = null; + if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + dimensionId = dimTag.getInt("id"); + details = dimTag.getCompound("element"); + } else { + details = dimTag; + } + + DimensionData deserializedDetails = decodeBaseCompoundTag(details, version); + return deserializedDetails.annotateWith(registryIdentifier, dimensionId); } /** * Encodes the Dimension data as CompoundTag. + * @param version the version to serialize as * @return compound containing the dimension data */ - public CompoundTag encodeAsCompundTag() { - CompoundTag ret = new CompoundTag(); - ret.putString("name", registryIdentifier); - ret.putBoolean("natural", isNatural); + public CompoundBinaryTag encodeAsCompoundTag(ProtocolVersion version) { + CompoundBinaryTag details = serializeDimensionDetails(); + if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + if (dimensionId == null) { + throw new IllegalStateException("Tried to serialize a 1.16.2+ dimension registry entry " + + "without an ID"); + } + + return CompoundBinaryTag.builder() + .putString("name", registryIdentifier) + .putInt("id", dimensionId) + .put("element", details) + .build(); + } else { + return details.putString("name", registryIdentifier); + } + } + + /** + * Serializes details of this dimension. + * @return serialized details of this dimension + */ + public CompoundBinaryTag serializeDimensionDetails() { + CompoundBinaryTag.Builder ret = CompoundBinaryTag.builder(); + ret.putByte("natural", (byte) (isNatural ? 1 : 0)); ret.putFloat("ambient_light", ambientLight); - ret.putBoolean("shrunk", isShrunk); - ret.putBoolean("ultrawarm", isUltrawarm); - ret.putBoolean("has_ceiling", hasCeiling); - ret.putBoolean("has_skylight", hasSkylight); - ret.putBoolean("piglin_safe", isPiglinSafe); - ret.putBoolean("bed_works", doBedsWork); - ret.putBoolean("respawn_anchor_works", doRespawnAnchorsWork); - ret.putBoolean("has_raids", hasRaids); + ret.putByte("shrunk", (byte) (isShrunk ? 1 : 0)); + ret.putByte("ultrawarm", (byte) (isUltrawarm ? 1 : 0)); + ret.putByte("has_ceiling", (byte) (hasCeiling ? 1 : 0)); + ret.putByte("has_skylight", (byte) (hasSkylight ? 1 : 0)); + ret.putByte("piglin_safe", (byte) (isPiglinSafe ? 1 : 0)); + ret.putByte("bed_works", (byte) (doBedsWork ? 1 : 0)); + ret.putByte("respawn_anchor_works", (byte) (doBedsWork ? 1 : 0)); + ret.putByte("has_raids", (byte) (hasRaids ? 1 : 0)); ret.putInt("logical_height", logicalHeight); ret.putString("infiniburn", burningBehaviourIdentifier); if (fixedTime != null) { ret.putLong("fixed_time", fixedTime); } if (createDragonFight != null) { - ret.putBoolean("has_enderdragon_fight", createDragonFight); + ret.putByte("has_enderdragon_fight", (byte) (createDragonFight ? 1 : 0)); } - return ret; + if (coordinateScale != null) { + ret.putDouble("coordinate_scale", coordinateScale); + } + if (effects != null) { + ret.putString("effects", effects); + } + return ret.build(); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java index ac5670194..5b7cba13f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java @@ -1,6 +1,7 @@ package com.velocitypowered.proxy.connection.registry; import com.google.common.base.Preconditions; +import org.checkerframework.checker.nullness.qual.Nullable; public final class DimensionInfo { @@ -16,18 +17,13 @@ public final class DimensionInfo { * @param isFlat if true will set world lighting below surface-level to not display fog * @param isDebugType if true constrains the world to the very limited debug-type world */ - public DimensionInfo(String registryIdentifier, String levelName, + public DimensionInfo(String registryIdentifier, @Nullable String levelName, boolean isFlat, boolean isDebugType) { this.registryIdentifier = Preconditions.checkNotNull( - registryIdentifier, "registryIdentifier cannot be null"); - Preconditions.checkArgument( - registryIdentifier.length() > 0, - "registryIdentifier cannot be empty"); - this.levelName = Preconditions.checkNotNull( - levelName, "levelName cannot be null"); - Preconditions.checkArgument( - levelName.length() > 0, + registryIdentifier, "registryIdentifier cannot be null"); + Preconditions.checkArgument(registryIdentifier.length() > 0, "registryIdentifier cannot be empty"); + this.levelName = levelName; this.isFlat = isFlat; this.isDebugType = isDebugType; } @@ -40,7 +36,7 @@ public final class DimensionInfo { return isFlat; } - public String getLevelName() { + public @Nullable String getLevelName() { return levelName; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java index 22bf61977..bdc9ae1ca 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java @@ -4,16 +4,16 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; +import com.velocitypowered.api.network.ProtocolVersion; import java.util.Map; import java.util.Set; -import net.kyori.nbt.CompoundTag; -import net.kyori.nbt.ListTag; -import net.kyori.nbt.Tag; -import net.kyori.nbt.TagType; +import net.kyori.adventure.nbt.BinaryTag; +import net.kyori.adventure.nbt.BinaryTagTypes; +import net.kyori.adventure.nbt.CompoundBinaryTag; +import net.kyori.adventure.nbt.ListBinaryTag; import org.checkerframework.checker.nullness.qual.Nullable; - public final class DimensionRegistry { private final Map registeredDimensions; @@ -79,29 +79,26 @@ public final class DimensionRegistry { * Encodes the stored Dimension registry as CompoundTag. * @return the CompoundTag containing identifier:type mappings */ - public CompoundTag encodeRegistry() { - CompoundTag ret = new CompoundTag(); - ListTag list = new ListTag(TagType.COMPOUND); + public ListBinaryTag encodeRegistry(ProtocolVersion version) { + ListBinaryTag.Builder listBuilder = ListBinaryTag + .builder(BinaryTagTypes.COMPOUND); for (DimensionData iter : registeredDimensions.values()) { - list.add(iter.encodeAsCompundTag()); + listBuilder.add(iter.encodeAsCompoundTag(version)); } - ret.put("dimension", list); - return ret; + return listBuilder.build(); } /** * Decodes a CompoundTag storing a dimension registry. * @param toParse CompoundTag containing a dimension registry */ - public static ImmutableSet fromGameData(CompoundTag toParse) { - Preconditions.checkNotNull(toParse, "CompoundTag cannot be null"); - Preconditions.checkArgument(toParse.contains("dimension", TagType.LIST), - "CompoundTag does not contain a dimension list"); - ListTag dimensions = toParse.getList("dimension"); + public static ImmutableSet fromGameData(ListBinaryTag toParse, + ProtocolVersion version) { + Preconditions.checkNotNull(toParse, "ListTag cannot be null"); ImmutableSet.Builder mappings = ImmutableSet.builder(); - for (Tag iter : dimensions) { - if (iter instanceof CompoundTag) { - mappings.add(DimensionData.decodeCompoundTag((CompoundTag) iter)); + for (BinaryTag iter : toParse) { + if (iter instanceof CompoundBinaryTag) { + mappings.add(DimensionData.decodeRegistryEntry((CompoundBinaryTag) iter, version)); } } return mappings.build(); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java index bb7516aa3..7b9764a71 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -12,17 +12,14 @@ import io.netty.buffer.ByteBufUtil; import io.netty.handler.codec.DecoderException; import io.netty.handler.codec.EncoderException; -import java.io.DataInput; -import java.io.DataOutput; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.UUID; -import net.kyori.nbt.CompoundTag; -import net.kyori.nbt.TagIO; -import net.kyori.nbt.TagType; +import net.kyori.adventure.nbt.BinaryTagIO; +import net.kyori.adventure.nbt.CompoundBinaryTag; public enum ProtocolUtils { ; @@ -192,11 +189,11 @@ public enum ProtocolUtils { } /** - * Reads a {@link net.kyori.nbt.CompoundTag} from the {@code buf}. + * Reads a {@link net.kyori.adventure.nbt.CompoundBinaryTag} from the {@code buf}. * @param buf the buffer to read from - * @return {@link net.kyori.nbt.CompoundTag} the CompoundTag from the buffer + * @return {@link net.kyori.adventure.nbt.CompoundBinaryTag} the CompoundTag from the buffer */ - public static CompoundTag readCompoundTag(ByteBuf buf) { + public static CompoundBinaryTag readCompoundTag(ByteBuf buf) { int indexBefore = buf.readerIndex(); byte startType = buf.readByte(); if (startType == 0) { @@ -204,7 +201,7 @@ public enum ProtocolUtils { } buf.readerIndex(indexBefore); try { - return TagIO.readDataInput(new ByteBufInputStream(buf)); + return BinaryTagIO.readDataInput(new ByteBufInputStream(buf)); } catch (IOException thrown) { throw new DecoderException( "Unable to parse NBT CompoundTag, full error: " + thrown.getMessage()); @@ -216,13 +213,13 @@ public enum ProtocolUtils { * @param buf the buffer to write to * @param compoundTag the CompoundTag to write */ - public static void writeCompoundTag(ByteBuf buf, CompoundTag compoundTag) { + public static void writeCompoundTag(ByteBuf buf, CompoundBinaryTag compoundTag) { if (compoundTag == null) { buf.writeByte(0); return; } try { - TagIO.writeDataOutput(compoundTag, new ByteBufOutputStream(buf)); + BinaryTagIO.writeDataOutput(compoundTag, new ByteBufOutputStream(buf)); } catch (IOException e) { throw new EncoderException("Unable to encode NBT CompoundTag"); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java index 658c818d7..f2a969dbf 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -7,6 +7,7 @@ import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_13; import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_14; import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_15; import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_16; +import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_16_2; import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_8; import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_9; import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_9_4; @@ -122,7 +123,8 @@ public enum StateRegistry { map(0x18, MINECRAFT_1_12, false), map(0x1D, MINECRAFT_1_13, false), map(0x1F, MINECRAFT_1_14, false), - map(0x20, MINECRAFT_1_16, false)); + map(0x20, MINECRAFT_1_16, false), + map(0x21, MINECRAFT_1_16_2, false)); clientbound.register(BossBar.class, BossBar::new, map(0x0C, MINECRAFT_1_9, false), @@ -139,39 +141,45 @@ public enum StateRegistry { map(0x0E, MINECRAFT_1_9, false), map(0x10, MINECRAFT_1_13, false), map(0x11, MINECRAFT_1_15, false), - map(0x10, MINECRAFT_1_16, false)); + map(0x10, MINECRAFT_1_16, false), + map(0x0F, MINECRAFT_1_16_2, false)); clientbound.register(AvailableCommands.class, AvailableCommands::new, map(0x11, MINECRAFT_1_13, false), map(0x12, MINECRAFT_1_15, false), - map(0x11, MINECRAFT_1_16, false)); + map(0x11, MINECRAFT_1_16, false), + map(0x10, MINECRAFT_1_16_2, false)); clientbound.register(PluginMessage.class, PluginMessage::new, map(0x3F, MINECRAFT_1_8, false), map(0x18, MINECRAFT_1_9, false), map(0x19, MINECRAFT_1_13, false), map(0x18, MINECRAFT_1_14, false), map(0x19, MINECRAFT_1_15, false), - map(0x18, MINECRAFT_1_16, false)); + map(0x18, MINECRAFT_1_16, false), + map(0x17, MINECRAFT_1_16_2, false)); clientbound.register(Disconnect.class, Disconnect::new, map(0x40, MINECRAFT_1_8, false), map(0x1A, MINECRAFT_1_9, false), map(0x1B, MINECRAFT_1_13, false), map(0x1A, MINECRAFT_1_14, false), map(0x1B, MINECRAFT_1_15, false), - map(0x1A, MINECRAFT_1_16, false)); + map(0x1A, MINECRAFT_1_16, false), + map(0x19, MINECRAFT_1_16_2, false)); clientbound.register(KeepAlive.class, KeepAlive::new, map(0x00, MINECRAFT_1_8, false), map(0x1F, MINECRAFT_1_9, false), map(0x21, MINECRAFT_1_13, false), map(0x20, MINECRAFT_1_14, false), map(0x21, MINECRAFT_1_15, false), - map(0x20, MINECRAFT_1_16, false)); + map(0x20, MINECRAFT_1_16, false), + map(0x1F, MINECRAFT_1_16_2, false)); clientbound.register(JoinGame.class, JoinGame::new, map(0x01, MINECRAFT_1_8, false), map(0x23, MINECRAFT_1_9, false), map(0x25, MINECRAFT_1_13, false), map(0x25, MINECRAFT_1_14, false), map(0x26, MINECRAFT_1_15, false), - map(0x25, MINECRAFT_1_16, false)); + map(0x25, MINECRAFT_1_16, false), + map(0x24, MINECRAFT_1_16_2, false)); clientbound.register(Respawn.class, Respawn::new, map(0x07, MINECRAFT_1_8, true), map(0x33, MINECRAFT_1_9, true), @@ -180,7 +188,8 @@ public enum StateRegistry { map(0x38, MINECRAFT_1_13, true), map(0x3A, MINECRAFT_1_14, true), map(0x3B, MINECRAFT_1_15, true), - map(0x3A, MINECRAFT_1_16, true)); + map(0x3A, MINECRAFT_1_16, true), + map(0x39, MINECRAFT_1_16_2, true)); clientbound.register(ResourcePackRequest.class, ResourcePackRequest::new, map(0x48, MINECRAFT_1_8, true), map(0x32, MINECRAFT_1_9, true), @@ -189,7 +198,8 @@ public enum StateRegistry { map(0x37, MINECRAFT_1_13, true), map(0x39, MINECRAFT_1_14, true), map(0x3A, MINECRAFT_1_15, true), - map(0x39, MINECRAFT_1_16, true)); + map(0x39, MINECRAFT_1_16, true), + map(0x38, MINECRAFT_1_16_2, true)); clientbound.register(HeaderAndFooter.class, HeaderAndFooter::new, map(0x47, MINECRAFT_1_8, true), map(0x48, MINECRAFT_1_9, true), @@ -216,7 +226,8 @@ public enum StateRegistry { map(0x30, MINECRAFT_1_13, false), map(0x33, MINECRAFT_1_14, false), map(0x34, MINECRAFT_1_15, false), - map(0x33, MINECRAFT_1_16, false)); + map(0x33, MINECRAFT_1_16, false), + map(0x32, MINECRAFT_1_16_2, false)); } }, LOGIN { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/JoinGame.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/JoinGame.java index 630a60de2..b2e1353e4 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/JoinGame.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/JoinGame.java @@ -6,8 +6,12 @@ import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.connection.registry.DimensionData; import com.velocitypowered.proxy.connection.registry.DimensionInfo; import com.velocitypowered.proxy.connection.registry.DimensionRegistry; -import com.velocitypowered.proxy.protocol.*; +import com.velocitypowered.proxy.protocol.MinecraftPacket; +import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; +import net.kyori.adventure.nbt.BinaryTagTypes; +import net.kyori.adventure.nbt.CompoundBinaryTag; +import net.kyori.adventure.nbt.ListBinaryTag; import org.checkerframework.checker.nullness.qual.Nullable; public class JoinGame implements MinecraftPacket { @@ -17,14 +21,17 @@ public class JoinGame implements MinecraftPacket { private int dimension; private long partialHashedSeed; // 1.15+ private short difficulty; - private short maxPlayers; + private boolean isHardcore; + private int maxPlayers; private @Nullable String levelType; private int viewDistance; // 1.14+ private boolean reducedDebugInfo; private boolean showRespawnScreen; private DimensionRegistry dimensionRegistry; // 1.16+ private DimensionInfo dimensionInfo; // 1.16+ + private DimensionData currentDimensionData; // 1.16.2+ private short previousGamemode; // 1.16+ + private CompoundBinaryTag biomeRegistry; // 1.16.2+ public int getEntityId() { return entityId; @@ -62,11 +69,11 @@ public class JoinGame implements MinecraftPacket { this.difficulty = difficulty; } - public short getMaxPlayers() { + public int getMaxPlayers() { return maxPlayers; } - public void setMaxPlayers(short maxPlayers) { + public void setMaxPlayers(int maxPlayers) { this.maxPlayers = maxPlayers; } @@ -118,6 +125,26 @@ public class JoinGame implements MinecraftPacket { this.previousGamemode = previousGamemode; } + public boolean getIsHardcore() { + return isHardcore; + } + + public void setIsHardcore(boolean isHardcore) { + this.isHardcore = isHardcore; + } + + public CompoundBinaryTag getBiomeRegistry() { + return biomeRegistry; + } + + public void setBiomeRegistry(CompoundBinaryTag biomeRegistry) { + this.biomeRegistry = biomeRegistry; + } + + public DimensionData getCurrentDimensionData() { + return currentDimensionData; + } + @Override public String toString() { return "JoinGame{" @@ -126,6 +153,7 @@ public class JoinGame implements MinecraftPacket { + ", dimension=" + dimension + ", partialHashedSeed=" + partialHashedSeed + ", difficulty=" + difficulty + + ", isHardcore=" + isHardcore + ", maxPlayers=" + maxPlayers + ", levelType='" + levelType + '\'' + ", viewDistance=" + viewDistance @@ -139,16 +167,41 @@ public class JoinGame implements MinecraftPacket { @Override public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { this.entityId = buf.readInt(); - this.gamemode = buf.readByte(); + if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + this.isHardcore = buf.readBoolean(); + this.gamemode = buf.readByte(); + } else { + this.gamemode = buf.readByte(); + this.isHardcore = (this.gamemode & 0x08) != 0; + this.gamemode &= ~0x08; + } String dimensionIdentifier = null; String levelName = null; if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { this.previousGamemode = buf.readByte(); ImmutableSet levelNames = ImmutableSet.copyOf(ProtocolUtils.readStringArray(buf)); - ImmutableSet readData = DimensionRegistry.fromGameData(ProtocolUtils.readCompoundTag(buf)); + CompoundBinaryTag registryContainer = ProtocolUtils.readCompoundTag(buf); + ListBinaryTag dimensionRegistryContainer = null; + if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + dimensionRegistryContainer = registryContainer.getCompound("minecraft:dimension_type") + .getList("value", BinaryTagTypes.COMPOUND); + this.biomeRegistry = registryContainer.getCompound("minecraft:worldgen/biome"); + } else { + dimensionRegistryContainer = registryContainer.getList("dimension", + BinaryTagTypes.COMPOUND); + } + ImmutableSet readData = + DimensionRegistry.fromGameData(dimensionRegistryContainer, version); this.dimensionRegistry = new DimensionRegistry(readData, levelNames); - dimensionIdentifier = ProtocolUtils.readString(buf); - levelName = ProtocolUtils.readString(buf); + if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + CompoundBinaryTag currentDimDataTag = ProtocolUtils.readCompoundTag(buf); + dimensionIdentifier = ProtocolUtils.readString(buf); + this.currentDimensionData = DimensionData.decodeBaseCompoundTag(currentDimDataTag, version) + .annotateWith(dimensionIdentifier, null); + } else { + dimensionIdentifier = ProtocolUtils.readString(buf); + levelName = ProtocolUtils.readString(buf); + } } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) { this.dimension = buf.readInt(); } else { @@ -160,7 +213,11 @@ public class JoinGame implements MinecraftPacket { if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { this.partialHashedSeed = buf.readLong(); } - this.maxPlayers = buf.readUnsignedByte(); + if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + this.maxPlayers = ProtocolUtils.readVarInt(buf); + } else { + this.maxPlayers = buf.readUnsignedByte(); + } if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) < 0) { this.levelType = ProtocolUtils.readString(buf, 16); } @@ -181,14 +238,35 @@ public class JoinGame implements MinecraftPacket { @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { buf.writeInt(entityId); - buf.writeByte(gamemode); + if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + buf.writeBoolean(isHardcore); + buf.writeByte(gamemode); + } else { + buf.writeByte(isHardcore ? gamemode | 0x8 : gamemode); + } if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { buf.writeByte(previousGamemode); ProtocolUtils.writeStringArray(buf, dimensionRegistry.getLevelNames().toArray( new String[dimensionRegistry.getLevelNames().size()])); - ProtocolUtils.writeCompoundTag(buf, dimensionRegistry.encodeRegistry()); - ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier()); - ProtocolUtils.writeString(buf, dimensionInfo.getLevelName()); + CompoundBinaryTag.Builder registryContainer = CompoundBinaryTag.builder(); + ListBinaryTag encodedDimensionRegistry = dimensionRegistry.encodeRegistry(version); + if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + CompoundBinaryTag.Builder dimensionRegistryDummy = CompoundBinaryTag.builder(); + dimensionRegistryDummy.putString("type", "minecraft:dimension_type"); + dimensionRegistryDummy.put("value", encodedDimensionRegistry); + registryContainer.put("minecraft:dimension_type", dimensionRegistryDummy.build()); + registryContainer.put("minecraft:worldgen/biome", biomeRegistry); + } else { + registryContainer.put("dimension", encodedDimensionRegistry); + } + ProtocolUtils.writeCompoundTag(buf, registryContainer.build()); + if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + ProtocolUtils.writeCompoundTag(buf, currentDimensionData.serializeDimensionDetails()); + ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier()); + } else { + ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier()); + ProtocolUtils.writeString(buf, dimensionInfo.getLevelName()); + } } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) { buf.writeInt(dimension); } else { @@ -200,7 +278,11 @@ public class JoinGame implements MinecraftPacket { if (version.compareTo(ProtocolVersion.MINECRAFT_1_15) >= 0) { buf.writeLong(partialHashedSeed); } - buf.writeByte(maxPlayers); + if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + ProtocolUtils.writeVarInt(buf, maxPlayers); + } else { + buf.writeByte(maxPlayers); + } if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) < 0) { if (levelType == null) { throw new IllegalStateException("No level type specified."); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Respawn.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Respawn.java index d14e9f8c8..3a4fdb4af 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Respawn.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Respawn.java @@ -2,10 +2,12 @@ package com.velocitypowered.proxy.protocol.packet; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; +import com.velocitypowered.proxy.connection.registry.DimensionData; import com.velocitypowered.proxy.connection.registry.DimensionInfo; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; +import net.kyori.adventure.nbt.CompoundBinaryTag; public class Respawn implements MinecraftPacket { @@ -15,15 +17,16 @@ public class Respawn implements MinecraftPacket { private short gamemode; private String levelType = ""; private boolean shouldKeepPlayerData; // 1.16+ - private DimensionInfo dimensionInfo; // 1.16+ + private DimensionInfo dimensionInfo; // 1.16-1.16.1 private short previousGamemode; // 1.16+ + private DimensionData currentDimensionData; // 1.16.2+ public Respawn() { } public Respawn(int dimension, long partialHashedSeed, short difficulty, short gamemode, String levelType, boolean shouldKeepPlayerData, DimensionInfo dimensionInfo, - short previousGamemode) { + short previousGamemode, DimensionData currentDimensionData) { this.dimension = dimension; this.partialHashedSeed = partialHashedSeed; this.difficulty = difficulty; @@ -32,6 +35,7 @@ public class Respawn implements MinecraftPacket { this.shouldKeepPlayerData = shouldKeepPlayerData; this.dimensionInfo = dimensionInfo; this.previousGamemode = previousGamemode; + this.currentDimensionData = currentDimensionData; } public int getDimension() { @@ -99,8 +103,9 @@ public class Respawn implements MinecraftPacket { + ", gamemode=" + gamemode + ", levelType='" + levelType + '\'' + ", shouldKeepPlayerData=" + shouldKeepPlayerData - + ", dimensionRegistryName='" + dimensionInfo.toString() + '\'' + + ", dimensionInfo=" + dimensionInfo + ", previousGamemode=" + previousGamemode + + ", dimensionData=" + currentDimensionData + '}'; } @@ -109,8 +114,15 @@ public class Respawn implements MinecraftPacket { String dimensionIdentifier = null; String levelName = null; if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - dimensionIdentifier = ProtocolUtils.readString(buf); - levelName = ProtocolUtils.readString(buf); + if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + CompoundBinaryTag dimDataTag = ProtocolUtils.readCompoundTag(buf); + dimensionIdentifier = ProtocolUtils.readString(buf); + this.currentDimensionData = DimensionData.decodeBaseCompoundTag(dimDataTag, version) + .annotateWith(dimensionIdentifier, null); + } else { + dimensionIdentifier = ProtocolUtils.readString(buf); + levelName = ProtocolUtils.readString(buf); + } } else { this.dimension = buf.readInt(); } @@ -135,8 +147,13 @@ public class Respawn implements MinecraftPacket { @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier()); - ProtocolUtils.writeString(buf, dimensionInfo.getLevelName()); + if (version.compareTo(ProtocolVersion.MINECRAFT_1_16_2) >= 0) { + ProtocolUtils.writeCompoundTag(buf, currentDimensionData.serializeDimensionDetails()); + ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier()); + } else { + ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier()); + ProtocolUtils.writeString(buf, dimensionInfo.getLevelName()); + } } else { buf.writeInt(dimension); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentPropertyRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentPropertyRegistry.java index 7c9b8fb9c..4baa0a3cd 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentPropertyRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/brigadier/ArgumentPropertyRegistry.java @@ -135,5 +135,6 @@ public class ArgumentPropertyRegistry { dummy("minecraft:float_range", DUMMY); dummy("minecraft:time", DUMMY); // added in 1.14 dummy("minecraft:uuid", DUMMY); // added in 1.16 + dummy("minecraft:angle", DUMMY); // added in 1.16.2 } }