Skip to content

Commit 4052564

Browse files
committed
Fix v26.2 lookup for UnsafeValues#nextEntityId
1 parent 6237aee commit 4052564

7 files changed

Lines changed: 66 additions & 12 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
val fullVersion = "3.3.3"
1+
val fullVersion = "3.3.4"
22
val snapshot = true
33

44
group = "io.github.tofaa2"

flake.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
adventure = "4.25.0"
33
jetbrains-annotations = "26.0.2"
44
gson = "2.13.1"
5-
packetevents = "2.11.0"
5+
packetevents = "2.13.0"
66
paper = "1.21-R0.1-SNAPSHOT"
77
velocity = "3.4.0-SNAPSHOT"
88
run-paper = "3.0.2"

platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityIdProvider.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package me.tofaa.entitylib.spigot;
22

33
import java.lang.reflect.Field;
4+
import java.lang.reflect.InvocationTargetException;
5+
import java.lang.reflect.Method;
46
import java.lang.reflect.Modifier;
57
import java.util.UUID;
68
import java.util.concurrent.atomic.AtomicInteger;
@@ -9,6 +11,7 @@
911

1012
import org.bukkit.Bukkit;
1113
import org.bukkit.UnsafeValues;
14+
import org.bukkit.World;
1215
import org.bukkit.plugin.java.JavaPlugin;
1316
import org.jetbrains.annotations.NotNull;
1417

@@ -49,7 +52,8 @@ public int provide(@NotNull UUID entityUUID, @NotNull EntityType entityType) {
4952
* and platform.
5053
* Logic:
5154
* <ul>
52-
* <li>Paper (1.16+): Use `{@link UnsafeValues#nextEntityId()}`. (Paper API)</li>
55+
* <li>Paper (1.16 until 26.1): Use `{@link UnsafeValues#nextEntityId()}`. (Paper API)</li>
56+
* <li>Paper 26.2 and ab ove: Uses the updated signature of `{@link UnsafeValues#nextEntityId()}` with the first World in `{@link Bukkit#getWorlds()}` which should be overworld </li>
5357
* <li>1.14+: Access `AtomicInteger` field ("entityCount", "d", "c").</li>
5458
* <li>1.8+: Access legacy `int` field "entityCount".</li>
5559
* </ul>
@@ -62,10 +66,36 @@ private Supplier<Integer> detectIdSupplier() {
6266

6367
final ServerVersion serverVersion = platform.getAPI().getPacketEvents().getServerManager().getVersion();
6468

65-
if (isPaper() && serverVersion.isNewerThanOrEquals(ServerVersion.V_1_16)) {
66-
return Bukkit.getUnsafe()::nextEntityId; // Paper API
69+
if (isPaper()) {
70+
if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_16)) {
71+
if (serverVersion.isOlderThanOrEquals(ServerVersion.V_26_1)) {
72+
return Bukkit.getUnsafe()::nextEntityId; // Paper API Before unsafe values change
73+
}
74+
else {
75+
// Please do not look at this branch
76+
Class<UnsafeValues> unsafeValuesClass = UnsafeValues.class;
77+
try {
78+
Method method = unsafeValuesClass.getMethod("nextEntityId", World.class);
79+
UnsafeValues unsafe = Bukkit.getUnsafe();
80+
return () -> {
81+
try {
82+
World overworld = Bukkit.getWorlds().get(0); // There is a chance the server has no worlds but at that point we have bigger issues to worry about
83+
return (Integer) method.invoke(unsafe, overworld);
84+
} catch (IllegalAccessException | InvocationTargetException e) {
85+
throw new RuntimeException(e);
86+
}
87+
};
88+
} catch (NoSuchMethodException e) {
89+
throw new RuntimeException(e);
90+
}
91+
}
92+
}
6793
}
6894

95+
// if (isPaper() && serverVersion.isNewerThanOrEquals(ServerVersion.V_1_16)) {
96+
// return Bukkit.getUnsafe()::nextEntityId; // Paper API
97+
// }
98+
6999
final Class<?> entityClass = getEntityClass();
70100
if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_14)) {
71101
final Supplier<Integer> modernSupplier = resolveAtomicSupplier(entityClass);

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ include(":movement-engine")
1818
include(":platforms:movement")
1919
include(":spaceNPC")
2020

21-
if (System.getenv("PRIVATE").toBoolean()) {
21+
if (System.getenv("ENTITYLIB_PRIVATE").toBoolean()) {
2222
include("discord-bot")
2323
include(":code-gen")
2424
include(":test-plugin")

test-plugin/build.gradle.kts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ plugins {
55
}
66

77
repositories {
8-
maven("https://maven.evokegames.gg/snapshots")
98
maven("https://repo.papermc.io/repository/maven-public/")
109
}
1110

1211
dependencies {
1312
compileOnly(libs.paper)
1413
compileOnly(libs.packetevents.spigot)
1514
implementation(project(":platforms:spigot"))
16-
// implementation(project(":platforms:spigot"))
17-
}
15+
2}
1816

1917
tasks {
20-
val version = "1.21.3"
21-
val javaVersion = JavaLanguageVersion.of(21)
18+
val version = "26.2"
19+
val javaVersion = JavaLanguageVersion.of(25)
2220

2321
val jvmArgsExternal = listOf(
2422
"-Dcom.mojang.eula.agree=true"

test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class TestEntityLibPlugin extends JavaPlugin {
1515

1616
@Override
1717
public void onEnable() {
18-
DateFormat.getDateTimeInstance().format(new java.util.Date(timestamp));
1918

2019
SpigotEntityLibPlatform platform = new SpigotEntityLibPlatform(this);
2120
APIConfig settings = new APIConfig(PacketEvents.getAPI())

0 commit comments

Comments
 (0)