Improve protocol version checking (#1203)

* Improve protocol version checking

* chore: since 3.3.0
This commit is contained in:
Riley Park
2024-01-18 17:32:42 -08:00
committed by GitHub
parent 2ac8751337
commit 0993ce2f86
53 changed files with 313 additions and 214 deletions

View File

@@ -18,6 +18,8 @@ java {
}
dependencies {
compileOnlyApi(libs.jspecify)
api(libs.gson)
api(libs.guava)

View File

@@ -100,7 +100,7 @@ public class PlayerResourcePackStatusEvent {
*/
public void setOverwriteKick(boolean overwriteKick) {
Preconditions.checkArgument(player.getProtocolVersion()
.compareTo(ProtocolVersion.MINECRAFT_1_17) < 0,
.lessThan(ProtocolVersion.MINECRAFT_1_17),
"overwriteKick is not supported on 1.17 or newer");
this.overwriteKick = overwriteKick;
}

View File

@@ -10,6 +10,7 @@ package com.velocitypowered.api.network;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.velocitypowered.api.util.Ordered;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
@@ -19,7 +20,7 @@ import java.util.Set;
/**
* Represents each Minecraft protocol version.
*/
public enum ProtocolVersion {
public enum ProtocolVersion implements Ordered<ProtocolVersion> {
UNKNOWN(-1, "Unknown"),
LEGACY(-2, "Legacy"),
MINECRAFT_1_7_2(4,

View File

@@ -9,6 +9,7 @@ package com.velocitypowered.api.proxy.crypto;
import com.google.common.collect.ImmutableSet;
import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.api.util.Ordered;
import java.security.PublicKey;
import java.util.Set;
import java.util.UUID;
@@ -57,7 +58,7 @@ public interface IdentifiedKey extends KeySigned {
/**
* The different versions of player keys, per Minecraft version.
*/
enum Revision {
enum Revision implements Ordered<Revision> {
GENERIC_V1(ImmutableSet.of(), ImmutableSet.of(ProtocolVersion.MINECRAFT_1_19)),
LINKED_V2(ImmutableSet.of(), ImmutableSet.of(ProtocolVersion.MINECRAFT_1_19_1));

View File

@@ -0,0 +1,93 @@
/*
* This file is part of commons, licensed under the MIT License.
*
* Copyright (c) 2021-2024 Seiama
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.velocitypowered.api.util;
import org.jspecify.annotations.NullMarked;
/**
* Something that is ordered.
*
* @param <T> the type
* @since 3.3.0
*/
@NullMarked
@SuppressWarnings("ComparableType") // allows us to be more flexible
public interface Ordered<T> extends Comparable<T> {
/**
* Checks if {@code this} is greater than {@code that}.
*
* @param that the other object
* @return {@code true} if {@code this} is greater than {@code that}, {@code false} otherwise
* @since 3.3.0
*/
default boolean greaterThan(final T that) {
return this.compareTo(that) > 0;
}
/**
* Checks if {@code this} is greater than or equal to {@code that}.
*
* @param that the other object
* @return {@code true} if {@code this} is greater than or
* equal to {@code that}, {@code false} otherwise
* @since 3.3.0
*/
default boolean noLessThan(final T that) {
return this.compareTo(that) >= 0;
}
/**
* Checks if {@code this} is less than {@code that}.
*
* @param that the other object
* @return {@code true} if {@code this} is less than {@code that}, {@code false} otherwise
* @since 3.3.0
*/
default boolean lessThan(final T that) {
return this.compareTo(that) < 0;
}
/**
* Checks if {@code this} is less than or equal to {@code that}.
*
* @param that the other object
* @return {@code true} if {@code this} is less than or
* equal to {@code that}, {@code false} otherwise
* @since 3.3.0
*/
default boolean noGreaterThan(final T that) {
return this.compareTo(that) <= 0;
}
/**
* Checks if {@code this} is equal to {@code that}.
*
* @param that the other object
* @return {@code true} if {@code this} is equal to {@code that}, {@code false} otherwise
* @since 3.3.0
*/
default boolean noGreaterOrLessThan(final T that) {
return this.compareTo(that) == 0;
}
}