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(); } }