Skip to content

Commit

Permalink
Improve nms, add 1.20.5+ support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravis96 committed Jun 30, 2024
1 parent 7f48954 commit 940ecc5
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 48 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ jobs:
strategy:
matrix:
java:
- 17
- 21
fail-fast: false
steps:
- name: Checkout repository
uses: actions/checkout@v4.1.6

- name: Set up JDK 17
- name: Set up JDK 21
uses: actions/setup-java@v4.2.1
with:
distribution: adopt
java-version: '17'
java-version: '21'

- name: Grant execute permission for gradlew
run: chmod +x gradlew
Expand Down
65 changes: 43 additions & 22 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
plugins {
`java-library`
id("com.github.johnrengelman.shadow") version "8.1.1"
id("idea")
id("io.github.goooler.shadow") version "8.1.7"
id("io.papermc.paperweight.userdev") version "1.7.1" apply false
}

idea {
project.jdkName = "21"
}

allprojects {
group = "cc.dreamcode.template"
version = "1.0-InDEV"

apply(plugin = "java-library")
apply(plugin = "com.github.johnrengelman.shadow")
apply(plugin = "io.github.goooler.shadow")

repositories {
/* Libraries */
Expand All @@ -20,27 +25,12 @@ allprojects {
}

subprojects {
if (name.startsWith("v") &&
(name.split("_").getOrNull(1)?.toInt() ?: 0) >= 17
) {
apply(plugin = "io.papermc.paperweight.userdev")

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

withSourcesJar()
withJavadocJar()
}
}
else {
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

withSourcesJar()
withJavadocJar()
}
withSourcesJar()
withJavadocJar()
}

tasks.withType<JavaCompile> {
Expand All @@ -58,6 +48,37 @@ subprojects {
}
}

project(":plugin-core:nms").subprojects {

val minor = name.split("_").getOrNull(1)?.toInt() ?: 0
val patch = name.split("R").getOrNull(1)?.toInt() ?: 0

if (name == "api" || minor < 17) {
return@subprojects
}

apply(plugin = "io.papermc.paperweight.userdev")

if (minor >= 21 || minor == 20 && patch >= 4) {
java {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21

withSourcesJar()
withJavadocJar()
}
}
else {
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

withSourcesJar()
withJavadocJar()
}
}
}

tasks.register("pluginVersion") {
println(project.version)
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion 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/HEAD/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 Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package cc.dreamcode.template.nms.api;

import cc.dreamcode.utilities.ClassUtil;
import cc.dreamcode.utilities.builder.MapBuilder;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.UnsafeValues;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicReference;
import java.util.TreeMap;

@UtilityClass
public class VersionProvider {

private final TreeMap<Integer, String> NEWER_NMS_VERSION = new TreeMap<>(MapBuilder.of(
3839, "v1_20_R4"
));

public static VersionAccessor getVersionAccessor() {

final String version = VersionProvider.getNmsVersion();
final String version = getNmsVersion();
final String className = "cc.dreamcode.template.nms." + version + "." + version.toUpperCase(Locale.ROOT) + "_VersionAccessor";

return (VersionAccessor) ClassUtil.getClass(className)
Expand All @@ -26,25 +35,16 @@ public static VersionAccessor getVersionAccessor() {
}

private static String getNmsVersion() {
final AtomicReference<String> ref = new AtomicReference<>();

for (Package pack : Package.getPackages()) {
if (pack.getName().startsWith("org.bukkit.craftbukkit.v")) {
final String name = pack.getName().split("\\.")[3];

try {
Class.forName("org.bukkit.craftbukkit." + name + ".entity.CraftPlayer");
ref.set(name);
}
catch (ClassNotFoundException ignored) {}
}
try {
Method getDataVersion = UnsafeValues.class.getMethod("getDataVersion");
int dataVersion = (int) getDataVersion.invoke(Bukkit.getServer().getUnsafe());
return NEWER_NMS_VERSION.floorEntry(dataVersion).getValue();
}

final String version = ref.get();
if (version == null) {
throw new RuntimeException("Cannot find server version");
catch (NoSuchMethodException ignored) {
return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
}
catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Cannot find server version", e);
}

return version;
}
}
14 changes: 14 additions & 0 deletions plugin-core/nms/v1_20_R4/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
repositories {
maven("https://repo.codemc.io/repository/nms")
maven("https://oss.sonatype.org/content/repositories/snapshots")
}

dependencies {
implementation(project(":plugin-core:nms:api"))

paperweight.paperDevBundle("1.20.6-R0.1-SNAPSHOT")

// -- dream-utilities --
implementation("cc.dreamcode:utilities:1.4.1")
implementation("cc.dreamcode:utilities-bukkit:1.4.1")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cc.dreamcode.template.nms.v1_20_R4;

import cc.dreamcode.template.nms.api.VersionAccessor;

public class V1_20_R4_VersionAccessor implements VersionAccessor {
}
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ include(":plugin-core:nms:v1_19_R2")
include(":plugin-core:nms:v1_19_R3")
include(":plugin-core:nms:v1_20_R1")
include(":plugin-core:nms:v1_20_R2")
include(":plugin-core:nms:v1_20_R3")
include(":plugin-core:nms:v1_20_R3")
include(":plugin-core:nms:v1_20_R4")

0 comments on commit 940ecc5

Please sign in to comment.