Skip to content

Commit

Permalink
Add support for Paper 1.21+ to AnvilInput (#684)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfnalex authored Nov 3, 2024
1 parent c249c09 commit 36ade21
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 69 deletions.
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 3 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
35 changes: 22 additions & 13 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand All @@ -80,13 +80,11 @@ do
esac
done

APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

APP_NAME="Gradle"
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -133,22 +131,29 @@ location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi

# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -193,11 +198,15 @@ if "$cygwin" || "$msys" ; then
done
fi

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
21 changes: 11 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

Expand All @@ -42,11 +43,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -56,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class AnvilInputNMS {
getMethod(ENTITY_PLAYER, "nextContainerCounter", MethodType.methodType(int.class));

GET_PLAYER_INVENTORY =
getMethod(ENTITY_PLAYER, "fN", MethodType.methodType(playerInventoryClass), false, "fR");
getMethod(ENTITY_PLAYER, "fN", MethodType.methodType(playerInventoryClass), false, "fR", "getInventory");

CONTAINER_WINDOW_ID = setField(CONTAINER, int.class, "windowId", "containerId", "j");
ADD_CONTAINER_SLOT_LISTENER = getMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public Object getObject() {
int version = ReflectionUtils.MINOR_NUMBER;
String name = (version == 14 && this == CARTOGRAPHY_TABLE) ? "CARTOGRAPHY" : name();
// Since 1.17, containers go from "a" to "x".
if (version > 16) name = String.valueOf(alphabet[ordinal()]);
if (version > 16 && version <= 20) name = String.valueOf(alphabet[ordinal()]);
Field field = CONTAINERS.getField(name);
return field.get(null);
} catch (ReflectiveOperationException exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package me.devnatan.inventoryframework.runtime.thirdparty;

import org.bukkit.Bukkit;

import java.util.Objects;

public class McVersion implements Comparable<McVersion> {

private static final McVersion CURRENT_VERSION;
private static final McVersion v1_17 = new McVersion(1, 17);
public static final McVersion v1_21_1 = new McVersion(1, 21, 1);

static {
final int currentMajor = Integer.parseInt(Bukkit.getBukkitVersion().split("\\.")[0]);
final int currentMinor = Integer.parseInt(Bukkit.getBukkitVersion().split("\\.")[1].split("-")[0]);
boolean hasPatch = countColons(Bukkit.getBukkitVersion()) == 3;
final int currentPatch = hasPatch ? Integer.parseInt(Bukkit.getBukkitVersion().split("\\.")[2].split("-")[0]) : 0;

CURRENT_VERSION = new McVersion(currentMajor, currentMinor, currentPatch);
}

private static int countColons(final String string) {
int count = 0;
char[] arr = string.toCharArray();
for (int i = 0; i < string.length(); i++) {
if (arr[i] == '.') {
count++;
}
}
return count;
}
private final int major;
private final int minor;
private final int patch;

public McVersion(final int major, final int minor) {
this(major, minor, 0);
}

public McVersion(final int major, final int minor, final int patch) {
this.major = major;
this.minor = minor;
this.patch = patch;
}

/**
* Gets the currently running McVersion
*/
public static McVersion current() {
return CURRENT_VERSION;
}

public boolean hasVersionInNmsPackageName() {
return !this.isAtLeast(v1_17);
}

public boolean isAtLeast(final McVersion other) {
return this.compareTo(other) >= 0;
}

@Override
public int compareTo(final McVersion other) {
if (this.major > other.major) return 3;
if (other.major > this.major) return -3;
if (this.minor > other.minor) return 2;
if (other.minor > this.minor) return -2;
return Integer.compare(this.patch, other.patch);
}

/**
* Gets the "major" part of this McVersion. For 1.16.5, this would be 1
*/
public int getMajor() {
return major;
}

/**
* Gets the "minor" part of this McVersion. For 1.16.5, this would be 16
*/
public int getMinor() {
return minor;
}

/**
* Gets the "patch" part of this McVersion. For 1.16.5, this would be 5.
*/
public int getPatch() {
return patch;
}

@Override
public int hashCode() {
return Objects.hash(major, minor, patch);
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final McVersion mcVersion = (McVersion) o;
return major == mcVersion.major && minor == mcVersion.minor && patch == mcVersion.patch;
}

@Override
public String toString() {
return getName();
}

public String getName() {
if (patch == 0) {
return major + "." + minor;
} else {
return major + "." + minor + "." + patch;
}
}

public boolean isAtLeast(final int major, final int minor, final int patch) {
return this.isAtLeast(new McVersion(major, minor, patch));
}

public boolean isAtLeast(final int major, final int minor) {
return this.isAtLeast(new McVersion(major, minor));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -87,9 +84,14 @@ public final class ReflectionUtils {
}
}
}
if (found == null)
throw new IllegalArgumentException(
"Failed to parse server version. Could not find any package starting with name: 'org.bukkit.craftbukkit.v'");
if (found == null) {
try {
Class.forName("org.bukkit.craftbukkit.entity.CraftPlayer");
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(
"Failed to parse server version. Could not find any package starting with name: 'org.bukkit.craftbukkit.v'");
}
}
NMS_VERSION = found;
}

Expand All @@ -100,7 +102,7 @@ public final class ReflectionUtils {
* @see #supports(int)
* @since 4.0.0
*/
public static final int MINOR_NUMBER;
public static final int MINOR_NUMBER = McVersion.current().getMinor();
/**
* The raw patch version number.
* E.g. {@code v1_17_R1} to {@code 1}
Expand All @@ -112,39 +114,7 @@ public final class ReflectionUtils {
*
* @since 7.0.0
*/
public static final int PATCH_NUMBER;

static {
String[] split = NMS_VERSION.substring(1).split("_");
if (split.length < 1) {
throw new IllegalStateException(
"Version number division error: " + Arrays.toString(split) + ' ' + getVersionInformation());
}

String minorVer = split[1];
try {
MINOR_NUMBER = Integer.parseInt(minorVer);
if (MINOR_NUMBER < 0)
throw new IllegalStateException("Negative minor number? " + minorVer + ' ' + getVersionInformation());
} catch (Throwable ex) {
throw new RuntimeException("Failed to parse minor number: " + minorVer + ' ' + getVersionInformation(), ex);
}

// Bukkit.getBukkitVersion() = "1.12.2-R0.1-SNAPSHOT"
Matcher bukkitVer = Pattern.compile("^\\d+\\.\\d+\\.(\\d+)").matcher(Bukkit.getBukkitVersion());
if (bukkitVer.find()) { // matches() won't work, we just want to match the start using "^"
try {
// group(0) gives the whole matched string, we just want the captured group.
PATCH_NUMBER = Integer.parseInt(bukkitVer.group(1));
} catch (Throwable ex) {
throw new RuntimeException(
"Failed to parse minor number: " + bukkitVer + ' ' + getVersionInformation(), ex);
}
} else {
// 1.8-R0.1-SNAPSHOT
PATCH_NUMBER = 0;
}
}
public static final int PATCH_NUMBER = McVersion.current().getPatch();

/**
* Gets the full version information of the server. Useful for including in errors.
Expand All @@ -160,8 +130,8 @@ public static String getVersionInformation() {
/**
* Mojang remapped their NMS in 1.17: <a href="https://www.spigotmc.org/threads/spigot-bungeecord-1-17.510208/#post-4184317">Spigot Thread</a>
*/
public static final String CRAFTBUKKIT_PACKAGE = "org.bukkit.craftbukkit." + NMS_VERSION + '.',
NMS_PACKAGE = v(17, "net.minecraft.").orElse("net.minecraft.server." + NMS_VERSION + '.');
public static final String CRAFTBUKKIT_PACKAGE = NMS_VERSION != null ? "org.bukkit.craftbukkit." + NMS_VERSION + '.' : "org.bukkit.craftbukkit.";
public static final String NMS_PACKAGE = v(17, "net.minecraft.").orElse("net.minecraft.server." + NMS_VERSION + '.');
/**
* A nullable public accessible field only available in {@code EntityPlayer}.
* This can be null if the player is offline.
Expand Down Expand Up @@ -197,7 +167,8 @@ public static String getVersionInformation() {
final VersionHandler<String> methodVersion;

// MC 1.20.2 obfuscated sendPacket is "b"
if (supportsMC1202()) methodVersion = v(20, "b");
if(McVersion.current().equals(McVersion.v1_21_1)) methodVersion = v(21, "send");
else if (supportsMC1202()) methodVersion = v(20, "b");
else methodVersion = v(18, "a");

sendPacket = lookup.findVirtual(
Expand Down

0 comments on commit 36ade21

Please sign in to comment.