From 18e595397660ea6651bb4c86e1bb3a6addf1dd24 Mon Sep 17 00:00:00 2001 From: Lechner Markus Date: Thu, 4 Jun 2020 15:36:58 +0200 Subject: [PATCH 1/7] Save progress --- .../backend/VelocityServerConnection.java | 9 ++ .../client/ClientPlaySessionHandler.java | 55 +++++++-- .../proxy/protocol/DimensionInfo.java | 40 +++++++ .../proxy/protocol/DimensionRegistry.java | 113 ++++++++++++++++++ .../proxy/protocol/ProtocolUtils.java | 33 ++++- .../proxy/protocol/packet/JoinGame.java | 78 +++++------- .../proxy/protocol/packet/Respawn.java | 56 +++------ 7 files changed, 289 insertions(+), 95 deletions(-) create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java index a8b97c028..7d1d5832f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java @@ -23,6 +23,7 @@ import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation; import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.connection.util.ConnectionRequestResults.Impl; +import com.velocitypowered.proxy.protocol.DimensionRegistry; import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder; @@ -53,6 +54,7 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, private BackendConnectionPhase connectionPhase = BackendConnectionPhases.UNKNOWN; private long lastPingId; private long lastPingSent; + private @Nullable DimensionRegistry activeDimensionRegistry; /** * Initializes a new server connection. @@ -297,4 +299,11 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, return hasCompletedJoin; } + public DimensionRegistry getActiveDimensionRegistry() { + return activeDimensionRegistry; + } + + public void setActiveDimensionRegistry(DimensionRegistry activeDimensionRegistry) { + this.activeDimensionRegistry = activeDimensionRegistry; + } } 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 d8e3d3718..5a0820cd9 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 @@ -13,6 +13,8 @@ import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.connection.backend.BackendConnectionPhases; import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; +import com.velocitypowered.proxy.protocol.DimensionInfo; +import com.velocitypowered.proxy.protocol.DimensionRegistry; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.packet.BossBar; @@ -313,8 +315,8 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { if (!spawned) { // Nothing special to do with regards to spawning the player spawned = true; + destination.setActiveDimensionRegistry(joinGame.getDimensionRegistry()); // 1.16 player.getMinecraftConnection().delayedWrite(joinGame); - // Required for Legacy Forge player.getPhase().onFirstJoin(player); } else { @@ -334,20 +336,59 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { // to perform entity ID rewrites, eliminating potential issues from rewriting packets and // improving compatibility with mods. player.getMinecraftConnection().delayedWrite(joinGame); + int tempDim = joinGame.getDimension() == 0 ? -1 : 0; + // Since 1.16 this dynamic changed: + // The respawn packet has a keepMetadata flag which should + // be true for dimension switches, so by double switching + // we can keep the flow of the game + // There is a problem here though: By only sending one dimension + // in the registry we can't do that, so we need to run an *unclean* switch. + // NOTE! We can't just send a fake dimension in the registry either + // to get two dimensions, as modded games will break with this. + final DimensionRegistry dimensionRegistry = joinGame.getDimensionRegistry(); + DimensionInfo dimensionInfo = joinGame.getDimensionInfo(); // 1.16+ + // The doubleSwitch variable doubles as keepMetadata flag for an unclean switch as + // well as to indicate the second switch. + boolean doubleSwitch; + // This is not ONE if because this will all be null in < 1.16 if (player.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_16) < 0) { - int tempDim = joinGame.getDimension() == 0 ? -1 : 0; + if(dimensionRegistry.getWorldNames().size() > 1 && dimensionRegistry.getDimensionRegistry().size() > 1){ + String tmpDimLevelName = null; + for(String s : dimensionRegistry.getWorldNames()){ + if(!s.equals(dimensionInfo.getDimensionLevelName())){ + tmpDimLevelName = s; + break; + } + } + String tmpDimIdentifier = null; + for(String s : dimensionRegistry.getDimensionRegistry().keySet()){ + if(!s.equals(dimensionInfo.getDimensionIdentifier())){ + tmpDimIdentifier = s; + break; + } + } + dimensionInfo = new DimensionInfo(tmpDimIdentifier, tmpDimLevelName, true, false); + doubleSwitch = true; + } else { + doubleSwitch = false; + // We should add a warning here. + } + } else { + doubleSwitch = true; + } + if(doubleSwitch) { player.getMinecraftConnection().delayedWrite( new Respawn(tempDim, joinGame.getPartialHashedSeed(), joinGame.getDifficulty(), joinGame.getGamemode(), joinGame.getLevelType(), - joinGame.getShouldKeepPlayerData(), - joinGame.getIsDebug(), joinGame.getIsFlat(), - joinGame.getDimensionRegistryName())); + false, dimensionInfo)); } + player.getMinecraftConnection().delayedWrite( new Respawn(joinGame.getDimension(), joinGame.getPartialHashedSeed(), joinGame.getDifficulty(), joinGame.getGamemode(), joinGame.getLevelType(), - joinGame.getShouldKeepPlayerData(), joinGame.getIsDebug(), joinGame.getIsFlat(), - joinGame.getDimensionRegistryName())); + doubleSwitch, joinGame.getDimensionInfo())); + + destination.setActiveDimensionRegistry(joinGame.getDimensionRegistry()); // 1.16 } // Remove previous boss bars. These don't get cleared when sending JoinGame, thus the need to diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java new file mode 100644 index 000000000..3e98da366 --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java @@ -0,0 +1,40 @@ +package com.velocitypowered.proxy.protocol; + +import javax.annotation.Nonnull; + +public class DimensionInfo { + + private final @Nonnull String dimensionIdentifier; + private final @Nonnull String dimensionLevelName; + private final boolean isFlat; + private final boolean isDebugType; + + public DimensionInfo(@Nonnull String dimensionIdentifier, @Nonnull String dimensionLevelName, boolean isFlat, boolean isDebugType) { + if(dimensionIdentifier == null || dimensionIdentifier.isEmpty() || dimensionIdentifier.isBlank()) { + throw new IllegalArgumentException("DimensionRegistryName may not be empty or null"); + } + this.dimensionIdentifier = dimensionIdentifier; + if(dimensionLevelName == null || dimensionLevelName.isEmpty() || dimensionLevelName.isBlank()) { + throw new IllegalArgumentException("DimensionLevelName may not be empty or null"); + } + this.dimensionLevelName = dimensionLevelName; + this.isFlat = isFlat; + this.isDebugType = isDebugType; + } + + public boolean isDebugType() { + return isDebugType; + } + + public boolean isFlat() { + return isFlat; + } + + public @Nonnull String getDimensionLevelName() { + return dimensionLevelName; + } + + public @Nonnull String getDimensionIdentifier() { + return dimensionIdentifier; + } +} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java new file mode 100644 index 000000000..d4305f540 --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java @@ -0,0 +1,113 @@ +package com.velocitypowered.proxy.protocol; + +import net.kyori.nbt.CompoundTag; +import net.kyori.nbt.ListTag; +import net.kyori.nbt.Tag; +import net.kyori.nbt.TagType; + +import javax.annotation.Nonnull; +import java.util.*; + +public class DimensionRegistry { + + private final @Nonnull Map dimensionRegistry; + private final @Nonnull Set worldNames; + + public DimensionRegistry(Map dimensionRegistry, Set worldNames) { + if(dimensionRegistry == null || dimensionRegistry.isEmpty() || worldNames == null || worldNames.isEmpty()) { + throw new IllegalArgumentException("DimensionRegistry requires valid arguments, not null and not empty"); + } + this.dimensionRegistry = dimensionRegistry; + this.worldNames = worldNames; + } + + public @Nonnull Map getDimensionRegistry() { + return dimensionRegistry; + } + + public @Nonnull Set getWorldNames() { + return worldNames; + } + + public @Nonnull String getDimensionIdentifier(@Nonnull String dimensionName) { + if (dimensionName == null) { + throw new IllegalArgumentException("DimensionName cannot be null!"); + } + if (dimensionName == null || !dimensionRegistry.containsKey(dimensionName)) { + throw new NoSuchElementException("DimensionName " + dimensionName + " doesn't exist in this Registry!"); + } + return dimensionRegistry.get(dimensionName); + } + + public @Nonnull String getDimensionName(@Nonnull String dimensionIdentifier) { + if (dimensionIdentifier == null) { + throw new IllegalArgumentException("DimensionIdentifier cannot be null!"); + } + for (Map.Entry entry : dimensionRegistry.entrySet()){ + if(entry.getValue().equals(dimensionIdentifier)){ + return entry.getKey(); + } + } + throw new NoSuchElementException("DimensionIdentifier " + dimensionIdentifier + " doesn't exist in this Registry!"); + } + + public boolean isValidFor(@Nonnull DimensionInfo toValidate) { + if(toValidate == null) { + throw new IllegalArgumentException("DimensionInfo cannot be null"); + } + try{ + if (!worldNames.contains(toValidate.getDimensionLevelName())){ + return false; + } + getDimensionName(toValidate.getDimensionIdentifier()); + return true; + + } catch(NoSuchElementException thrown){ + return false; + } + + } + + public CompoundTag encodeToCompoundTag(){ + CompoundTag ret = new CompoundTag(); + ListTag list = new ListTag(TagType.COMPOUND); + for(Map.Entry entry : dimensionRegistry.entrySet()){ + CompoundTag item = new CompoundTag(); + item.putString("key", entry.getKey()); + item.putString("element", entry.getValue()); + list.add(item); + } + ret.put("dimension", list); + return ret; + } + + public static Map parseToMapping(@Nonnull CompoundTag toParse){ + if(toParse == null) { + throw new IllegalArgumentException("CompoundTag cannot be null"); + } + if(!toParse.contains("dimension", TagType.LIST)){ + throw new IllegalStateException("CompoundTag does not contain a dimension List"); + } + ListTag dimensions = toParse.getList("dimension"); + Map mappings = new HashMap(); + for(Tag iter : dimensions){ + if(iter instanceof CompoundTag){ + throw new IllegalStateException("DimensionList in CompoundTag contains an invalid entry"); + } + CompoundTag mapping = (CompoundTag) iter; + String key = mapping.getString("key", null); + String element = mapping.getString("element", null); + if(element == null || key == null){ + throw new IllegalStateException("DimensionList in CompoundTag contains an mapping"); + } + if(mappings.containsKey(key) || mappings.containsValue(element)) { + throw new IllegalStateException("Dimension mappings may not have identifier/name duplicates"); + } + mappings.put(key, element); + } + if(mappings.isEmpty()){ + throw new IllegalStateException("Dimension mapping cannot be empty"); + } + return mappings; + } +} 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 cfedec888..e2b55c544 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.UUID; import net.kyori.nbt.CompoundTag; +import net.kyori.nbt.TagType; public enum ProtocolUtils { ; @@ -204,7 +205,7 @@ public enum ProtocolUtils { try { DataInput input = new ByteBufInputStream(buf); byte type = input.readByte(); - if (type != 10) { + if (type != TagType.COMPOUND.id()) { throw new DecoderException("NBTTag is not a CompoundTag"); } input.readUTF(); // Head-padding @@ -236,6 +237,36 @@ public enum ProtocolUtils { } } + /** + * Reads a String array from the {@code buf}. + * @param buf the buffer to read from + * @return the String array from the buffer + */ + public static String[] readStringArray(ByteBuf buf) { + int length = readVarInt(buf); + String[] ret = new String[length]; + for(int i = 0; i < length; i++) { + ret[i] = readString(buf); + } + return ret; + } + + /** + * Writes a String Array to the {@code buf}. + * @param buf the buffer to write to + * @param stringArray the array to write + */ + public static void writeStringArray(ByteBuf buf, String[] stringArray) { + if (stringArray == null) { + writeVarInt(buf, 0); + return; + } + writeVarInt(buf, stringArray.length); + for(int i = 0; i < stringArray.length; i++) { + writeString(buf, stringArray[i]); + } + } + /** * Writes a list of {@link com.velocitypowered.api.util.GameProfile.Property} to the buffer. * @param buf the buffer to write to 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 ef5315701..8ea5cbeb6 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 @@ -2,12 +2,17 @@ package com.velocitypowered.proxy.protocol.packet; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; +import com.velocitypowered.proxy.protocol.DimensionInfo; +import com.velocitypowered.proxy.protocol.DimensionRegistry; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; import net.kyori.nbt.CompoundTag; import org.checkerframework.checker.nullness.qual.Nullable; +import java.util.Map; +import java.util.Set; + public class JoinGame implements MinecraftPacket { private int entityId; @@ -20,11 +25,8 @@ public class JoinGame implements MinecraftPacket { private int viewDistance; // 1.14+ private boolean reducedDebugInfo; private boolean showRespawnScreen; - private boolean shouldKeepPlayerData; // 1.16+ - private boolean isDebug; // 1.16+ - private boolean isFlat; // 1.16+ - private String dimensionRegistryName; // 1.16+ - private CompoundTag dimensionRegistry; // 1.16+ + private DimensionRegistry dimensionRegistry; // 1.16+ + private DimensionInfo dimensionInfo; // 1.16+ public int getEntityId() { return entityId; @@ -97,43 +99,19 @@ public class JoinGame implements MinecraftPacket { this.reducedDebugInfo = reducedDebugInfo; } - public boolean getShouldKeepPlayerData() { - return shouldKeepPlayerData; + public DimensionInfo getDimensionInfo() { + return dimensionInfo; } - public void setShouldKeepPlayerData(boolean shouldKeepPlayerData) { - this.shouldKeepPlayerData = shouldKeepPlayerData; + public void setDimensionInfo(DimensionInfo dimensionInfo) { + this.dimensionInfo = dimensionInfo; } - public boolean getIsDebug() { - return isDebug; - } - - public void setIsDebug(boolean isDebug) { - this.isDebug = isDebug; - } - - public boolean getIsFlat() { - return isFlat; - } - - public void setIsFlat(boolean isFlat) { - this.isFlat = isFlat; - } - - public String getDimensionRegistryName() { - return dimensionRegistryName; - } - - public void setDimensionRegistryName(String dimensionRegistryName) { - this.dimensionRegistryName = dimensionRegistryName; - } - - public CompoundTag getDimensionRegistry() { + public DimensionRegistry getDimensionRegistry() { return dimensionRegistry; } - public void setDimensionRegistry(CompoundTag dimensionRegistry) { + public void setDimensionRegistry(DimensionRegistry dimensionRegistry) { this.dimensionRegistry = dimensionRegistry; } @@ -149,10 +127,8 @@ public class JoinGame implements MinecraftPacket { + ", levelType='" + levelType + '\'' + ", viewDistance=" + viewDistance + ", reducedDebugInfo=" + reducedDebugInfo - + ", shouldKeepPlayerData=" + shouldKeepPlayerData - + ", isDebug=" + isDebug - + ", isFlat='" + isFlat - + ", dimensionRegistryName='" + dimensionRegistryName + '\'' + + ", dimensionRegistry='" + dimensionRegistry.toString() + '\'' + + ", dimensionInfo='" + dimensionInfo.toString() + '\'' + '}'; } @@ -160,9 +136,14 @@ public class JoinGame implements MinecraftPacket { public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { this.entityId = buf.readInt(); this.gamemode = buf.readUnsignedByte(); + String dimensionIdentifier = null; + String levelName = null; if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - this.dimensionRegistry = ProtocolUtils.readCompoundTag(buf); - this.dimensionRegistryName = ProtocolUtils.readString(buf); + String levelNames[] = ProtocolUtils.readStringArray(buf); + Map dimensionMapping = DimensionRegistry.parseToMapping(ProtocolUtils.readCompoundTag(buf)); + this.dimensionRegistry = new DimensionRegistry(dimensionMapping, Set.of(levelNames)); + dimensionIdentifier = ProtocolUtils.readString(buf); + levelName = ProtocolUtils.readString(buf); } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) { this.dimension = buf.readInt(); } else { @@ -188,8 +169,9 @@ public class JoinGame implements MinecraftPacket { this.showRespawnScreen = buf.readBoolean(); } if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - isDebug = buf.readBoolean(); - isFlat = buf.readBoolean(); + boolean isDebug = buf.readBoolean(); + boolean isFlat = buf.readBoolean(); + this.dimensionInfo = new DimensionInfo(dimensionIdentifier, levelName, isFlat, isDebug); } } @@ -198,8 +180,10 @@ public class JoinGame implements MinecraftPacket { buf.writeInt(entityId); buf.writeByte(gamemode); if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - ProtocolUtils.writeCompoundTag(buf, dimensionRegistry); - ProtocolUtils.writeString(buf, dimensionRegistryName); + ProtocolUtils.writeStringArray(buf, dimensionRegistry.getWorldNames().toArray(new String[dimensionRegistry.getWorldNames().size()])); + ProtocolUtils.writeCompoundTag(buf, dimensionRegistry.encodeToCompoundTag()); + ProtocolUtils.writeString(buf, dimensionInfo.getDimensionIdentifier()); + ProtocolUtils.writeString(buf, dimensionInfo.getDimensionLevelName()); } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) { buf.writeInt(dimension); } else { @@ -226,8 +210,8 @@ public class JoinGame implements MinecraftPacket { buf.writeBoolean(showRespawnScreen); } if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - buf.writeBoolean(isDebug); - buf.writeBoolean(isFlat); + buf.writeBoolean(dimensionInfo.isDebugType()); + buf.writeBoolean(dimensionInfo.isFlat()); } } 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 0f988a5f9..67fafb7f7 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,6 +2,7 @@ package com.velocitypowered.proxy.protocol.packet; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; +import com.velocitypowered.proxy.protocol.DimensionInfo; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; @@ -14,24 +15,20 @@ public class Respawn implements MinecraftPacket { private short gamemode; private String levelType = ""; private boolean shouldKeepPlayerData; // 1.16+ - private boolean isDebug; // 1.16+ - private boolean isFlat; // 1.16+ - private String dimensionRegistryName; // 1.16+ + private DimensionInfo dimensionInfo; public Respawn() { } public Respawn(int dimension, long partialHashedSeed, short difficulty, short gamemode, - String levelType, boolean shouldKeepPlayerData, boolean isDebug, boolean isFlat, String dimensionRegistryName) { + String levelType, boolean shouldKeepPlayerData, DimensionInfo dimensionInfo) { this.dimension = dimension; this.partialHashedSeed = partialHashedSeed; this.difficulty = difficulty; this.gamemode = gamemode; this.levelType = levelType; this.shouldKeepPlayerData = shouldKeepPlayerData; - this.isDebug = isDebug; - this.isFlat = isFlat; - this.dimensionRegistryName = dimensionRegistryName; + this.dimensionInfo = dimensionInfo; } public int getDimension() { @@ -82,30 +79,6 @@ public class Respawn implements MinecraftPacket { this.shouldKeepPlayerData = shouldKeepPlayerData; } - public boolean getIsDebug() { - return isDebug; - } - - public void setIsDebug(boolean isDebug) { - this.isDebug = isDebug; - } - - public boolean getIsFlat() { - return isFlat; - } - - public void setIsFlat(boolean isFlat) { - this.isFlat = isFlat; - } - - public String getDimensionRegistryName() { - return dimensionRegistryName; - } - - public void setDimensionRegistryName(String dimensionRegistryName) { - this.dimensionRegistryName = dimensionRegistryName; - } - @Override public String toString() { return "Respawn{" @@ -115,16 +88,17 @@ public class Respawn implements MinecraftPacket { + ", gamemode=" + gamemode + ", levelType='" + levelType + '\'' + ", shouldKeepPlayerData=" + shouldKeepPlayerData - + ", isDebug=" + isDebug - + ", isFlat='" + isFlat - + ", dimensionRegistryName='" + dimensionRegistryName + '\'' + + ", dimensionRegistryName='" + dimensionInfo.toString() + '\'' + '}'; } @Override public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) { + String dimensionIdentifier = null; + String levelName = null; if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - this.dimensionRegistryName = ProtocolUtils.readString(buf); // Not sure what the cap on that is + dimensionIdentifier = ProtocolUtils.readString(buf); + levelName = ProtocolUtils.readString(buf); } else { this.dimension = buf.readInt(); } @@ -136,8 +110,9 @@ public class Respawn implements MinecraftPacket { } this.gamemode = buf.readUnsignedByte(); if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - isDebug = buf.readBoolean(); - isFlat = buf.readBoolean(); + boolean isDebug = buf.readBoolean(); + boolean isFlat = buf.readBoolean(); + this.dimensionInfo = new DimensionInfo(dimensionIdentifier, levelName, isFlat, isDebug); shouldKeepPlayerData = buf.readBoolean(); } else { this.levelType = ProtocolUtils.readString(buf, 16); @@ -147,7 +122,8 @@ 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, dimensionRegistryName); + ProtocolUtils.writeString(buf, dimensionInfo.getDimensionIdentifier()); + ProtocolUtils.writeString(buf, dimensionInfo.getDimensionLevelName()); } else { buf.writeInt(dimension); } @@ -159,8 +135,8 @@ public class Respawn implements MinecraftPacket { } buf.writeByte(gamemode); if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - buf.writeBoolean(isDebug); - buf.writeBoolean(isFlat); + buf.writeBoolean(dimensionInfo.isDebugType()); + buf.writeBoolean(dimensionInfo.isFlat()); buf.writeBoolean(shouldKeepPlayerData); } else { ProtocolUtils.writeString(buf, levelType); From 6734ef3a087ed3f068ee9ce809d93ce31ce844bc Mon Sep 17 00:00:00 2001 From: "Five (Xer)" Date: Thu, 4 Jun 2020 19:13:10 +0200 Subject: [PATCH 2/7] Checkstyle-auto --- .../client/ClientPlaySessionHandler.java | 13 +- .../proxy/protocol/DimensionInfo.java | 57 +++--- .../proxy/protocol/DimensionRegistry.java | 186 +++++++++--------- .../proxy/protocol/ProtocolUtils.java | 4 +- 4 files changed, 135 insertions(+), 125 deletions(-) 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 5a0820cd9..7dc865355 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 @@ -352,17 +352,18 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { boolean doubleSwitch; // This is not ONE if because this will all be null in < 1.16 if (player.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_16) < 0) { - if(dimensionRegistry.getWorldNames().size() > 1 && dimensionRegistry.getDimensionRegistry().size() > 1){ + if (dimensionRegistry.getWorldNames().size() > 1 + && dimensionRegistry.getDimensionRegistry().size() > 1) { String tmpDimLevelName = null; - for(String s : dimensionRegistry.getWorldNames()){ - if(!s.equals(dimensionInfo.getDimensionLevelName())){ + for (String s : dimensionRegistry.getWorldNames()) { + if (!s.equals(dimensionInfo.getDimensionLevelName())) { tmpDimLevelName = s; break; } } String tmpDimIdentifier = null; - for(String s : dimensionRegistry.getDimensionRegistry().keySet()){ - if(!s.equals(dimensionInfo.getDimensionIdentifier())){ + for (String s : dimensionRegistry.getDimensionRegistry().keySet()) { + if (!s.equals(dimensionInfo.getDimensionIdentifier())) { tmpDimIdentifier = s; break; } @@ -376,7 +377,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } else { doubleSwitch = true; } - if(doubleSwitch) { + if (doubleSwitch) { player.getMinecraftConnection().delayedWrite( new Respawn(tempDim, joinGame.getPartialHashedSeed(), joinGame.getDifficulty(), joinGame.getGamemode(), joinGame.getLevelType(), 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 3e98da366..1a5a5245b 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java @@ -4,37 +4,40 @@ import javax.annotation.Nonnull; public class DimensionInfo { - private final @Nonnull String dimensionIdentifier; - private final @Nonnull String dimensionLevelName; - private final boolean isFlat; - private final boolean isDebugType; + private final @Nonnull String dimensionIdentifier; + private final @Nonnull String dimensionLevelName; + private final boolean isFlat; + private final boolean isDebugType; - public DimensionInfo(@Nonnull String dimensionIdentifier, @Nonnull String dimensionLevelName, boolean isFlat, boolean isDebugType) { - if(dimensionIdentifier == null || dimensionIdentifier.isEmpty() || dimensionIdentifier.isBlank()) { - throw new IllegalArgumentException("DimensionRegistryName may not be empty or null"); - } - this.dimensionIdentifier = dimensionIdentifier; - if(dimensionLevelName == null || dimensionLevelName.isEmpty() || dimensionLevelName.isBlank()) { - throw new IllegalArgumentException("DimensionLevelName may not be empty or null"); - } - this.dimensionLevelName = dimensionLevelName; - this.isFlat = isFlat; - this.isDebugType = isDebugType; + public DimensionInfo(@Nonnull String dimensionIdentifier, @Nonnull String dimensionLevelName, + boolean isFlat, boolean isDebugType) { + if (dimensionIdentifier == null || dimensionIdentifier.isEmpty() + || dimensionIdentifier.isBlank()) { + throw new IllegalArgumentException("DimensionRegistryName may not be empty or null"); } + this.dimensionIdentifier = dimensionIdentifier; + if (dimensionLevelName == null || dimensionLevelName.isEmpty() + || dimensionLevelName.isBlank()) { + throw new IllegalArgumentException("DimensionLevelName may not be empty or null"); + } + this.dimensionLevelName = dimensionLevelName; + this.isFlat = isFlat; + this.isDebugType = isDebugType; + } - public boolean isDebugType() { - return isDebugType; - } + public boolean isDebugType() { + return isDebugType; + } - public boolean isFlat() { - return isFlat; - } + public boolean isFlat() { + return isFlat; + } - public @Nonnull String getDimensionLevelName() { - return dimensionLevelName; - } + public @Nonnull String getDimensionLevelName() { + return dimensionLevelName; + } - public @Nonnull String getDimensionIdentifier() { - return dimensionIdentifier; - } + public @Nonnull String getDimensionIdentifier() { + return dimensionIdentifier; + } } 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 d4305f540..70ea76e26 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java @@ -1,113 +1,119 @@ package com.velocitypowered.proxy.protocol; +import java.util.HashMap; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Set; +import javax.annotation.Nonnull; import net.kyori.nbt.CompoundTag; import net.kyori.nbt.ListTag; import net.kyori.nbt.Tag; import net.kyori.nbt.TagType; -import javax.annotation.Nonnull; -import java.util.*; - public class DimensionRegistry { - private final @Nonnull Map dimensionRegistry; - private final @Nonnull Set worldNames; + private final @Nonnull Map dimensionRegistry; + private final @Nonnull Set worldNames; - public DimensionRegistry(Map dimensionRegistry, Set worldNames) { - if(dimensionRegistry == null || dimensionRegistry.isEmpty() || worldNames == null || worldNames.isEmpty()) { - throw new IllegalArgumentException("DimensionRegistry requires valid arguments, not null and not empty"); - } - this.dimensionRegistry = dimensionRegistry; - this.worldNames = worldNames; + public DimensionRegistry(Map dimensionRegistry, + Set worldNames) { + if (dimensionRegistry == null || dimensionRegistry.isEmpty() + || worldNames == null || worldNames.isEmpty()) { + throw new IllegalArgumentException( + "DimensionRegistry requires valid arguments, not null and not empty"); } + this.dimensionRegistry = dimensionRegistry; + this.worldNames = worldNames; + } - public @Nonnull Map getDimensionRegistry() { - return dimensionRegistry; + public @Nonnull Map getDimensionRegistry() { + return dimensionRegistry; + } + + public @Nonnull Set getWorldNames() { + return worldNames; + } + + public @Nonnull String getDimensionIdentifier(@Nonnull String dimensionName) { + if (dimensionName == null) { + throw new IllegalArgumentException("DimensionName cannot be null!"); } - - public @Nonnull Set getWorldNames() { - return worldNames; + if (dimensionName == null || !dimensionRegistry.containsKey(dimensionName)) { + throw new NoSuchElementException("DimensionName " + dimensionName + + " doesn't exist in this Registry!"); } + return dimensionRegistry.get(dimensionName); + } - public @Nonnull String getDimensionIdentifier(@Nonnull String dimensionName) { - if (dimensionName == null) { - throw new IllegalArgumentException("DimensionName cannot be null!"); - } - if (dimensionName == null || !dimensionRegistry.containsKey(dimensionName)) { - throw new NoSuchElementException("DimensionName " + dimensionName + " doesn't exist in this Registry!"); - } - return dimensionRegistry.get(dimensionName); + public @Nonnull String getDimensionName(@Nonnull String dimensionIdentifier) { + if (dimensionIdentifier == null) { + throw new IllegalArgumentException("DimensionIdentifier cannot be null!"); } - - public @Nonnull String getDimensionName(@Nonnull String dimensionIdentifier) { - if (dimensionIdentifier == null) { - throw new IllegalArgumentException("DimensionIdentifier cannot be null!"); - } - for (Map.Entry entry : dimensionRegistry.entrySet()){ - if(entry.getValue().equals(dimensionIdentifier)){ - return entry.getKey(); - } - } - throw new NoSuchElementException("DimensionIdentifier " + dimensionIdentifier + " doesn't exist in this Registry!"); + for (Map.Entry entry : dimensionRegistry.entrySet()) { + if (entry.getValue().equals(dimensionIdentifier)) { + return entry.getKey(); + } } + throw new NoSuchElementException("DimensionIdentifier " + dimensionIdentifier + + " doesn't exist in this Registry!"); + } - public boolean isValidFor(@Nonnull DimensionInfo toValidate) { - if(toValidate == null) { - throw new IllegalArgumentException("DimensionInfo cannot be null"); - } - try{ - if (!worldNames.contains(toValidate.getDimensionLevelName())){ - return false; - } - getDimensionName(toValidate.getDimensionIdentifier()); - return true; - - } catch(NoSuchElementException thrown){ - return false; - } - + public boolean isValidFor(@Nonnull DimensionInfo toValidate) { + if (toValidate == null) { + throw new IllegalArgumentException("DimensionInfo cannot be null"); } - - public CompoundTag encodeToCompoundTag(){ - CompoundTag ret = new CompoundTag(); - ListTag list = new ListTag(TagType.COMPOUND); - for(Map.Entry entry : dimensionRegistry.entrySet()){ - CompoundTag item = new CompoundTag(); - item.putString("key", entry.getKey()); - item.putString("element", entry.getValue()); - list.add(item); - } - ret.put("dimension", list); - return ret; + try { + if (!worldNames.contains(toValidate.getDimensionLevelName())) { + return false; + } + getDimensionName(toValidate.getDimensionIdentifier()); + return true; + } catch (NoSuchElementException thrown) { + return false; } + } - public static Map parseToMapping(@Nonnull CompoundTag toParse){ - if(toParse == null) { - throw new IllegalArgumentException("CompoundTag cannot be null"); - } - if(!toParse.contains("dimension", TagType.LIST)){ - throw new IllegalStateException("CompoundTag does not contain a dimension List"); - } - ListTag dimensions = toParse.getList("dimension"); - Map mappings = new HashMap(); - for(Tag iter : dimensions){ - if(iter instanceof CompoundTag){ - throw new IllegalStateException("DimensionList in CompoundTag contains an invalid entry"); - } - CompoundTag mapping = (CompoundTag) iter; - String key = mapping.getString("key", null); - String element = mapping.getString("element", null); - if(element == null || key == null){ - throw new IllegalStateException("DimensionList in CompoundTag contains an mapping"); - } - if(mappings.containsKey(key) || mappings.containsValue(element)) { - throw new IllegalStateException("Dimension mappings may not have identifier/name duplicates"); - } - mappings.put(key, element); - } - if(mappings.isEmpty()){ - throw new IllegalStateException("Dimension mapping cannot be empty"); - } - return mappings; + public CompoundTag encodeToCompoundTag() { + CompoundTag ret = new CompoundTag(); + ListTag list = new ListTag(TagType.COMPOUND); + for (Map.Entry entry : dimensionRegistry.entrySet()) { + CompoundTag item = new CompoundTag(); + item.putString("key", entry.getKey()); + item.putString("element", entry.getValue()); + list.add(item); } + ret.put("dimension", list); + return ret; + } + + public static Map parseToMapping(@Nonnull CompoundTag toParse) { + if (toParse == null) { + throw new IllegalArgumentException("CompoundTag cannot be null"); + } + if (!toParse.contains("dimension", TagType.LIST)) { + throw new IllegalStateException("CompoundTag does not contain a dimension List"); + } + ListTag dimensions = toParse.getList("dimension"); + Map mappings = new HashMap(); + for (Tag iter : dimensions) { + if (iter instanceof CompoundTag) { + throw new IllegalStateException("DimensionList in CompoundTag contains an invalid entry"); + } + CompoundTag mapping = (CompoundTag) iter; + String key = mapping.getString("key", null); + String element = mapping.getString("element", null); + if (element == null || key == null) { + throw new IllegalStateException("DimensionList in CompoundTag contains an mapping"); + } + if (mappings.containsKey(key) || mappings.containsValue(element)) { + throw new IllegalStateException( + "Dimension mappings may not have identifier/name duplicates"); + } + mappings.put(key, element); + } + if (mappings.isEmpty()) { + throw new IllegalStateException("Dimension mapping cannot be empty"); + } + return mappings; + } } 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 e2b55c544..a272c8023 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -245,7 +245,7 @@ public enum ProtocolUtils { public static String[] readStringArray(ByteBuf buf) { int length = readVarInt(buf); String[] ret = new String[length]; - for(int i = 0; i < length; i++) { + for (int i = 0; i < length; i++) { ret[i] = readString(buf); } return ret; @@ -262,7 +262,7 @@ public enum ProtocolUtils { return; } writeVarInt(buf, stringArray.length); - for(int i = 0; i < stringArray.length; i++) { + for (int i = 0; i < stringArray.length; i++) { writeString(buf, stringArray[i]); } } From 009f207883738e3ee1b1fb51adf614fec89b8a4c Mon Sep 17 00:00:00 2001 From: "Five (Xer)" Date: Thu, 4 Jun 2020 21:21:54 +0200 Subject: [PATCH 3/7] More progress --- .../api/network/ProtocolVersion.java | 2 +- .../proxy/protocol/DimensionInfo.java | 9 +++ .../proxy/protocol/DimensionRegistry.java | 62 ++++++++++++++----- .../proxy/protocol/StateRegistry.java | 39 ++++++++---- 4 files changed, 84 insertions(+), 28 deletions(-) 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 07b35926b..3f86aced0 100644 --- a/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java +++ b/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java @@ -35,7 +35,7 @@ public enum ProtocolVersion { MINECRAFT_1_15(573, "1.15"), MINECRAFT_1_15_1(575, "1.15.1"), MINECRAFT_1_15_2(578, "1.15.2"), - MINECRAFT_1_16(718, "1.16"); + MINECRAFT_1_16(721, "1.16"); private final int protocol; private final String name; 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 1a5a5245b..803f011a1 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java @@ -1,5 +1,7 @@ package com.velocitypowered.proxy.protocol; +import com.velocitypowered.proxy.connection.MinecraftConnection; + import javax.annotation.Nonnull; public class DimensionInfo { @@ -9,6 +11,13 @@ public class DimensionInfo { 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 dimensionLevelName 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 dimensionLevelName, boolean isFlat, boolean isDebugType) { if (dimensionIdentifier == null || dimensionIdentifier.isEmpty() 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 70ea76e26..2c917d29f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java @@ -12,15 +12,26 @@ import net.kyori.nbt.TagType; public class DimensionRegistry { + // Mapping: + // dimensionIdentifier (Client connection refers to this), + // dimensionType (The game refers to this). private final @Nonnull Map dimensionRegistry; private final @Nonnull Set worldNames; + /** + * Initializes a new {@link DimensionRegistry} instance. + * This registry is required for 1.16+ clients/servers to communicate, + * it constrains the dimension types and names the client can be sent + * in a Respawn action (dimension change). + * @param dimensionRegistry a populated map containing dimensionIdentifier and dimensionType sets + * @param worldNames a populated {@link Set} of the dimension level names the server offers + */ public DimensionRegistry(Map dimensionRegistry, Set worldNames) { if (dimensionRegistry == null || dimensionRegistry.isEmpty() || worldNames == null || worldNames.isEmpty()) { throw new IllegalArgumentException( - "DimensionRegistry requires valid arguments, not null and not empty"); + "Dimension registry requires valid arguments, not null and not empty"); } this.dimensionRegistry = dimensionRegistry; this.worldNames = worldNames; @@ -34,45 +45,64 @@ public class DimensionRegistry { return worldNames; } - public @Nonnull String getDimensionIdentifier(@Nonnull String dimensionName) { - if (dimensionName == null) { - throw new IllegalArgumentException("DimensionName cannot be null!"); + /** + * Returns the internal dimension type as used by the game. + * @param dimensionIdentifier how the type is identified by the connection + * @return game internal dimension type + */ + public @Nonnull String getDimensionType(@Nonnull String dimensionIdentifier) { + if (dimensionIdentifier == null) { + throw new IllegalArgumentException("Dimension identifier cannot be null!"); } - if (dimensionName == null || !dimensionRegistry.containsKey(dimensionName)) { - throw new NoSuchElementException("DimensionName " + dimensionName + if (dimensionIdentifier == null || !dimensionRegistry.containsKey(dimensionIdentifier)) { + throw new NoSuchElementException("Dimension with identifier " + dimensionIdentifier + " doesn't exist in this Registry!"); } - return dimensionRegistry.get(dimensionName); + return dimensionRegistry.get(dimensionIdentifier); } - public @Nonnull String getDimensionName(@Nonnull String dimensionIdentifier) { - if (dimensionIdentifier == null) { - throw new IllegalArgumentException("DimensionIdentifier cannot be null!"); + /** + * Returns the dimension identifier as used by the client. + * @param dimensionType the internal dimension type + * @return game dimension identifier + */ + public @Nonnull String getDimensionIdentifier(@Nonnull String dimensionType) { + if (dimensionType == null) { + throw new IllegalArgumentException("Dimension type cannot be null!"); } for (Map.Entry entry : dimensionRegistry.entrySet()) { - if (entry.getValue().equals(dimensionIdentifier)) { + if (entry.getValue().equals(dimensionType)) { return entry.getKey(); } } - throw new NoSuchElementException("DimensionIdentifier " + dimensionIdentifier + throw new NoSuchElementException("Dimension type " + dimensionType + " doesn't exist in this Registry!"); } + /** + * Checks a {@link DimensionInfo} against this registry. + * @param toValidate the {@link DimensionInfo} to validate + * @return true: the dimension information is valid for this registry + */ public boolean isValidFor(@Nonnull DimensionInfo toValidate) { if (toValidate == null) { - throw new IllegalArgumentException("DimensionInfo cannot be null"); + throw new IllegalArgumentException("Dimension info cannot be null"); } try { if (!worldNames.contains(toValidate.getDimensionLevelName())) { return false; } - getDimensionName(toValidate.getDimensionIdentifier()); + getDimensionType(toValidate.getDimensionIdentifier()); return true; } catch (NoSuchElementException thrown) { return false; } } + /** + * Encodes the stored Dimension registry as CompoundTag. + * @return the CompoundTag containing identifier:type mappings + */ public CompoundTag encodeToCompoundTag() { CompoundTag ret = new CompoundTag(); ListTag list = new ListTag(TagType.COMPOUND); @@ -86,6 +116,10 @@ public class DimensionRegistry { return ret; } + /** + * Decodes a CompoundTag storing dimension mappings to a Map identifier:type. + * @param toParse CompoundTag containing a dimension registry + */ public static Map parseToMapping(@Nonnull CompoundTag toParse) { if (toParse == null) { throw new IllegalArgumentException("CompoundTag cannot be null"); 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 07f174b51..658c818d7 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -126,44 +126,52 @@ public enum StateRegistry { clientbound.register(BossBar.class, BossBar::new, map(0x0C, MINECRAFT_1_9, false), - map(0x0D, MINECRAFT_1_15, false)); + map(0x0D, MINECRAFT_1_15, false), + map(0x0C, MINECRAFT_1_16, false)); clientbound.register(Chat.class, Chat::new, map(0x02, MINECRAFT_1_8, true), map(0x0F, MINECRAFT_1_9, true), map(0x0E, MINECRAFT_1_13, true), - map(0x0F, MINECRAFT_1_15, true)); + map(0x0F, MINECRAFT_1_15, true), + map(0x0E, MINECRAFT_1_16, true)); clientbound.register(TabCompleteResponse.class, TabCompleteResponse::new, map(0x3A, MINECRAFT_1_8, false), map(0x0E, MINECRAFT_1_9, false), map(0x10, MINECRAFT_1_13, false), - map(0x11, MINECRAFT_1_15, false)); + map(0x11, MINECRAFT_1_15, false), + map(0x10, MINECRAFT_1_16, false)); clientbound.register(AvailableCommands.class, AvailableCommands::new, map(0x11, MINECRAFT_1_13, false), - map(0x12, MINECRAFT_1_15, false)); + map(0x12, MINECRAFT_1_15, false), + map(0x11, MINECRAFT_1_16, 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(0x19, MINECRAFT_1_15, false), + map(0x18, MINECRAFT_1_16, 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(0x1B, MINECRAFT_1_15, false), + map(0x1A, MINECRAFT_1_16, 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(0x21, MINECRAFT_1_15, false), + map(0x20, MINECRAFT_1_16, 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(0x26, MINECRAFT_1_15, false), + map(0x25, MINECRAFT_1_16, false)); clientbound.register(Respawn.class, Respawn::new, map(0x07, MINECRAFT_1_8, true), map(0x33, MINECRAFT_1_9, true), @@ -171,7 +179,8 @@ public enum StateRegistry { map(0x35, MINECRAFT_1_12_1, true), map(0x38, MINECRAFT_1_13, true), map(0x3A, MINECRAFT_1_14, true), - map(0x3B, MINECRAFT_1_15, true)); + map(0x3B, MINECRAFT_1_15, true), + map(0x3A, MINECRAFT_1_16, true)); clientbound.register(ResourcePackRequest.class, ResourcePackRequest::new, map(0x48, MINECRAFT_1_8, true), map(0x32, MINECRAFT_1_9, true), @@ -179,7 +188,8 @@ public enum StateRegistry { map(0x34, MINECRAFT_1_12_1, true), map(0x37, MINECRAFT_1_13, true), map(0x39, MINECRAFT_1_14, true), - map(0x3A, MINECRAFT_1_15, true)); + map(0x3A, MINECRAFT_1_15, true), + map(0x39, MINECRAFT_1_16, true)); clientbound.register(HeaderAndFooter.class, HeaderAndFooter::new, map(0x47, MINECRAFT_1_8, true), map(0x48, MINECRAFT_1_9, true), @@ -188,7 +198,8 @@ public enum StateRegistry { map(0x4A, MINECRAFT_1_12_1, true), map(0x4E, MINECRAFT_1_13, true), map(0x53, MINECRAFT_1_14, true), - map(0x54, MINECRAFT_1_15, true)); + map(0x54, MINECRAFT_1_15, true), + map(0x53, MINECRAFT_1_16, true)); clientbound.register(TitlePacket.class, TitlePacket::new, map(0x45, MINECRAFT_1_8, true), map(0x45, MINECRAFT_1_9, true), @@ -196,14 +207,16 @@ public enum StateRegistry { map(0x48, MINECRAFT_1_12_1, true), map(0x4B, MINECRAFT_1_13, true), map(0x4F, MINECRAFT_1_14, true), - map(0x50, MINECRAFT_1_15, true)); + map(0x50, MINECRAFT_1_15, true), + map(0x4F, MINECRAFT_1_16, true)); clientbound.register(PlayerListItem.class, PlayerListItem::new, map(0x38, MINECRAFT_1_8, false), map(0x2D, MINECRAFT_1_9, false), map(0x2E, MINECRAFT_1_12_1, false), map(0x30, MINECRAFT_1_13, false), map(0x33, MINECRAFT_1_14, false), - map(0x34, MINECRAFT_1_15, false)); + map(0x34, MINECRAFT_1_15, false), + map(0x33, MINECRAFT_1_16, false)); } }, LOGIN { From 368d50b4555a87f0fb5ca734947f098e86ccac96 Mon Sep 17 00:00:00 2001 From: Lechner Markus Date: Fri, 5 Jun 2020 15:22:55 +0200 Subject: [PATCH 4/7] Rework Dimension Registry --- .../api/network/ProtocolVersion.java | 2 +- .../client/ClientPlaySessionHandler.java | 48 +------- .../proxy/protocol/DimensionData.java | 106 +++++++++++++++++ .../proxy/protocol/DimensionInfo.java | 18 ++- .../proxy/protocol/DimensionRegistry.java | 109 +++++++----------- .../proxy/protocol/packet/JoinGame.java | 15 +-- .../proxy/protocol/packet/Respawn.java | 2 +- 7 files changed, 166 insertions(+), 134 deletions(-) create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionData.java 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 3f86aced0..92326cc62 100644 --- a/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java +++ b/api/src/main/java/com/velocitypowered/api/network/ProtocolVersion.java @@ -35,7 +35,7 @@ public enum ProtocolVersion { MINECRAFT_1_15(573, "1.15"), MINECRAFT_1_15_1(575, "1.15.1"), MINECRAFT_1_15_2(578, "1.15.2"), - MINECRAFT_1_16(721, "1.16"); + MINECRAFT_1_16(722, "1.16"); private final int protocol; private final String name; 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 7dc865355..07132bac0 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 @@ -34,11 +34,9 @@ import io.netty.buffer.ByteBuf; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Queue; -import java.util.Set; import java.util.UUID; import net.kyori.text.TextComponent; import net.kyori.text.format.TextColor; @@ -336,58 +334,20 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { // to perform entity ID rewrites, eliminating potential issues from rewriting packets and // improving compatibility with mods. player.getMinecraftConnection().delayedWrite(joinGame); - int tempDim = joinGame.getDimension() == 0 ? -1 : 0; // Since 1.16 this dynamic changed: - // The respawn packet has a keepMetadata flag which should - // be true for dimension switches, so by double switching - // we can keep the flow of the game - // There is a problem here though: By only sending one dimension - // in the registry we can't do that, so we need to run an *unclean* switch. - // NOTE! We can't just send a fake dimension in the registry either - // to get two dimensions, as modded games will break with this. - final DimensionRegistry dimensionRegistry = joinGame.getDimensionRegistry(); - DimensionInfo dimensionInfo = joinGame.getDimensionInfo(); // 1.16+ - // The doubleSwitch variable doubles as keepMetadata flag for an unclean switch as - // well as to indicate the second switch. - boolean doubleSwitch; - // This is not ONE if because this will all be null in < 1.16 + // We don't need to send two dimension swiches anymore! if (player.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_16) < 0) { - if (dimensionRegistry.getWorldNames().size() > 1 - && dimensionRegistry.getDimensionRegistry().size() > 1) { - String tmpDimLevelName = null; - for (String s : dimensionRegistry.getWorldNames()) { - if (!s.equals(dimensionInfo.getDimensionLevelName())) { - tmpDimLevelName = s; - break; - } - } - String tmpDimIdentifier = null; - for (String s : dimensionRegistry.getDimensionRegistry().keySet()) { - if (!s.equals(dimensionInfo.getDimensionIdentifier())) { - tmpDimIdentifier = s; - break; - } - } - dimensionInfo = new DimensionInfo(tmpDimIdentifier, tmpDimLevelName, true, false); - doubleSwitch = true; - } else { - doubleSwitch = false; - // We should add a warning here. - } - } else { - doubleSwitch = true; - } - if (doubleSwitch) { + int tempDim = joinGame.getDimension() == 0 ? -1 : 0; player.getMinecraftConnection().delayedWrite( new Respawn(tempDim, joinGame.getPartialHashedSeed(), joinGame.getDifficulty(), joinGame.getGamemode(), joinGame.getLevelType(), - false, dimensionInfo)); + false, joinGame.getDimensionInfo())); } player.getMinecraftConnection().delayedWrite( new Respawn(joinGame.getDimension(), joinGame.getPartialHashedSeed(), joinGame.getDifficulty(), joinGame.getGamemode(), joinGame.getLevelType(), - doubleSwitch, joinGame.getDimensionInfo())); + false, joinGame.getDimensionInfo())); destination.setActiveDimensionRegistry(joinGame.getDimensionRegistry()); // 1.16 } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionData.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionData.java new file mode 100644 index 000000000..9ecfebe15 --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionData.java @@ -0,0 +1,106 @@ +package com.velocitypowered.proxy.protocol; + +import net.kyori.nbt.CompoundTag; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class DimensionData { + private final @Nonnull String registryIdentifier; + private final boolean isNatural; + private final float ambientLight; + private final boolean isShrunk; + private final boolean isUltrawarm; + private final boolean hasCeiling; + private final boolean hasSkylight; + private final @Nullable Long fixedTime; + private final @Nullable Boolean hasEnderdragonFight; + + public DimensionData(@Nonnull String registryIdentifier, boolean isNatural, + float ambientLight, boolean isShrunk, boolean isUltrawarm, + boolean hasCeiling, boolean hasSkylight, + @Nullable Long fixedTime, @Nullable Boolean hasEnderdragonFight) { + this.registryIdentifier = registryIdentifier; + this.isNatural = isNatural; + this.ambientLight = ambientLight; + this.isShrunk = isShrunk; + this.isUltrawarm = isUltrawarm; + this.hasCeiling = hasCeiling; + this.hasSkylight = hasSkylight; + this.fixedTime = fixedTime; + this.hasEnderdragonFight = hasEnderdragonFight; + } + + public @Nonnull String getRegistryIdentifier() { + return registryIdentifier; + } + + public boolean isNatural() { + return isNatural; + } + + public float getAmbientLight() { + return ambientLight; + } + + public boolean isShrunk() { + return isShrunk; + } + + public boolean isUltrawarm() { + return isUltrawarm; + } + + public boolean isHasCeiling() { + return hasCeiling; + } + + public boolean isHasSkylight() { + return hasSkylight; + } + + public @Nullable Long getFixedTime() { + return fixedTime; + } + + public @Nullable Boolean getHasEnderdragonFight() { + return hasEnderdragonFight; + } + + public static DimensionData fromNBT(@Nonnull CompoundTag toRead) { + if (toRead == null){ + throw new IllegalArgumentException("CompoundTag cannot be null"); + } + String registryIdentifier = toRead.getString("key"); + CompoundTag values = toRead.getCompound("element"); + boolean isNatural = values.getBoolean("natural"); + float ambientLight = values.getFloat("ambient_light"); + boolean isShrunk = values.getBoolean("shrunk"); + boolean isUltrawarm = values.getBoolean("ultrawarm"); + 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); + } + + public CompoundTag encode() { + CompoundTag ret = new CompoundTag(); + ret.putString("key", registryIdentifier); + CompoundTag values = new CompoundTag(); + values.putBoolean("natural", isNatural); + values.putFloat("ambient_light", ambientLight); + values.putBoolean("shrunk", isShrunk); + values.putBoolean("ultrawarm", isUltrawarm); + values.putBoolean("has_ceiling", hasCeiling); + values.putBoolean("has_skylight", hasSkylight); + if (fixedTime != null) { + values.putLong("fixed_time", fixedTime); + } + if (hasEnderdragonFight != null) { + values.putBoolean("has_enderdragon_fight", hasEnderdragonFight); + } + ret.put("element", values); + return ret; + } +} 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 803f011a1..787c7a948 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java @@ -1,35 +1,33 @@ package com.velocitypowered.proxy.protocol; -import com.velocitypowered.proxy.connection.MinecraftConnection; - import javax.annotation.Nonnull; public class DimensionInfo { private final @Nonnull String dimensionIdentifier; - private final @Nonnull String dimensionLevelName; + 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 dimensionLevelName the level name as displayed in the F3 menu and logs + * @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 dimensionLevelName, + public DimensionInfo(@Nonnull String dimensionIdentifier, @Nonnull String levelName, boolean isFlat, boolean isDebugType) { if (dimensionIdentifier == null || dimensionIdentifier.isEmpty() || dimensionIdentifier.isBlank()) { throw new IllegalArgumentException("DimensionRegistryName may not be empty or null"); } this.dimensionIdentifier = dimensionIdentifier; - if (dimensionLevelName == null || dimensionLevelName.isEmpty() - || dimensionLevelName.isBlank()) { + if (levelName == null || levelName.isEmpty() + || levelName.isBlank()) { throw new IllegalArgumentException("DimensionLevelName may not be empty or null"); } - this.dimensionLevelName = dimensionLevelName; + this.levelName = levelName; this.isFlat = isFlat; this.isDebugType = isDebugType; } @@ -42,8 +40,8 @@ public class DimensionInfo { return isFlat; } - public @Nonnull String getDimensionLevelName() { - return dimensionLevelName; + public @Nonnull String getLevelName() { + return levelName; } public @Nonnull String getDimensionIdentifier() { 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 2c917d29f..606f53cc7 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java @@ -1,10 +1,9 @@ package com.velocitypowered.proxy.protocol; -import java.util.HashMap; -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Set; +import java.util.*; 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; @@ -12,70 +11,51 @@ import net.kyori.nbt.TagType; public class DimensionRegistry { - // Mapping: - // dimensionIdentifier (Client connection refers to this), - // dimensionType (The game refers to this). - private final @Nonnull Map dimensionRegistry; - private final @Nonnull Set worldNames; + private final @Nonnull Set dimensionRegistry; + private final @Nonnull String[] levelNames; /** * Initializes a new {@link DimensionRegistry} instance. * This registry is required for 1.16+ clients/servers to communicate, * it constrains the dimension types and names the client can be sent * in a Respawn action (dimension change). - * @param dimensionRegistry a populated map containing dimensionIdentifier and dimensionType sets - * @param worldNames a populated {@link Set} of the dimension level names the server offers + * @param dimensionRegistry a populated set containing dimension data types + * @param levelNames a populated {@link Set} of the dimension level names the server offers */ - public DimensionRegistry(Map dimensionRegistry, - Set worldNames) { + public DimensionRegistry(Set dimensionRegistry, + String[] levelNames) { if (dimensionRegistry == null || dimensionRegistry.isEmpty() - || worldNames == null || worldNames.isEmpty()) { + || levelNames == null || levelNames.length == 0) { throw new IllegalArgumentException( "Dimension registry requires valid arguments, not null and not empty"); } this.dimensionRegistry = dimensionRegistry; - this.worldNames = worldNames; + this.levelNames = levelNames; } - public @Nonnull Map getDimensionRegistry() { + public @Nonnull Set getDimensionRegistry() { return dimensionRegistry; } - public @Nonnull Set getWorldNames() { - return worldNames; + public @Nonnull String[] getLevelNames() { + return levelNames; } /** - * Returns the internal dimension type as used by the game. - * @param dimensionIdentifier how the type is identified by the connection - * @return game internal dimension type + * Returns the internal dimension data type as used by the game. + * @param dimensionIdentifier how the dimension is identified by the connection + * @return game dimension data */ - public @Nonnull String getDimensionType(@Nonnull String dimensionIdentifier) { + public @Nonnull DimensionData getDimensionData(@Nonnull String dimensionIdentifier) { if (dimensionIdentifier == null) { throw new IllegalArgumentException("Dimension identifier cannot be null!"); } - if (dimensionIdentifier == null || !dimensionRegistry.containsKey(dimensionIdentifier)) { - throw new NoSuchElementException("Dimension with identifier " + dimensionIdentifier - + " doesn't exist in this Registry!"); - } - return dimensionRegistry.get(dimensionIdentifier); - } - - /** - * Returns the dimension identifier as used by the client. - * @param dimensionType the internal dimension type - * @return game dimension identifier - */ - public @Nonnull String getDimensionIdentifier(@Nonnull String dimensionType) { - if (dimensionType == null) { - throw new IllegalArgumentException("Dimension type cannot be null!"); - } - for (Map.Entry entry : dimensionRegistry.entrySet()) { - if (entry.getValue().equals(dimensionType)) { - return entry.getKey(); + for (DimensionData iter : dimensionRegistry) { + if(iter.getRegistryIdentifier().equals(dimensionIdentifier)) { + return iter; } } - throw new NoSuchElementException("Dimension type " + dimensionType + throw new NoSuchElementException("Dimension with identifier " + dimensionIdentifier + " doesn't exist in this Registry!"); } @@ -89,11 +69,13 @@ public class DimensionRegistry { throw new IllegalArgumentException("Dimension info cannot be null"); } try { - if (!worldNames.contains(toValidate.getDimensionLevelName())) { - return false; + getDimensionData(toValidate.getDimensionIdentifier()); + for(int i = 0; i < levelNames.length; i++) { + if(levelNames[i].equals(toValidate.getDimensionIdentifier())) { + return true; + } } - getDimensionType(toValidate.getDimensionIdentifier()); - return true; + return false; } catch (NoSuchElementException thrown) { return false; } @@ -103,51 +85,42 @@ public class DimensionRegistry { * Encodes the stored Dimension registry as CompoundTag. * @return the CompoundTag containing identifier:type mappings */ - public CompoundTag encodeToCompoundTag() { + public CompoundTag encodeRegistry() { CompoundTag ret = new CompoundTag(); ListTag list = new ListTag(TagType.COMPOUND); - for (Map.Entry entry : dimensionRegistry.entrySet()) { - CompoundTag item = new CompoundTag(); - item.putString("key", entry.getKey()); - item.putString("element", entry.getValue()); - list.add(item); + for (DimensionData iter : dimensionRegistry) { + list.add(iter.encode()); } ret.put("dimension", list); return ret; } /** - * Decodes a CompoundTag storing dimension mappings to a Map identifier:type. + * Decodes a CompoundTag storing a dimension registry * @param toParse CompoundTag containing a dimension registry + * @param levelNames world level names */ - public static Map parseToMapping(@Nonnull CompoundTag toParse) { + public static DimensionRegistry fromGameData(@Nonnull CompoundTag toParse, @Nonnull String[] levelNames) { if (toParse == null) { throw new IllegalArgumentException("CompoundTag cannot be null"); } + if (levelNames == null || levelNames.length == 0) { + throw new IllegalArgumentException("Level names cannot be null or empty"); + } if (!toParse.contains("dimension", TagType.LIST)) { throw new IllegalStateException("CompoundTag does not contain a dimension List"); } ListTag dimensions = toParse.getList("dimension"); - Map mappings = new HashMap(); + Set mappings = new HashSet(); for (Tag iter : dimensions) { - if (iter instanceof CompoundTag) { + if (!(iter instanceof CompoundTag)) { throw new IllegalStateException("DimensionList in CompoundTag contains an invalid entry"); } - CompoundTag mapping = (CompoundTag) iter; - String key = mapping.getString("key", null); - String element = mapping.getString("element", null); - if (element == null || key == null) { - throw new IllegalStateException("DimensionList in CompoundTag contains an mapping"); - } - if (mappings.containsKey(key) || mappings.containsValue(element)) { - throw new IllegalStateException( - "Dimension mappings may not have identifier/name duplicates"); - } - mappings.put(key, element); + mappings.add(DimensionData.fromNBT((CompoundTag) iter)); } if (mappings.isEmpty()) { throw new IllegalStateException("Dimension mapping cannot be empty"); } - return mappings; + return new DimensionRegistry(mappings, levelNames); } } 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 8ea5cbeb6..64104ebfa 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 @@ -2,12 +2,8 @@ package com.velocitypowered.proxy.protocol.packet; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; -import com.velocitypowered.proxy.protocol.DimensionInfo; -import com.velocitypowered.proxy.protocol.DimensionRegistry; -import com.velocitypowered.proxy.protocol.MinecraftPacket; -import com.velocitypowered.proxy.protocol.ProtocolUtils; +import com.velocitypowered.proxy.protocol.*; import io.netty.buffer.ByteBuf; -import net.kyori.nbt.CompoundTag; import org.checkerframework.checker.nullness.qual.Nullable; import java.util.Map; @@ -140,8 +136,7 @@ public class JoinGame implements MinecraftPacket { String levelName = null; if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { String levelNames[] = ProtocolUtils.readStringArray(buf); - Map dimensionMapping = DimensionRegistry.parseToMapping(ProtocolUtils.readCompoundTag(buf)); - this.dimensionRegistry = new DimensionRegistry(dimensionMapping, Set.of(levelNames)); + this.dimensionRegistry = DimensionRegistry.fromGameData(ProtocolUtils.readCompoundTag(buf), levelNames); dimensionIdentifier = ProtocolUtils.readString(buf); levelName = ProtocolUtils.readString(buf); } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) { @@ -180,10 +175,10 @@ public class JoinGame implements MinecraftPacket { buf.writeInt(entityId); buf.writeByte(gamemode); if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - ProtocolUtils.writeStringArray(buf, dimensionRegistry.getWorldNames().toArray(new String[dimensionRegistry.getWorldNames().size()])); - ProtocolUtils.writeCompoundTag(buf, dimensionRegistry.encodeToCompoundTag()); + ProtocolUtils.writeStringArray(buf, dimensionRegistry.getLevelNames()); + ProtocolUtils.writeCompoundTag(buf, dimensionRegistry.encodeRegistry()); ProtocolUtils.writeString(buf, dimensionInfo.getDimensionIdentifier()); - ProtocolUtils.writeString(buf, dimensionInfo.getDimensionLevelName()); + ProtocolUtils.writeString(buf, dimensionInfo.getLevelName()); } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) { buf.writeInt(dimension); } else { 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 67fafb7f7..74832b5d8 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 @@ -123,7 +123,7 @@ public class Respawn implements MinecraftPacket { 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.getDimensionLevelName()); + ProtocolUtils.writeString(buf, dimensionInfo.getLevelName()); } else { buf.writeInt(dimension); } From aa4a8de2fd474325927be5e221534a4f98a7a046 Mon Sep 17 00:00:00 2001 From: Lechner Markus Date: Fri, 5 Jun 2020 15:45:11 +0200 Subject: [PATCH 5/7] Stylize --- .../proxy/protocol/DimensionData.java | 37 +++++++++++++++---- .../proxy/protocol/DimensionInfo.java | 20 +++++----- .../proxy/protocol/DimensionRegistry.java | 22 ++++++----- .../proxy/protocol/packet/JoinGame.java | 5 +-- .../proxy/protocol/packet/Respawn.java | 2 +- 5 files changed, 54 insertions(+), 32 deletions(-) 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); From 6368b47e78a499b7c1cfada8f1d0913a1ca59c8c Mon Sep 17 00:00:00 2001 From: Lechner Markus Date: Fri, 5 Jun 2020 15:58:34 +0200 Subject: [PATCH 6/7] Old sins --- .../java/com/velocitypowered/proxy/protocol/packet/Respawn.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e3a1a99e0..ba12ee9a3 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 @@ -113,7 +113,7 @@ public class Respawn implements MinecraftPacket { boolean isDebug = buf.readBoolean(); boolean isFlat = buf.readBoolean(); this.dimensionInfo = new DimensionInfo(dimensionIdentifier, levelName, isFlat, isDebug); - shouldKeepPlayerData = buf.readBoolean(); + this.shouldKeepPlayerData = buf.readBoolean(); } else { this.levelType = ProtocolUtils.readString(buf, 16); } From 0377a6829f7b280aa35bb78219cfb86b0321ab64 Mon Sep 17 00:00:00 2001 From: Lechner Markus Date: Fri, 5 Jun 2020 16:00:51 +0200 Subject: [PATCH 7/7] Move to Registry --- .../proxy/connection/backend/VelocityServerConnection.java | 3 +-- .../proxy/connection/client/ClientPlaySessionHandler.java | 2 -- .../proxy/{protocol => connection/registry}/DimensionData.java | 2 +- .../proxy/{protocol => connection/registry}/DimensionInfo.java | 2 +- .../{protocol => connection/registry}/DimensionRegistry.java | 2 +- .../com/velocitypowered/proxy/protocol/packet/JoinGame.java | 2 ++ .../com/velocitypowered/proxy/protocol/packet/Respawn.java | 2 +- 7 files changed, 7 insertions(+), 8 deletions(-) rename proxy/src/main/java/com/velocitypowered/proxy/{protocol => connection/registry}/DimensionData.java (98%) rename proxy/src/main/java/com/velocitypowered/proxy/{protocol => connection/registry}/DimensionInfo.java (96%) rename proxy/src/main/java/com/velocitypowered/proxy/{protocol => connection/registry}/DimensionRegistry.java (98%) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java index 7d1d5832f..dd3fd6e02 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java @@ -12,7 +12,6 @@ import static com.velocitypowered.proxy.network.Connections.READ_TIMEOUT; import com.google.common.base.Preconditions; import com.velocitypowered.api.network.ProtocolVersion; -import com.velocitypowered.api.proxy.ConnectionRequestBuilder; import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import com.velocitypowered.api.proxy.server.ServerInfo; @@ -23,7 +22,7 @@ import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation; import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.connection.util.ConnectionRequestResults.Impl; -import com.velocitypowered.proxy.protocol.DimensionRegistry; +import com.velocitypowered.proxy.connection.registry.DimensionRegistry; import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder; 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 07132bac0..4f64edff7 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 @@ -13,8 +13,6 @@ import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.connection.backend.BackendConnectionPhases; import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; -import com.velocitypowered.proxy.protocol.DimensionInfo; -import com.velocitypowered.proxy.protocol.DimensionRegistry; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.packet.BossBar; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionData.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java similarity index 98% rename from proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionData.java rename to proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java index ed6867e03..cb5df5f2e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionData.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java @@ -1,4 +1,4 @@ -package com.velocitypowered.proxy.protocol; +package com.velocitypowered.proxy.connection.registry; import javax.annotation.Nonnull; import javax.annotation.Nullable; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java similarity index 96% rename from proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java rename to proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java index 42f2980cd..c1c1874d4 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionInfo.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java @@ -1,4 +1,4 @@ -package com.velocitypowered.proxy.protocol; +package com.velocitypowered.proxy.connection.registry; import javax.annotation.Nonnull; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java similarity index 98% rename from proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java rename to proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java index 2bf53ffc1..303ce3aa1 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/DimensionRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java @@ -1,4 +1,4 @@ -package com.velocitypowered.proxy.protocol; +package com.velocitypowered.proxy.connection.registry; import java.util.HashSet; import java.util.NoSuchElementException; 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 56e1d8006..3e1bfe5a6 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 @@ -2,6 +2,8 @@ package com.velocitypowered.proxy.protocol.packet; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; +import com.velocitypowered.proxy.connection.registry.DimensionInfo; +import com.velocitypowered.proxy.connection.registry.DimensionRegistry; import com.velocitypowered.proxy.protocol.*; import io.netty.buffer.ByteBuf; import org.checkerframework.checker.nullness.qual.Nullable; 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 ba12ee9a3..5f883c538 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,7 +2,7 @@ package com.velocitypowered.proxy.protocol.packet; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; -import com.velocitypowered.proxy.protocol.DimensionInfo; +import com.velocitypowered.proxy.connection.registry.DimensionInfo; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf;