From 384137a67c5244445927f213910f63bb4feee66a Mon Sep 17 00:00:00 2001 From: William278 Date: Wed, 24 Jan 2024 19:36:59 +0000 Subject: [PATCH] feat: Add Velocity version compatibility checking --- build.gradle | 10 ++- gradle.properties | 5 +- .../net/william278/velocitab/Velocitab.java | 9 ++- .../velocitab/config/ConfigProvider.java | 28 ++++++++ .../william278/velocitab/config/Metadata.java | 67 +++++++++++++++++++ src/main/resources/metadata.yml | 2 + 6 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 src/main/java/net/william278/velocitab/config/Metadata.java create mode 100644 src/main/resources/metadata.yml diff --git a/build.gradle b/build.gradle index bc323ff..26d0f0c 100644 --- a/build.gradle +++ b/build.gradle @@ -17,6 +17,9 @@ defaultTasks 'licenseFormat', 'build' ext { set 'version', version.toString() set 'description', description.toString() + + set 'velocity_api_version', velocity_api_version.toString() + set 'velocity_minimum_build', velocity_minimum_build.toString() } repositories { @@ -29,8 +32,9 @@ repositories { } dependencies { - compileOnly 'com.velocitypowered:velocity-api:3.3.0-SNAPSHOT' - compileOnly 'com.velocitypowered:velocity-proxy:3.3.0-SNAPSHOT' + compileOnly "com.velocitypowered:velocity-api:${velocity_api_version}-SNAPSHOT" + compileOnly "com.velocitypowered:velocity-proxy:${velocity_api_version}-SNAPSHOT" + compileOnly 'io.netty:netty-codec-http:4.1.105.Final' compileOnly 'org.projectlombok:lombok:1.18.30' compileOnly 'net.luckperms:api:5.4' @@ -146,7 +150,7 @@ publishing { tasks { runVelocity { - velocityVersion("3.3.0-SNAPSHOT") + velocityVersion("${velocity_api_version}-SNAPSHOT") } } diff --git a/gradle.properties b/gradle.properties index a3790b4..ac20ad1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,6 +3,9 @@ javaVersion=17 org.gradle.jvmargs='-Dfile.encoding=UTF-8' org.gradle.daemon=true -plugin_version=1.6.1 +plugin_version=1.6.2 plugin_archive=velocitab plugin_description=A beautiful and versatile TAB list plugin for Velocity proxies + +velocity_api_version=3.3.0 +velocity_minimum_build=329 \ No newline at end of file diff --git a/src/main/java/net/william278/velocitab/Velocitab.java b/src/main/java/net/william278/velocitab/Velocitab.java index 02a3cdf..baef0ae 100644 --- a/src/main/java/net/william278/velocitab/Velocitab.java +++ b/src/main/java/net/william278/velocitab/Velocitab.java @@ -61,13 +61,14 @@ import java.util.List; import java.util.Optional; @Plugin(id = "velocitab") -@SuppressWarnings("unused") @Getter public class Velocitab implements ConfigProvider, ScoreboardProvider, LoggerProvider, HookProvider, MetricProvider { + @Setter private Settings settings; @Setter private TabGroups tabGroups; + private final ProxyServer server; private final Logger logger; private final Path configDirectory; @@ -95,6 +96,7 @@ public class Velocitab implements ConfigProvider, ScoreboardProvider, LoggerProv @Subscribe public void onProxyInitialization(@NotNull ProxyInitializeEvent event) { + checkCompatibility(); loadConfigs(); loadHooks(); prepareVanishManager(); @@ -161,6 +163,11 @@ public class Velocitab implements ConfigProvider, ScoreboardProvider, LoggerProv return pluginContainer.getDescription(); } + @NotNull + public Version getVelocityVersion() { + return Version.fromString(server.getVersion().getVersion(), "-"); + } + @NotNull public Version getVersion() { return Version.fromString(getDescription().getVersion().orElseThrow(), "-"); diff --git a/src/main/java/net/william278/velocitab/config/ConfigProvider.java b/src/main/java/net/william278/velocitab/config/ConfigProvider.java index 13e2a96..ceeece0 100644 --- a/src/main/java/net/william278/velocitab/config/ConfigProvider.java +++ b/src/main/java/net/william278/velocitab/config/ConfigProvider.java @@ -22,9 +22,14 @@ package net.william278.velocitab.config; import de.exlll.configlib.NameFormatters; import de.exlll.configlib.YamlConfigurationProperties; import de.exlll.configlib.YamlConfigurations; +import net.william278.desertwell.util.Version; import org.jetbrains.annotations.NotNull; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; import java.nio.file.Path; +import java.util.Objects; /** * Interface for getting and setting data from plugin configuration files @@ -99,7 +104,30 @@ public interface ConfigProvider { getTabGroups().validateConfig(); } + /** + * Load the tab groups from the config file + * + * @since 1.0 + */ + @NotNull + default Metadata getMetadata() { + final URL resource = ConfigProvider.class.getResource("/metadata.yml"); + try (InputStream input = Objects.requireNonNull(resource, "Metadata file missing").openStream()) { + return YamlConfigurations.read(input, Metadata.class, YAML_CONFIGURATION_PROPERTIES.build()); + } catch (IOException e) { + throw new IllegalStateException("Unable to load plugin metadata", e); + } + } + default void checkCompatibility() { + final Metadata metadata = getMetadata(); + final Version proxyVersion = getVelocityVersion(); + metadata.validateApiVersion(proxyVersion); + metadata.validateBuild(proxyVersion); + } + + @NotNull + Version getVelocityVersion(); /** * Get the plugin config directory diff --git a/src/main/java/net/william278/velocitab/config/Metadata.java b/src/main/java/net/william278/velocitab/config/Metadata.java new file mode 100644 index 0000000..815e2b4 --- /dev/null +++ b/src/main/java/net/william278/velocitab/config/Metadata.java @@ -0,0 +1,67 @@ +/* + * This file is part of Velocitab, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.william278.velocitab.config; + +import de.exlll.configlib.Configuration; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import net.william278.desertwell.util.Version; +import org.jetbrains.annotations.NotNull; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +@SuppressWarnings("unused") +@Configuration +@Getter +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class Metadata { + + private String velocityApiVersion; + private int velocityMinimumBuild; + + public void validateApiVersion(@NotNull Version version) { + if (version.compareTo(Version.fromString(velocityApiVersion)) < 0) { + final String serverVersion = version.toStringWithoutMetadata(); + throw new IllegalStateException("Your Velocity API version (" + serverVersion + ") is not supported! " + + "Disabling Velocitab. Please update to at least Velocity v" + velocityApiVersion + + " build #" + velocityMinimumBuild + " or newer."); + } + } + + public void validateBuild(@NotNull Version version) { + int serverBuild = getBuildNumber(version.toString()); + if (serverBuild < velocityMinimumBuild) { + throw new IllegalStateException("Your Velocity build version (#" + serverBuild + ") is not supported! " + + "Disabling Velocitab. Please update to at least Velocity v" + velocityApiVersion + + " build #" + velocityMinimumBuild + " or newer."); + } + } + + private int getBuildNumber(@NotNull String proxyVersion) { + final Matcher matcher = Pattern.compile(".*-b(\\d+).*").matcher(proxyVersion); + if (matcher.find(1)) { + return Integer.parseInt(matcher.group(1)); + } + throw new IllegalArgumentException("No build number found for proxy version: " + proxyVersion); + } + +} diff --git a/src/main/resources/metadata.yml b/src/main/resources/metadata.yml new file mode 100644 index 0000000..9fa88f5 --- /dev/null +++ b/src/main/resources/metadata.yml @@ -0,0 +1,2 @@ +velocity_api_version: '${velocity_api_version}' +velocity_minimum_build: ${velocity_minimum_build} \ No newline at end of file