feat: Add Velocity version compatibility checking

This commit is contained in:
William278 2024-01-24 19:36:59 +00:00
parent c4a07e1997
commit 384137a67c
6 changed files with 116 additions and 5 deletions

View File

@ -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")
}
}

View File

@ -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

View File

@ -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(), "-");

View File

@ -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

View File

@ -0,0 +1,67 @@
/*
* This file is part of Velocitab, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <will27528@gmail.com>
* 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);
}
}

View File

@ -0,0 +1,2 @@
velocity_api_version: '${velocity_api_version}'
velocity_minimum_build: ${velocity_minimum_build}