Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix running Duels on MC 1.21+ #120

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ buildscript {
}

dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
classpath 'com.gradleup.shadow:shadow-gradle-plugin:8.3.0'
}
}

allprojects {
group 'me.realized'
version '3.5.3'
version '3.5.4'
}

subprojects {
apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'com.gradleup.shadow'
apply plugin: 'maven-publish'

sourceCompatibility = 1.8
Expand Down
10 changes: 5 additions & 5 deletions duels-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ processResources {

dependencies {
compileOnly 'org.jetbrains:annotations-java5:22.0.0'
compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
compileOnly 'org.projectlombok:lombok:1.18.36'
annotationProcessor 'org.projectlombok:lombok:1.18.36'
implementation 'org.spigotmc:spigot-api:1.14.4-R0.1-SNAPSHOT'
implementation 'com.mojang:authlib:1.5.21'
implementation 'me.clip:placeholderapi:2.11.1'
implementation 'me.clip:placeholderapi:2.11.6'
implementation 'com.SirBlobman.combatlogx:CombatLogX-API:10.0.0.0-SNAPSHOT'
implementation ('net.essentialsx:EssentialsX:2.19.2') {
transitive = false
Expand All @@ -42,7 +42,7 @@ dependencies {
implementation project(':duels-worldguard')
implementation project(':duels-worldguard-v6')
implementation project(':duels-worldguard-v7')
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.1'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.2'
}

shadowJar {
Expand All @@ -60,7 +60,7 @@ shadowJar {
}

final String group = project.group.toString() + "." + parent.name.toLowerCase() + ".shaded."
relocate 'com.fasterxml.jackson.core', group + 'jackson-core'
relocate 'com.fasterxml.jackson', group + 'jackson'
}

// To build Duels plugin jar, run './gradlew clean build'.
Expand Down
30 changes: 28 additions & 2 deletions duels-plugin/src/main/java/me/realized/duels/util/EnumUtil.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
package me.realized.duels.util;

import java.lang.reflect.Field;
import java.util.Arrays;

public final class EnumUtil {

private EnumUtil() {}

public static <E extends Enum<E>> E getByName(final String name, Class<E> clazz) {
return clazz.cast(Arrays.stream(clazz.getEnumConstants()).filter(type -> type.name().equalsIgnoreCase(name)).findFirst().orElse(null));
public static <E> E getByName(final String name, final Class<E> clazz) {
if (clazz == null) {
throw new IllegalArgumentException("Class cannot be null");
}

// Handle enums
if (clazz.isEnum()) {
return Arrays.stream(clazz.getEnumConstants())
.filter(type -> type.toString().equalsIgnoreCase(name))
.findFirst()
.orElse(null);
}

// Handle static classes with public static fields
try {
final Field[] fields = clazz.getDeclaredFields();
for (final Field field : fields) {
if (field.getType() == clazz && field.getName().equalsIgnoreCase(name)) {
return (E) field.get(null); // Get the static field value
}
}
} catch (final IllegalAccessException e) {
throw new RuntimeException("Failed to access static field in " + clazz.getName(), e);
}

// No match found
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public final class ReflectionUtil {
static {
final String packageName = Bukkit.getServer().getClass().getPackage().getName();
PACKAGE_VERSION = packageName.substring(packageName.lastIndexOf('.') + 1);
MAJOR_VERSION = NumberUtil.parseInt(PACKAGE_VERSION.split("_")[1]).orElse(0);
if (PACKAGE_VERSION.equalsIgnoreCase("craftbukkit")) {
String bukkitVersion = Bukkit.getBukkitVersion();
MAJOR_VERSION = NumberUtil.parseInt(bukkitVersion.split("-")[0].split("\\.")[1]).orElse(0);
} else {
MAJOR_VERSION = NumberUtil.parseInt(PACKAGE_VERSION.split("_")[1]).orElse(0);
}
}

public static int getMajorVersion() {
Expand Down Expand Up @@ -56,6 +61,9 @@ public static Class<?> getNMSClass(final String name) {

public static Class<?> getCBClass(final String path, final boolean logError) {
try {
if (getMajorVersion() >= 21) {
return Class.forName("org.bukkit.craftbukkit." + path);
}
return Class.forName("org.bukkit.craftbukkit." + PACKAGE_VERSION + "." + path);
} catch (ClassNotFoundException ex) {
if (logError) {
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip