Skip to content

Commit

Permalink
feat: 1.20.4対応
Browse files Browse the repository at this point in the history
  • Loading branch information
Maru32768 committed Jun 2, 2024
1 parent a574352 commit ecf5566
Show file tree
Hide file tree
Showing 63 changed files with 3,104 additions and 19 deletions.
18 changes: 18 additions & 0 deletions .run/RunTestPlugin_1.20.4.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="RunTestPlugin_1.20.4" type="JarApplication">
<option name="JAR_PATH" value="$PROJECT_DIR$/bukkit/test_plugin_1.20.4/server/server.jar"/>
<option name="VM_PARAMETERS" value="-Dplugin.env=DEV"/>
<option name="PROGRAM_PARAMETERS" value="nogui"/>
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/bukkit/test_plugin_1.20.4/server"/>
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true"/>
<option name="ALTERNATIVE_JRE_PATH" value="17"/>
<method v="2">
<option name="Gradle.BeforeRunTask" enabled="false" tasks="downloadServerJar"
externalProjectPath="$PROJECT_DIR$/bukkit/test_plugin_1.20.4" vmOptions="" scriptParameters=""/>
<option name="Gradle.BeforeRunTask" enabled="true" tasks="build"
externalProjectPath="$PROJECT_DIR$/bukkit/test_plugin_1.20.4" vmOptions="" scriptParameters=""/>
<option name="Gradle.BeforeRunTask" enabled="true" tasks="copyToServer"
externalProjectPath="$PROJECT_DIR$/bukkit/test_plugin_1.20.4" vmOptions="" scriptParameters=""/>
</method>
</configuration>
</component>
18 changes: 18 additions & 0 deletions .run/RunTestPlugin_1.20.6.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="RunTestPlugin_1.20.6" type="JarApplication">
<option name="JAR_PATH" value="$PROJECT_DIR$/bukkit/test_plugin_1.20.6/server/server.jar"/>
<option name="VM_PARAMETERS" value="-Dplugin.env=DEV"/>
<option name="PROGRAM_PARAMETERS" value="nogui"/>
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/bukkit/test_plugin_1.20.6/server"/>
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true"/>
<option name="ALTERNATIVE_JRE_PATH" value="21"/>
<method v="2">
<option name="Gradle.BeforeRunTask" enabled="false" tasks="downloadServerJar"
externalProjectPath="$PROJECT_DIR$/bukkit/test_plugin_1.20.6" vmOptions="" scriptParameters=""/>
<option name="Gradle.BeforeRunTask" enabled="true" tasks="build"
externalProjectPath="$PROJECT_DIR$/bukkit/test_plugin_1.20.6" vmOptions="" scriptParameters=""/>
<option name="Gradle.BeforeRunTask" enabled="true" tasks="copyToServer"
externalProjectPath="$PROJECT_DIR$/bukkit/test_plugin_1.20.6" vmOptions="" scriptParameters=""/>
</method>
</configuration>
</component>
7 changes: 7 additions & 0 deletions bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ dependencies {
compileOnly "com.mojang:brigadier:1.0.18"
// compileOnly fileTree(dir: "./test_plugin_1.16.5/", include: "server/cache/patched*.jar")
// compileOnly fileTree(dir: "./test_plugin_1.19.4/", include: ["server/versions/1.19.4/paper*.jar", "server_mojmap/versions/1.19.4/paper*jar"])

testImplementation "org.junit.jupiter:junit-jupiter:5.8.1"
testImplementation "org.assertj:assertj-core:3.25.1"
}

def targetJavaVersion = 8
Expand All @@ -27,6 +30,10 @@ java {
}
}

test {
useJUnitPlatform()
}

shadowJar {
relocate "org.apache", "${project.group}.${rootProject.name.toLowerCase()}.shadow.org.apache"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import java.lang.reflect.Method;

public class ReflectionUtil {
private ReflectionUtil() {
}

public static Field getFieldIncludingSuperclasses(Class<?> clazz, String name) throws NoSuchFieldException {
Field field;

Expand Down Expand Up @@ -40,4 +37,7 @@ public static Method getMethodIncludingSuperclasses(Class<?> clazz,

return method;
}

private ReflectionUtil() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package net.kunmc.lab.commandlib.util.bukkit;

import org.bukkit.Bukkit;

import java.util.regex.Pattern;

public class VersionUtil {
private static final Pattern MINECRAFT_VERSION_PATTERN = Pattern.compile("\\d+.\\d+.\\d+");

private static void checkMinecraftVersionFormat(String s) {
if (!MINECRAFT_VERSION_PATTERN.matcher(s)
.matches()) {
throw new InvalidVersionFormatException("'" + s + "' is not satisfies the version format '\\d+.\\d+.\\d+'");
}
}

public static boolean is1_20_x() {
return compareMinecraftVersion(Bukkit.getMinecraftVersion(),
"1.19.99999") > 0 && compareMinecraftVersion(Bukkit.getMinecraftVersion(),
"1.21.0") < 0;
}

/**
* Compares two Minecraft Version values numerically.
*
* @param x the first Minecraft Version to compare
* @param y the second Minecraft Version to compare
* @return the value {@code 0} if {@code x == y};
* a value less than {@code 0} if {@code x < y}; and
* a value greater than {@code 0} if {@code x > y}
*/
public static int compareMinecraftVersion(String x, String y) {
checkMinecraftVersionFormat(x);
checkMinecraftVersionFormat(y);

String[] xUnits = x.split("\\.");
String[] yUnits = y.split("\\.");
for (int i = 0; i < xUnits.length; i++) {
int xn = Integer.parseInt(xUnits[i]);
int yn = Integer.parseInt(yUnits[i]);

if (xn < yn) {
return -1;
}
if (xn > yn) {
return 1;
}
}

return 0;
}

public static class InvalidVersionFormatException extends RuntimeException {
public InvalidVersionFormatException(String message) {
super(message);
}
}

private VersionUtil() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ protected final Object invokeMethod(String[] methodNames, Object... args) {
return invokeMethod(methodNames, argClasses, args);
}

protected final Object invokeMethod(String methodName, Class<?>[] parameterClasses, Object... args) {
return invokeMethod(new String[]{methodName}, parameterClasses, args);
}

protected final Object invokeMethod(String[] methodNames, Class<?>[] parameterClasses, Object... args) {
Method method = null;
for (String methodName : methodNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,26 @@ public class NMSReflection {
.getClassLoader();

static {
VERSION = Bukkit.getServer()
String ver;
try {
ver = Bukkit.getServer()
.getClass()
.getPackage()
.getName()
.split("\\.")[3];
NMS_PACKAGE_PREFIX = "net.minecraft.server." + VERSION;
CRAFT_BUKKIT_PACKAGE_PREFIX = "org.bukkit.craftbukkit." + VERSION;
} catch (ArrayIndexOutOfBoundsException e) {
// 1.20.6からはバージョンがパッケージ名から消えていた
ver = null;
}
VERSION = ver;

if (VERSION != null) {
NMS_PACKAGE_PREFIX = "net.minecraft.server." + VERSION;
CRAFT_BUKKIT_PACKAGE_PREFIX = "org.bukkit.craftbukkit." + VERSION;
} else {
NMS_PACKAGE_PREFIX = "net.minecraft.server";
CRAFT_BUKKIT_PACKAGE_PREFIX = "org.bukkit.craftbukkit";
}
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import net.kunmc.lab.commandlib.util.bukkit.VersionUtil;
import net.kunmc.lab.commandlib.util.nms.command.NMSCommandBuildContext;
import net.kunmc.lab.commandlib.util.nms.core.NMSHolder;
import net.kunmc.lab.commandlib.util.nms.core.NMSRegistries;
Expand Down Expand Up @@ -32,6 +33,10 @@ public ArgumentType<?> argument() {

@Override
protected NMSEnchantment parseImpl(CommandContext<?> ctx, String name) {
if (VersionUtil.is1_20_x()) {
return new NMSEnchantment(new NMSHolder.NMSReference(invokeMethod("g", ctx, name)).value());
}

try {
return new NMSEnchantment(invokeMethod("a", ctx, name));
} catch (MethodNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import net.kunmc.lab.commandlib.util.bukkit.VersionUtil;
import net.kunmc.lab.commandlib.util.nms.command.NMSCommandBuildContext;
import net.kunmc.lab.commandlib.util.nms.core.NMSHolder;
import net.kunmc.lab.commandlib.util.nms.core.NMSRegistries;
Expand Down Expand Up @@ -32,6 +33,10 @@ public ArgumentType<?> argument() {

@Override
protected NMSMobEffectList parseImpl(CommandContext<?> ctx, String name) {
if (VersionUtil.is1_20_x()) {
return new NMSMobEffectList(new NMSHolder.NMSReference(invokeMethod("f", ctx, name)).value());
}

try {
return new NMSMobEffectList(invokeMethod("a", ctx, name));
} catch (MethodNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.kunmc.lab.commandlib.util.nms.chat;

import net.kunmc.lab.commandlib.util.bukkit.VersionUtil;
import net.kunmc.lab.commandlib.util.nms.MinecraftClass;

public class NMSTranslatableContents extends MinecraftClass {
Expand All @@ -8,10 +9,18 @@ public NMSTranslatableContents(Object handle) {
}

public String getKey() {
if (VersionUtil.is1_20_x()) {
return ((String) invokeMethod("b"));
}

return ((String) invokeMethod("a", "getKey"));
}

public Object[] getArgs() {
if (VersionUtil.is1_20_x()) {
return ((Object[]) invokeMethod("d"));
}

return ((Object[]) invokeMethod("c", "getArgs"));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.kunmc.lab.commandlib.util.nms.core;

import net.kunmc.lab.commandlib.util.bukkit.VersionUtil;
import net.kunmc.lab.commandlib.util.nms.MinecraftClass;
import net.kunmc.lab.commandlib.util.nms.resources.NMSResourceKey;

Expand All @@ -9,10 +10,18 @@ public NMSRegistries() {
}

public NMSResourceKey enchantment() {
if (VersionUtil.is1_20_x()) {
return new NMSResourceKey(getValue(Object.class, "t", "ENCHANTMENT"));
}

return new NMSResourceKey(getValue(Object.class, "q", "ENCHANTMENT"));
}

public NMSResourceKey mobEffect() {
if (VersionUtil.is1_20_x()) {
return new NMSResourceKey(getValue(Object.class, "Q", "MOB_EFFECT"));
}

return new NMSResourceKey(getValue(Object.class, "N", "MOB_EFFECT"));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.kunmc.lab.commandlib.util.nms.server;

import net.kunmc.lab.commandlib.util.bukkit.VersionUtil;
import net.kunmc.lab.commandlib.util.nms.MinecraftClass;
import net.kunmc.lab.commandlib.util.nms.command.NMSCommandDispatcher;

Expand All @@ -9,11 +10,21 @@ public NMSDedicatedServer(Object handle) {
}

public NMSCommandDispatcher getCommandDispatcher() {
if (VersionUtil.is1_20_x()) {
return new NMSCommandDispatcher(invokeMethod("aE"));
}

return new NMSCommandDispatcher(invokeMethod("getCommandDispatcher", "getCommands", "aC"));
}

public NMSDataPackResources getDataPackResources() {
Object obj = getValue(Object.class, "dataPackResources", "au", "resources");
Object obj;
if (VersionUtil.is1_20_x()) {
obj = getValue(Object.class, "ax");
} else {
obj = getValue(Object.class, "dataPackResources", "au", "resources");
}

try {
return new NMSDataPackResources(obj);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.kunmc.lab.commandlib.util.nms.world;

import net.kunmc.lab.commandlib.util.bukkit.VersionUtil;
import net.kunmc.lab.commandlib.util.nms.CraftBukkitClass;
import org.bukkit.enchantments.Enchantment;

Expand All @@ -9,6 +10,10 @@ public NMSCraftEnchantment() {
}

public Enchantment createInstance(NMSEnchantment nms) {
if (VersionUtil.is1_20_x()) {
return ((Enchantment) invokeMethod("minecraftToBukkit", new Class[]{nms.getFoundClass()}, nms.getHandle()));
}

return ((Enchantment) newInstance(new Class[]{nms.getFoundClass()}, new Object[]{nms.getHandle()}));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.kunmc.lab.commandlib.util.nms.world;

import net.kunmc.lab.commandlib.util.bukkit.VersionUtil;
import net.kunmc.lab.commandlib.util.nms.CraftBukkitClass;
import org.bukkit.potion.PotionEffectType;

Expand All @@ -13,6 +14,12 @@ public NMSCraftPotionEffectType(Object handle) {
}

public PotionEffectType createInstance(NMSMobEffectList nms) {
if (VersionUtil.is1_20_x()) {
return ((PotionEffectType) invokeMethod("minecraftToBukkit",
new Class[]{nms.getFoundClass()},
nms.getHandle()));
}

return ((PotionEffectType) newInstance(new Class[]{nms.getFoundClass()}, new Object[]{nms.getHandle()}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.kunmc.lab.commandlib.util.bukkit;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

class VersionUtilTest {
@Nested
class CompareMinecraftVersion {
@Test
void returns_zero_with_same_values() {
int res = VersionUtil.compareMinecraftVersion("1.16.5", "1.16.5");
Assertions.assertThat(res)
.isEqualTo(0);
}

@Test
void returns_negative_when_x_is_lower() {
int res = VersionUtil.compareMinecraftVersion("1.16.5", "1.20.4");
Assertions.assertThat(res)
.isLessThan(0);
}

@Test
void returns_positive_when_x_is_greater() {
int res = VersionUtil.compareMinecraftVersion("1.20.4", "1.16.5");
Assertions.assertThat(res)
.isGreaterThan(0);
}
}
}
6 changes: 0 additions & 6 deletions bukkit/test_plugin_1.16.5/README.md

This file was deleted.

6 changes: 0 additions & 6 deletions bukkit/test_plugin_1.19.4/README.md

This file was deleted.

Loading

0 comments on commit ecf5566

Please sign in to comment.