From f868cea5830fac6fec03fa360fd6650d65de0e20 Mon Sep 17 00:00:00 2001 From: "Five (Xer)" Date: Sun, 7 Jun 2020 00:14:23 +0200 Subject: [PATCH 1/3] Move to proper API --- .../backend/VelocityServerConnection.java | 6 +- .../connection/registry/DimensionData.java | 55 +++++---- .../connection/registry/DimensionInfo.java | 34 +++--- .../registry/DimensionRegistry.java | 109 ++++++++---------- 4 files changed, 97 insertions(+), 107 deletions(-) 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 dd3fd6e02..525b5cf26 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 @@ -21,8 +21,8 @@ import com.velocitypowered.proxy.connection.ConnectionTypes; 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.connection.registry.DimensionRegistry; +import com.velocitypowered.proxy.connection.util.ConnectionRequestResults.Impl; import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder; @@ -40,6 +40,8 @@ import io.netty.handler.flow.FlowControlHandler; import io.netty.handler.timeout.ReadTimeoutHandler; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; + +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.Nullable; public class VelocityServerConnection implements MinecraftConnectionAssociation, ServerConnection { @@ -53,7 +55,7 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, private BackendConnectionPhase connectionPhase = BackendConnectionPhases.UNKNOWN; private long lastPingId; private long lastPingSent; - private @Nullable DimensionRegistry activeDimensionRegistry; + private @MonotonicNonNull DimensionRegistry activeDimensionRegistry; /** * Initializes a new server connection. diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java index cb5df5f2e..f6ced21ce 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionData.java @@ -1,19 +1,19 @@ package com.velocitypowered.proxy.connection.registry; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; import net.kyori.nbt.CompoundTag; -public class DimensionData { - private final @Nonnull String registryIdentifier; +public final class DimensionData { + private final 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; + private final Optional fixedTime; + private final Optional hasEnderdragonFight; /** * Initializes a new {@link DimensionData} instance. @@ -27,10 +27,14 @@ public class DimensionData { * @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, + public DimensionData(String registryIdentifier, boolean isNatural, float ambientLight, boolean isShrunk, boolean isUltrawarm, boolean hasCeiling, boolean hasSkylight, - @Nullable Long fixedTime, @Nullable Boolean hasEnderdragonFight) { + Optional fixedTime, Optional hasEnderdragonFight) { + Preconditions.checkNotNull( + registryIdentifier, "registryIdentifier cannot be null"); + Preconditions.checkArgument(registryIdentifier.length() > 0 && !registryIdentifier.isBlank(), + "registryIdentifier cannot be empty"); this.registryIdentifier = registryIdentifier; this.isNatural = isNatural; this.ambientLight = ambientLight; @@ -38,11 +42,13 @@ public class DimensionData { this.isUltrawarm = isUltrawarm; this.hasCeiling = hasCeiling; this.hasSkylight = hasSkylight; - this.fixedTime = fixedTime; - this.hasEnderdragonFight = hasEnderdragonFight; + this.fixedTime = Preconditions.checkNotNull( + fixedTime, "fixedTime optional object cannot be null"); + this.hasEnderdragonFight = Preconditions.checkNotNull( + hasEnderdragonFight, "hasEnderdragonFight optional object cannot be null"); } - public @Nonnull String getRegistryIdentifier() { + public String getRegistryIdentifier() { return registryIdentifier; } @@ -70,11 +76,11 @@ public class DimensionData { return hasSkylight; } - public @Nullable Long getFixedTime() { + public Optional getFixedTime() { return fixedTime; } - public @Nullable Boolean getHasEnderdragonFight() { + public Optional getHasEnderdragonFight() { return hasEnderdragonFight; } @@ -83,10 +89,8 @@ public class DimensionData { * @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"); - } + public static DimensionData decodeCompoundTag(CompoundTag toRead) { + Preconditions.checkNotNull(toRead, "CompoundTag cannot be null"); String registryIdentifier = toRead.getString("key"); CompoundTag values = toRead.getCompound("element"); boolean isNatural = values.getBoolean("natural"); @@ -95,9 +99,12 @@ public class DimensionData { 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; + Optional fixedTime = Optional.fromNullable( + values.contains("fixed_time") + ? values.getLong("fixed_time") : null); + Optional hasEnderdragonFight = Optional.fromNullable( + values.contains("has_enderdragon_fight") + ? values.getBoolean("has_enderdragon_fight") : null); return new DimensionData( registryIdentifier, isNatural, ambientLight, isShrunk, isUltrawarm, hasCeiling, hasSkylight, fixedTime, hasEnderdragonFight); @@ -117,11 +124,11 @@ public class DimensionData { values.putBoolean("ultrawarm", isUltrawarm); values.putBoolean("has_ceiling", hasCeiling); values.putBoolean("has_skylight", hasSkylight); - if (fixedTime != null) { - values.putLong("fixed_time", fixedTime); + if (fixedTime.isPresent()) { + values.putLong("fixed_time", fixedTime.get()); } - if (hasEnderdragonFight != null) { - values.putBoolean("has_enderdragon_fight", hasEnderdragonFight); + if (hasEnderdragonFight.isPresent()) { + values.putBoolean("has_enderdragon_fight", hasEnderdragonFight.get()); } ret.put("element", values); return ret; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java index c1c1874d4..38500ed3a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java @@ -1,11 +1,11 @@ package com.velocitypowered.proxy.connection.registry; -import javax.annotation.Nonnull; +import com.google.common.base.Preconditions; -public class DimensionInfo { +public final class DimensionInfo { - private final @Nonnull String registryIdentifier; - private final @Nonnull String levelName; + private final String registryIdentifier; + private final String levelName; private final boolean isFlat; private final boolean isDebugType; @@ -16,18 +16,18 @@ public class DimensionInfo { * @param isFlat if true will set world lighting below surface-level to not display fog * @param isDebugType if true constrains the world to the very limited debug-type world */ - public DimensionInfo(@Nonnull String registryIdentifier, @Nonnull String levelName, + public DimensionInfo(String registryIdentifier, String levelName, boolean isFlat, boolean isDebugType) { - if (registryIdentifier == null || registryIdentifier.isEmpty() - || registryIdentifier.isBlank()) { - throw new IllegalArgumentException("Dimension registry identifier may not be empty or null"); - } - this.registryIdentifier = registryIdentifier; - if (levelName == null || levelName.isEmpty() - || levelName.isBlank()) { - throw new IllegalArgumentException("dimensions level name may not be empty or null"); - } - this.levelName = levelName; + this.registryIdentifier = Preconditions.checkNotNull( + registryIdentifier, "registryIdentifier cannot be null"); + Preconditions.checkArgument( + registryIdentifier.length() > 0 && registryIdentifier.isBlank(), + "registryIdentifier cannot be empty"); + this.levelName = Preconditions.checkNotNull( + levelName, "levelName cannot be null"); + Preconditions.checkArgument( + levelName.length() > 0 && levelName.isBlank(), + "registryIdentifier cannot be empty"); this.isFlat = isFlat; this.isDebugType = isDebugType; } @@ -40,11 +40,11 @@ public class DimensionInfo { return isFlat; } - public @Nonnull String getLevelName() { + public String getLevelName() { return levelName; } - public @Nonnull String getRegistryIdentifier() { + public String getRegistryIdentifier() { return registryIdentifier; } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java index 303ce3aa1..9e5f1af62 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java @@ -1,63 +1,65 @@ package com.velocitypowered.proxy.connection.registry; -import java.util.HashSet; -import java.util.NoSuchElementException; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; + +import java.util.Map; 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 org.checkerframework.checker.nullness.qual.Nullable; -public class DimensionRegistry { - private final @Nonnull Set dimensionRegistry; - private final @Nonnull String[] levelNames; +public final class DimensionRegistry { + + private final Map registeredDimensions; + private final ImmutableSet 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 set containing dimension data types - * @param levelNames a populated {@link Set} of the dimension level names the server offers + * This WILL raise an IllegalArgumentException if the following is not met: + * - At least one valid DimensionData instance is provided + * - At least one valid world name is provided + * @param registeredDimensions a populated {@link ImmutableSet} containing dimension data types + * @param levelNames a populated {@link ImmutableSet} of the level (world) names the server offers */ - public DimensionRegistry(Set dimensionRegistry, - String[] levelNames) { - if (dimensionRegistry == null || dimensionRegistry.isEmpty() - || levelNames == null || levelNames.length == 0) { - throw new IllegalArgumentException( - "Dimension registry requires valid arguments, not null and not empty"); - } - this.dimensionRegistry = dimensionRegistry; + public DimensionRegistry(ImmutableSet registeredDimensions, + ImmutableSet levelNames) { + Preconditions.checkNotNull(registeredDimensions, + "registeredDimensions cannot be null"); + Preconditions.checkNotNull(levelNames, + "levelNames cannot be null"); + Preconditions.checkArgument(registeredDimensions.size() > 0, + "registeredDimensions needs to be populated"); + Preconditions.checkArgument(levelNames.size() > 0, + "levelNames needs to populated"); + this.registeredDimensions = Maps.uniqueIndex( + registeredDimensions, DimensionData::getRegistryIdentifier); this.levelNames = levelNames; } - public @Nonnull Set getDimensionRegistry() { - return dimensionRegistry; + public Map getRegisteredDimensions() { + return registeredDimensions; } - public @Nonnull String[] getLevelNames() { + public Set getLevelNames() { return levelNames; } /** * 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 + * @return game dimension data or null if not registered */ - public @Nonnull DimensionData getDimensionData(@Nonnull String dimensionIdentifier) { - if (dimensionIdentifier == null) { - throw new IllegalArgumentException("Dimension identifier cannot be null!"); - } - for (DimensionData iter : dimensionRegistry) { - if (iter.getRegistryIdentifier().equals(dimensionIdentifier)) { - return iter; - } - } - throw new NoSuchElementException("Dimension with identifier " + dimensionIdentifier - + " doesn't exist in this Registry!"); + public @Nullable DimensionData getDimensionData(String dimensionIdentifier) { + return registeredDimensions.getOrDefault(dimensionIdentifier, null); } /** @@ -65,21 +67,12 @@ public class DimensionRegistry { * @param toValidate the {@link DimensionInfo} to validate * @return true: the dimension information is valid for this registry */ - public boolean isValidFor(@Nonnull DimensionInfo toValidate) { + public boolean isValidFor(DimensionInfo toValidate) { if (toValidate == null) { - throw new IllegalArgumentException("Dimension info cannot be null"); - } - try { - getDimensionData(toValidate.getRegistryIdentifier()); - for (int i = 0; i < levelNames.length; i++) { - if (levelNames[i].equals(toValidate.getRegistryIdentifier())) { - return true; - } - } - return false; - } catch (NoSuchElementException thrown) { return false; } + return registeredDimensions.containsKey(toValidate.getRegistryIdentifier()) + && levelNames.contains(toValidate.getLevelName()); } /** @@ -89,7 +82,7 @@ public class DimensionRegistry { public CompoundTag encodeRegistry() { CompoundTag ret = new CompoundTag(); ListTag list = new ListTag(TagType.COMPOUND); - for (DimensionData iter : dimensionRegistry) { + for (DimensionData iter : registeredDimensions.values()) { list.add(iter.encodeAsCompundTag()); } ret.put("dimension", list); @@ -99,30 +92,18 @@ public class DimensionRegistry { /** * 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) { - 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"); - } + public static Set fromGameData(CompoundTag toParse) { + Preconditions.checkNotNull(toParse, "CompoundTag cannot be null"); + Preconditions.checkArgument(toParse.contains("dimension", TagType.LIST), + "CompoundTag does not contain a dimension list"); ListTag dimensions = toParse.getList("dimension"); - Set mappings = new HashSet(); + ImmutableSet.Builder mappings = ImmutableSet.builder(); for (Tag iter : dimensions) { - if (!(iter instanceof CompoundTag)) { - throw new IllegalStateException("DimensionList in CompoundTag contains an invalid entry"); + if (iter instanceof CompoundTag) { + mappings.add(DimensionData.decodeCompoundTag((CompoundTag) iter)); } - mappings.add(DimensionData.fromCompoundTag((CompoundTag) iter)); } - if (mappings.isEmpty()) { - throw new IllegalStateException("Dimension mapping cannot be empty"); - } - return new DimensionRegistry(mappings, levelNames); + return mappings.build(); } } From ef5b9cf183da0eda038374f01dc342bbea5d2d40 Mon Sep 17 00:00:00 2001 From: "Five (Xer)" Date: Sun, 7 Jun 2020 00:22:11 +0200 Subject: [PATCH 2/3] Sync to IDE --- .../proxy/connection/registry/DimensionRegistry.java | 2 +- .../proxy/protocol/packet/JoinGame.java | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java index 9e5f1af62..4122d09e3 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionRegistry.java @@ -93,7 +93,7 @@ public final class DimensionRegistry { * Decodes a CompoundTag storing a dimension registry. * @param toParse CompoundTag containing a dimension registry */ - public static Set fromGameData(CompoundTag toParse) { + public static ImmutableSet fromGameData(CompoundTag toParse) { Preconditions.checkNotNull(toParse, "CompoundTag cannot be null"); Preconditions.checkArgument(toParse.contains("dimension", TagType.LIST), "CompoundTag does not contain a dimension list"); 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 3e1bfe5a6..d000320ac 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 @@ -1,7 +1,9 @@ package com.velocitypowered.proxy.protocol.packet; +import com.google.common.collect.ImmutableSet; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; +import com.velocitypowered.proxy.connection.registry.DimensionData; import com.velocitypowered.proxy.connection.registry.DimensionInfo; import com.velocitypowered.proxy.connection.registry.DimensionRegistry; import com.velocitypowered.proxy.protocol.*; @@ -134,8 +136,9 @@ public class JoinGame implements MinecraftPacket { String dimensionIdentifier = null; String levelName = null; if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - String levelNames[] = ProtocolUtils.readStringArray(buf); - this.dimensionRegistry = DimensionRegistry.fromGameData(ProtocolUtils.readCompoundTag(buf), levelNames); + ImmutableSet levelNames = ImmutableSet.copyOf(ProtocolUtils.readStringArray(buf)); + ImmutableSet readData = DimensionRegistry.fromGameData(ProtocolUtils.readCompoundTag(buf)); + this.dimensionRegistry = new DimensionRegistry(readData, levelNames); dimensionIdentifier = ProtocolUtils.readString(buf); levelName = ProtocolUtils.readString(buf); } else if (version.compareTo(ProtocolVersion.MINECRAFT_1_9_1) >= 0) { @@ -174,7 +177,8 @@ public class JoinGame implements MinecraftPacket { buf.writeInt(entityId); buf.writeByte(gamemode); if (version.compareTo(ProtocolVersion.MINECRAFT_1_16) >= 0) { - ProtocolUtils.writeStringArray(buf, dimensionRegistry.getLevelNames()); + ProtocolUtils.writeStringArray(buf, dimensionRegistry.getLevelNames().toArray( + new String[dimensionRegistry.getLevelNames().size()])); ProtocolUtils.writeCompoundTag(buf, dimensionRegistry.encodeRegistry()); ProtocolUtils.writeString(buf, dimensionInfo.getRegistryIdentifier()); ProtocolUtils.writeString(buf, dimensionInfo.getLevelName()); From 3ed5e7718c918bbdded6c9654dc656110966244b Mon Sep 17 00:00:00 2001 From: "Five (Xer)" Date: Sun, 7 Jun 2020 00:33:06 +0200 Subject: [PATCH 3/3] Fix logic error --- .../proxy/connection/registry/DimensionInfo.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java index 38500ed3a..fdfc3dc5d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/registry/DimensionInfo.java @@ -21,12 +21,12 @@ public final class DimensionInfo { this.registryIdentifier = Preconditions.checkNotNull( registryIdentifier, "registryIdentifier cannot be null"); Preconditions.checkArgument( - registryIdentifier.length() > 0 && registryIdentifier.isBlank(), + registryIdentifier.length() > 0 && !registryIdentifier.isBlank(), "registryIdentifier cannot be empty"); this.levelName = Preconditions.checkNotNull( levelName, "levelName cannot be null"); Preconditions.checkArgument( - levelName.length() > 0 && levelName.isBlank(), + levelName.length() > 0 && !levelName.isBlank(), "registryIdentifier cannot be empty"); this.isFlat = isFlat; this.isDebugType = isDebugType;