9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-19 14:59:21 +00:00

feat: support 1.21.8 alongside 1.21.7

adds support for version range expressions to the CompatibilityChecker. Also adds tests for this.
This commit is contained in:
William278
2025-07-20 14:37:05 +01:00
parent 64c81a9a5a
commit 879aef471a
20 changed files with 122 additions and 19 deletions

View File

@@ -327,7 +327,7 @@ public class Settings {
private Map<String, String> eventPriorities = EventListener.ListenerType.getDefaults();
@Comment("Enable check-in petitions for data syncing (don't change this unless you know what you're doing)")
private boolean checkinPetitions = true;
private boolean checkinPetitions = false;
public boolean doAutoPin(@NotNull DataSnapshot.SaveCause cause) {
return autoPinnedSaveCauses.contains(cause.name());

View File

@@ -22,12 +22,13 @@ package net.william278.husksync.util;
import de.exlll.configlib.Configuration;
import de.exlll.configlib.YamlConfigurationProperties;
import de.exlll.configlib.YamlConfigurationStore;
import lombok.AllArgsConstructor;
import net.william278.desertwell.util.Version;
import net.william278.husksync.HuskSync;
import org.jetbrains.annotations.NotNull;
import java.io.InputStream;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.logging.Level;
import static net.william278.husksync.config.ConfigProvider.YAML_CONFIGURATION_PROPERTIES;
@@ -38,23 +39,22 @@ public interface CompatibilityChecker {
default void checkCompatibility() throws HuskSync.FailedToLoadException {
final YamlConfigurationProperties p = YAML_CONFIGURATION_PROPERTIES.build();
final Version compatible;
final CompatibilityConfig compat;
// Load compatibility file
try (InputStream input = getResource(COMPATIBILITY_FILE)) {
final CompatibilityConfig compat = new YamlConfigurationStore<>(CompatibilityConfig.class, p).read(input);
compatible = Objects.requireNonNull(compat.getCompatibleWith());
compat = new YamlConfigurationStore<>(CompatibilityConfig.class, p).read(input);
} catch (Throwable e) {
getPlugin().log(Level.WARNING, "Failed to load compatibility config, skipping check.", e);
return;
}
// Check compatibility
if (compatible.compareTo(getPlugin().getMinecraftVersion()) != 0) {
if (!compat.isCompatibleWith(getPlugin().getMinecraftVersion())) {
throw new HuskSync.FailedToLoadException("""
Incompatible Minecraft version. This version of HuskSync is designed for Minecraft %s.
Please download the correct version of HuskSync for your server's Minecraft version (%s)."""
.formatted(compatible.toString(), getPlugin().getMinecraftVersion().toString()));
.formatted(compat.minecraftVersionRange(), getPlugin().getMinecraftVersion().toString()));
}
}
@@ -64,11 +64,38 @@ public interface CompatibilityChecker {
HuskSync getPlugin();
@Configuration
record CompatibilityConfig(@NotNull String minecraftVersion) {
record CompatibilityConfig(@NotNull String minecraftVersionRange) {
@NotNull
public Version getCompatibleWith() {
return Version.fromString(minecraftVersion);
@AllArgsConstructor
enum ExpressionType {
GTE(">=", (v, s) -> v.compareTo(Version.fromString(s.substring(2))) >= 0),
LTE("<=", (v, s) -> v.compareTo(Version.fromString(s.substring(2))) <= 0),
GT(">", (v, s) -> v.compareTo(Version.fromString(s.substring(1))) > 0),
LT("<", (v, s) -> v.compareTo(Version.fromString(s.substring(1))) < 0),
NOT("!", (v, s) -> v.compareTo(Version.fromString(s.substring(1))) != 0),
E("=", (v, s) -> v.compareTo(Version.fromString(s.substring(1))) == 0);
private final String match;
private final BiFunction<Version, String, Boolean> function;
private static boolean check(@NotNull String versionRange, @NotNull Version mcVer) {
boolean passes = true;
versions:
for (String exp : versionRange.split(" ")) {
for (ExpressionType type : values()) {
if (exp.trim().startsWith(type.match)) {
passes = passes && type.function.apply(mcVer, exp.trim());
continue versions;
}
}
passes = passes && mcVer.compareTo(Version.fromString(exp.trim())) == 0;
}
return passes;
}
}
public boolean isCompatibleWith(@NotNull Version version) {
return ExpressionType.check(minecraftVersionRange, version);
}
}

View File

@@ -0,0 +1,59 @@
/*
* This file is part of HuskSync, 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.husksync.util;
import net.william278.desertwell.util.Version;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
@DisplayName("Compatibility Checker Tests")
public class CompatibilityCheckerTests {
@ParameterizedTest(name = "Ver: {0}, Range: {1}")
@DisplayName("Test Compatibility Checker")
@CsvSource({
"1.20.1, 1.21.1, false",
"1.21.1, 1.20.1, false",
"1.7.2, 1.21.5, false",
"1.19.4, 1.21.1, false",
"1.21.3, 1.21.3, true",
"1.20.1, 1.20.1, true",
"1.21.7, 1.21.7, true",
"1.21.8, >=1.21.7, true",
"1.21.8, >1.21.7, true",
"1.0, <1.21.7, true",
"1.17.1, !1.17.1, false",
"1.21.7, '>=1.21.7 <=1.21.8', true",
"1.21.8, '>=1.21.7 <=1.21.8', true",
"1.21.5, '>=1.21.7 <=1.21.8', false",
})
public void testCompatibilityChecker(@NotNull String mcVer, @NotNull String range, boolean exp) {
final Version version = Version.fromString(mcVer);
Assertions.assertNotNull(version, "Version should not be null");
final CompatibilityChecker.CompatibilityConfig config = new CompatibilityChecker.CompatibilityConfig(range);
Assertions.assertEquals(exp, config.isCompatibleWith(version), "Checker should return " + exp);
}
}