diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionData.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionData.java index 9ecfebe15..ed6867e03 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionData.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionData.java @@ -1,9 +1,8 @@ package com.velocitypowered.proxy.protocol; -import net.kyori.nbt.CompoundTag; - import javax.annotation.Nonnull; import javax.annotation.Nullable; +import net.kyori.nbt.CompoundTag; public class DimensionData { private final @Nonnull String registryIdentifier; @@ -16,6 +15,18 @@ public class DimensionData { private final @Nullable Long fixedTime; private final @Nullable Boolean hasEnderdragonFight; + /** + * Initializes a new {@link DimensionData} instance. + * @param registryIdentifier the identifier for the dimension from the registry. + * @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) + * @param isUltrawarm internal dimension warmth flag + * @param hasCeiling indicates if the dimension has a ceiling layer + * @param hasSkylight indicates if the dimension should display the sun + * @param fixedTime optional. If set to any game daytime value will deactivate time cycle + * @param hasEnderdragonFight optional. Internal flag used in the end dimension + */ public DimensionData(@Nonnull String registryIdentifier, boolean isNatural, float ambientLight, boolean isShrunk, boolean isUltrawarm, boolean hasCeiling, boolean hasSkylight, @@ -67,8 +78,13 @@ public class DimensionData { return hasEnderdragonFight; } - public static DimensionData fromNBT(@Nonnull CompoundTag toRead) { - if (toRead == null){ + /** + * Parses a given CompoundTag to a DimensionData instance. + * @param toRead the compound from the registry to read + * @return game dimension data + */ + public static DimensionData fromCompoundTag(@Nonnull CompoundTag toRead) { + if (toRead == null) { throw new IllegalArgumentException("CompoundTag cannot be null"); } String registryIdentifier = toRead.getString("key"); @@ -80,11 +96,18 @@ public class DimensionData { boolean hasCeiling = values.getBoolean("has_ceiling"); boolean hasSkylight = values.getBoolean("has_skylight"); Long fixedTime = values.contains("fixed_time") ? values.getLong("fixed_time") : null; - Boolean hasEnderdragonFight = values.contains("has_enderdragon_fight") ? values.getBoolean("has_enderdragon_fight") : null; - return new DimensionData(registryIdentifier, isNatural, ambientLight, isShrunk, isUltrawarm, hasCeiling, hasSkylight, fixedTime, hasEnderdragonFight); + Boolean hasEnderdragonFight = values.contains("has_enderdragon_fight") + ? values.getBoolean("has_enderdragon_fight") : null; + return new DimensionData( + registryIdentifier, isNatural, ambientLight, isShrunk, + isUltrawarm, hasCeiling, hasSkylight, fixedTime, hasEnderdragonFight); } - public CompoundTag encode() { + /** + * Encodes the Dimension data as CompoundTag. + * @return compound containing the dimension data + */ + public CompoundTag encodeAsCompundTag() { CompoundTag ret = new CompoundTag(); ret.putString("key", registryIdentifier); CompoundTag values = new CompoundTag(); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java index 787c7a948..42f2980cd 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java @@ -4,28 +4,28 @@ import javax.annotation.Nonnull; public class DimensionInfo { - private final @Nonnull String dimensionIdentifier; + private final @Nonnull String registryIdentifier; private final @Nonnull String levelName; private final boolean isFlat; private final boolean isDebugType; /** * Initializes a new {@link DimensionInfo} instance. - * @param dimensionIdentifier the identifier for the dimension from the registry + * @param registryIdentifier the identifier for the dimension from the registry * @param levelName the level name as displayed in the F3 menu and logs * @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(@Nonnull String dimensionIdentifier, @Nonnull String levelName, + public DimensionInfo(@Nonnull String registryIdentifier, @Nonnull String levelName, boolean isFlat, boolean isDebugType) { - if (dimensionIdentifier == null || dimensionIdentifier.isEmpty() - || dimensionIdentifier.isBlank()) { - throw new IllegalArgumentException("DimensionRegistryName may not be empty or null"); + if (registryIdentifier == null || registryIdentifier.isEmpty() + || registryIdentifier.isBlank()) { + throw new IllegalArgumentException("Dimension registry identifier may not be empty or null"); } - this.dimensionIdentifier = dimensionIdentifier; + this.registryIdentifier = registryIdentifier; if (levelName == null || levelName.isEmpty() || levelName.isBlank()) { - throw new IllegalArgumentException("DimensionLevelName may not be empty or null"); + throw new IllegalArgumentException("dimensions level name may not be empty or null"); } this.levelName = levelName; this.isFlat = isFlat; @@ -44,7 +44,7 @@ public class DimensionInfo { return levelName; } - public @Nonnull String getDimensionIdentifier() { - return dimensionIdentifier; + public @Nonnull String getRegistryIdentifier() { + return registryIdentifier; } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java index 606f53cc7..2bf53ffc1 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java @@ -1,9 +1,10 @@ package com.velocitypowered.proxy.protocol; -import java.util.*; +import java.util.HashSet; +import java.util.NoSuchElementException; +import java.util.Set; import javax.annotation.Nonnull; -import com.google.inject.internal.asm.$TypePath; import net.kyori.nbt.CompoundTag; import net.kyori.nbt.ListTag; import net.kyori.nbt.Tag; @@ -51,7 +52,7 @@ public class DimensionRegistry { throw new IllegalArgumentException("Dimension identifier cannot be null!"); } for (DimensionData iter : dimensionRegistry) { - if(iter.getRegistryIdentifier().equals(dimensionIdentifier)) { + if (iter.getRegistryIdentifier().equals(dimensionIdentifier)) { return iter; } } @@ -69,9 +70,9 @@ public class DimensionRegistry { throw new IllegalArgumentException("Dimension info cannot be null"); } try { - getDimensionData(toValidate.getDimensionIdentifier()); - for(int i = 0; i < levelNames.length; i++) { - if(levelNames[i].equals(toValidate.getDimensionIdentifier())) { + getDimensionData(toValidate.getRegistryIdentifier()); + for (int i = 0; i < levelNames.length; i++) { + if (levelNames[i].equals(toValidate.getRegistryIdentifier())) { return true; } } @@ -89,18 +90,19 @@ public class DimensionRegistry { CompoundTag ret = new CompoundTag(); ListTag list = new ListTag(TagType.COMPOUND); for (DimensionData iter : dimensionRegistry) { - list.add(iter.encode()); + list.add(iter.encodeAsCompundTag()); } ret.put("dimension", list); return ret; } /** - * Decodes a CompoundTag storing a dimension registry + * Decodes a CompoundTag storing a dimension registry. * @param toParse CompoundTag containing a dimension registry * @param levelNames world level names */ - public static DimensionRegistry fromGameData(@Nonnull CompoundTag toParse, @Nonnull String[] levelNames) { + public static DimensionRegistry fromGameData( + @Nonnull CompoundTag toParse, @Nonnull String[] levelNames) { if (toParse == null) { throw new IllegalArgumentException("CompoundTag cannot be null"); } @@ -116,7 +118,7 @@ public class DimensionRegistry { if (!(iter instanceof CompoundTag)) { throw new IllegalStateException("DimensionList in CompoundTag contains an invalid entry"); } - mappings.add(DimensionData.fromNBT((CompoundTag) iter)); + mappings.add(DimensionData.fromCompoundTag((CompoundTag) iter)); } if (mappings.isEmpty()) { throw new IllegalStateException("Dimension mapping cannot be empty"); 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 64104ebfa..56e1d8006 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,9 +6,6 @@ import com.velocitypowered.proxy.protocol.*; import io.netty.buffer.ByteBuf; import org.checkerframework.checker.nullness.qual.Nullable; -import java.util.Map; -import java.util.Set; - public class JoinGame implements MinecraftPacket { private int entityId; @@ -177,7 +174,7 @@ public class JoinGame implements MinecraftPacket { if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { ProtocolUtils.writeStringArray(buf, dimensionRegistry.getLevelNames()); ProtocolUtils.writeCompoundTag(buf, dimensionRegistry.encodeRegistry()); - ProtocolUtils.writeString(buf, dimensionInfo.getDimensionIdentifier()); + ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier()); ProtocolUtils.writeString(buf, dimensionInfo.getLevelName()); } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) { buf.writeInt(dimension); 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 74832b5d8..e3a1a99e0 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 @@ -122,7 +122,7 @@ 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.getDimensionIdentifier()); + ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier()); ProtocolUtils.writeString(buf, dimensionInfo.getLevelName()); } else { buf.writeInt(dimension);