From ee5e1a8089ece27e2e872f3d64f4e767e5e0efab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurice=20Eisenbl=C3=A4tter?= Date: Tue, 7 May 2024 23:13:30 +0200 Subject: [PATCH] fix: removal of bukkit package remapping in paper (#370) * fix: removale of bukkit package remapping in paper * fix: github ci --- .github/workflows/buildtools.sh | 2 +- .../orebfuscator/util/MinecraftVersion.java | 170 ++++++++++++++++-- .../orebfuscator-nms-v1_20_R4/pom.xml | 10 +- .../orebfuscator/chunk/ChunkCapabilities.java | 21 ++- 4 files changed, 167 insertions(+), 36 deletions(-) diff --git a/.github/workflows/buildtools.sh b/.github/workflows/buildtools.sh index bbfad0c2..102091c7 100644 --- a/.github/workflows/buildtools.sh +++ b/.github/workflows/buildtools.sh @@ -44,4 +44,4 @@ checkVersion "1.19.4" "17" checkVersion "1.20.1" "17" checkVersion "1.20.2" "17" checkVersion "1.20.4" "17" -checkVersion "1.20.5" "21" +checkVersion "1.20.6" "21" diff --git a/orebfuscator-common/src/main/java/net/imprex/orebfuscator/util/MinecraftVersion.java b/orebfuscator-common/src/main/java/net/imprex/orebfuscator/util/MinecraftVersion.java index 17a1e5ab..74944552 100644 --- a/orebfuscator-common/src/main/java/net/imprex/orebfuscator/util/MinecraftVersion.java +++ b/orebfuscator-common/src/main/java/net/imprex/orebfuscator/util/MinecraftVersion.java @@ -1,31 +1,63 @@ package net.imprex.orebfuscator.util; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.bukkit.Bukkit; -public final class MinecraftVersion { +public final class MinecraftVersion implements Comparable { - private static final Pattern VERSION_PATTERN = Pattern.compile("org\\.bukkit\\.craftbukkit\\.(v(\\d+)_(\\d+)_R(\\d+))"); + private static final class NmsMapping { - private static final String NMS_VERSION; - private static final int MAJOR_VERSION; - private static final int MINOR_VERSION; - private static final int REVISION_NUMBER; + private static final List MAPPINGS = new ArrayList<>(); + + static { + MAPPINGS.add(new NmsMapping("1.20.5", "v1_20_R4")); + } + + public static String get(MinecraftVersion version) { + for (NmsMapping mapping : MAPPINGS) { + if (version.isAtOrAbove(mapping.version)) { + if (mapping.version.minor() != version.minor()) { + OFCLogger.warn(String.format("Using nms mapping with mismatched minor versions: %s - %s", + mapping.version, version)); + } + + return mapping.nmsVersion; + } + } + + throw new RuntimeException("Can't get nms package version for minecraft version: " + version); + } + + private final MinecraftVersion version; + private final String nmsVersion; + + public NmsMapping(String version, String nmsVersion) { + this.version = new MinecraftVersion(version); + this.nmsVersion = nmsVersion; + } + } + + private static final Pattern VERSION_PATTERN = Pattern.compile("(?\\d+)(?:\\.(?\\d+))(?:\\.(?\\d+))?"); + private static final Pattern PACKAGE_PATTERN = Pattern.compile("org\\.bukkit\\.craftbukkit\\.(v\\d+_\\d+_R\\d+)"); + + private static final MinecraftVersion CURRENT_VERSION = new MinecraftVersion(Bukkit.getBukkitVersion()); + + private static String NMS_VERSION; static { String craftBukkitPackage = Bukkit.getServer().getClass().getPackage().getName(); - Matcher matcher = VERSION_PATTERN.matcher(craftBukkitPackage); + Matcher matcher = PACKAGE_PATTERN.matcher(craftBukkitPackage); - if (!matcher.find()) { - throw new RuntimeException("Can't parse craftbukkit package version " + craftBukkitPackage); + if (matcher.find()) { + NMS_VERSION = matcher.group(1); + } else { + NMS_VERSION = NmsMapping.get(CURRENT_VERSION); } - - NMS_VERSION = matcher.group(1); - MAJOR_VERSION = Integer.parseInt(matcher.group(2)); - MINOR_VERSION = Integer.parseInt(matcher.group(3)); - REVISION_NUMBER = Integer.parseInt(matcher.group(4)); } public static String nmsVersion() { @@ -33,17 +65,117 @@ public static String nmsVersion() { } public static int majorVersion() { - return MAJOR_VERSION; + return CURRENT_VERSION.major; } public static int minorVersion() { - return MINOR_VERSION; + return CURRENT_VERSION.minor; + } + + public static int patchVersion() { + return CURRENT_VERSION.patch; + } + + public static boolean isAbove(String version) { + return CURRENT_VERSION.isAbove(new MinecraftVersion(version)); + } + + public static boolean isAtOrAbove(String version) { + return CURRENT_VERSION.isAtOrAbove(new MinecraftVersion(version)); + } + + public static boolean isAtOrBelow(String version) { + return CURRENT_VERSION.isAtOrBelow(new MinecraftVersion(version)); + } + + public static boolean isBelow(String version) { + return CURRENT_VERSION.isBelow(new MinecraftVersion(version)); + } + + private final int major; + private final int minor; + private final int patch; + + public MinecraftVersion(String version) { + Matcher matcher = VERSION_PATTERN.matcher(version); + + if (!matcher.find()) { + throw new IllegalArgumentException("can't parse minecraft version: " + version); + } + + this.major = Integer.parseInt(matcher.group("major")); + this.minor = Integer.parseInt(matcher.group("minor")); + + String patch = matcher.group("patch"); + if (patch != null) { + this.patch = Integer.parseInt(patch); + } else { + this.patch = 0; + } + } + + public int major() { + return this.major; + } + + public int minor() { + return this.minor; + } + + public int patch() { + return this.patch; + } + + public boolean isAbove(MinecraftVersion version) { + return this.compareTo(version) > 0; + } + + public boolean isAtOrAbove(MinecraftVersion version) { + return this.compareTo(version) >= 0; } - public static int revisionNumber() { - return REVISION_NUMBER; + public boolean isAtOrBelow(MinecraftVersion version) { + return this.compareTo(version) <= 0; + } + + public boolean isBelow(MinecraftVersion version) { + return this.compareTo(version) < 0; + } + + @Override + public int compareTo(MinecraftVersion other) { + int major = Integer.compare(this.major, other.major); + if (major != 0) { + return major; + } + + int minor = Integer.compare(this.minor, other.minor); + if (minor != 0) { + return minor; + } + + return Integer.compare(this.patch, other.patch); + } + + @Override + public int hashCode() { + return Objects.hash(major, minor, patch); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof MinecraftVersion)) { + return false; + } + MinecraftVersion other = (MinecraftVersion) obj; + return major == other.major && minor == other.minor && patch == other.patch; } - private MinecraftVersion() { + @Override + public String toString() { + return String.format("%s.%s.%s", this.major, this.minor, this.patch); } } diff --git a/orebfuscator-nms/orebfuscator-nms-v1_20_R4/pom.xml b/orebfuscator-nms/orebfuscator-nms-v1_20_R4/pom.xml index d5bb3333..2458a40d 100644 --- a/orebfuscator-nms/orebfuscator-nms-v1_20_R4/pom.xml +++ b/orebfuscator-nms/orebfuscator-nms-v1_20_R4/pom.xml @@ -20,7 +20,7 @@ org.spigotmc spigot - 1.20.5-R0.1-SNAPSHOT + 1.20.6-R0.1-SNAPSHOT remapped-mojang provided @@ -54,9 +54,9 @@ remap-obf - org.spigotmc:minecraft-server:1.20.5-R0.1-SNAPSHOT:txt:maps-mojang + org.spigotmc:minecraft-server:1.20.6-R0.1-SNAPSHOT:txt:maps-mojang true - org.spigotmc:spigot:1.20.5-R0.1-SNAPSHOT:jar:remapped-mojang + org.spigotmc:spigot:1.20.6-R0.1-SNAPSHOT:jar:remapped-mojang true remapped-obf @@ -69,8 +69,8 @@ remap-spigot ${project.build.directory}/${project.artifactId}-${project.version}-remapped-obf.jar - org.spigotmc:minecraft-server:1.20.5-R0.1-SNAPSHOT:csrg:maps-spigot - org.spigotmc:spigot:1.20.5-R0.1-SNAPSHOT:jar:remapped-obf + org.spigotmc:minecraft-server:1.20.6-R0.1-SNAPSHOT:csrg:maps-spigot + org.spigotmc:spigot:1.20.6-R0.1-SNAPSHOT:jar:remapped-obf diff --git a/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/chunk/ChunkCapabilities.java b/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/chunk/ChunkCapabilities.java index 6bc20a9d..0a123d86 100644 --- a/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/chunk/ChunkCapabilities.java +++ b/orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/chunk/ChunkCapabilities.java @@ -15,17 +15,16 @@ public final class ChunkCapabilities { // hasDirectPaletteZeroLength < 1.13 // hasLight < 1.14 - private static final boolean hasChunkPosFieldUnloadPacket = MinecraftVersion.minorVersion() > 20 || - (MinecraftVersion.minorVersion() == 20 && MinecraftVersion.revisionNumber() >= 2); - private static final boolean hasClientboundLevelChunkPacketData = MinecraftVersion.minorVersion() >= 18; - private static final boolean hasBiomePalettedContainer = MinecraftVersion.minorVersion() >= 18; - private static final boolean hasSingleValuePalette = MinecraftVersion.minorVersion() >= 18; - private static final boolean hasHeightBitMask = MinecraftVersion.minorVersion() <= 17; - private static final boolean hasDynamicHeight = MinecraftVersion.minorVersion() >= 17; - private static final boolean hasSimpleVarBitBuffer = MinecraftVersion.minorVersion() >= 16; - private static final boolean hasBlockCount = MinecraftVersion.minorVersion() >= 14; - private static final boolean hasDirectPaletteZeroLength = MinecraftVersion.minorVersion() < 13; - private static final boolean hasLightArray = MinecraftVersion.minorVersion() < 14; + private static final boolean hasChunkPosFieldUnloadPacket = MinecraftVersion.isAtOrAbove("1.20.2"); + private static final boolean hasClientboundLevelChunkPacketData = MinecraftVersion.isAtOrAbove("1.18"); + private static final boolean hasBiomePalettedContainer = MinecraftVersion.isAtOrAbove("1.18"); + private static final boolean hasSingleValuePalette = MinecraftVersion.isAtOrAbove("1.18"); + private static final boolean hasHeightBitMask = MinecraftVersion.isAtOrBelow("1.17"); + private static final boolean hasDynamicHeight = MinecraftVersion.isAtOrAbove("1.17"); + private static final boolean hasSimpleVarBitBuffer = MinecraftVersion.isAtOrAbove("1.16"); + private static final boolean hasBlockCount = MinecraftVersion.isAtOrAbove("1.14"); + private static final boolean hasDirectPaletteZeroLength = MinecraftVersion.isBelow("1.13"); + private static final boolean hasLightArray = MinecraftVersion.isBelow("1.14"); private ChunkCapabilities() { }