Skip to content

Commit 3820cdb

Browse files
authored
Remove outdated CraftChunk#getHandle workaround (#3095)
1 parent 512a04d commit 3820cdb

File tree

6 files changed

+35
-144
lines changed

6 files changed

+35
-144
lines changed

worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightPlatformAdapter.java

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import net.minecraft.world.level.block.Block;
4646
import net.minecraft.world.level.block.Blocks;
4747
import net.minecraft.world.level.block.entity.BlockEntity;
48-
import net.minecraft.world.level.chunk.ChunkAccess;
4948
import net.minecraft.world.level.chunk.ChunkStatus;
5049
import net.minecraft.world.level.chunk.GlobalPalette;
5150
import net.minecraft.world.level.chunk.HashMapPalette;
@@ -57,13 +56,13 @@
5756
import net.minecraft.world.level.chunk.SingleValuePalette;
5857
import net.minecraft.world.level.entity.PersistentEntitySectionManager;
5958
import org.apache.logging.log4j.Logger;
59+
import org.bukkit.Chunk;
6060
import org.bukkit.craftbukkit.v1_20_R2.CraftChunk;
6161

6262
import javax.annotation.Nonnull;
6363
import javax.annotation.Nullable;
6464
import java.lang.invoke.MethodHandle;
6565
import java.lang.invoke.MethodHandles;
66-
import java.lang.invoke.MethodType;
6766
import java.lang.reflect.Constructor;
6867
import java.lang.reflect.Field;
6968
import java.lang.reflect.InvocationTargetException;
@@ -82,7 +81,6 @@
8281
import java.util.concurrent.Semaphore;
8382
import java.util.function.IntFunction;
8483

85-
import static java.lang.invoke.MethodType.methodType;
8684
import static net.minecraft.core.registries.Registries.BIOME;
8785

8886
public final class PaperweightPlatformAdapter extends NMSAdapter {
@@ -106,12 +104,6 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
106104
private static final MethodHandle methodremoveTickingBlockEntity;
107105
private static final Field fieldBiomes;
108106

109-
/*
110-
* This is a workaround for the changes from https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/commits/1fddefce1cdce44010927b888432bf70c0e88cde#src/main/java/org/bukkit/craftbukkit/CraftChunk.java
111-
* and is only needed to support 1.19.4 versions before *and* after this change.
112-
*/
113-
private static final MethodHandle CRAFT_CHUNK_GET_HANDLE;
114-
115107
private static final Field fieldRemove;
116108

117109
private static final Logger LOGGER = LogManagerCompat.getLogger();
@@ -224,20 +216,6 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
224216
} catch (Exception e) {
225217
throw new RuntimeException(e);
226218
}
227-
MethodHandle craftChunkGetHandle;
228-
final MethodType type = methodType(LevelChunk.class);
229-
try {
230-
craftChunkGetHandle = lookup.findVirtual(CraftChunk.class, "getHandle", type);
231-
} catch (NoSuchMethodException | IllegalAccessException e) {
232-
try {
233-
final MethodType newType = methodType(ChunkAccess.class, ChunkStatus.class);
234-
craftChunkGetHandle = lookup.findVirtual(CraftChunk.class, "getHandle", newType);
235-
craftChunkGetHandle = MethodHandles.insertArguments(craftChunkGetHandle, 1, ChunkStatus.FULL);
236-
} catch (NoSuchMethodException | IllegalAccessException ex) {
237-
throw new RuntimeException(ex);
238-
}
239-
}
240-
CRAFT_CHUNK_GET_HANDLE = craftChunkGetHandle;
241219
}
242220

243221
static boolean setSectionAtomic(
@@ -291,7 +269,7 @@ public static CompletableFuture<LevelChunk> ensureLoaded(ServerLevel serverLevel
291269
.thenApply(chunk -> {
292270
addTicket(serverLevel, chunkX, chunkZ);
293271
try {
294-
return (LevelChunk) CRAFT_CHUNK_GET_HANDLE.invoke(chunk);
272+
return toLevelChunk(chunk);
295273
} catch (Throwable e) {
296274
LOGGER.error("Could not asynchronously load chunk at {},{}", chunkX, chunkZ, e);
297275
return null;
@@ -315,6 +293,9 @@ public static CompletableFuture<LevelChunk> ensureLoaded(ServerLevel serverLevel
315293
return CompletableFuture.supplyAsync(() -> TaskManager.taskManager().sync(() -> serverLevel.getChunk(chunkX, chunkZ)));
316294
}
317295

296+
private static LevelChunk toLevelChunk(Chunk chunk) {
297+
return (LevelChunk) ((CraftChunk) chunk).getHandle(ChunkStatus.FULL);
298+
}
318299

319300
public static @Nullable LevelChunk getChunkImmediatelyAsync(ServerLevel serverLevel, int chunkX, int chunkZ) {
320301
if (!PaperLib.isPaper()) {

worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightPlatformAdapter.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import net.minecraft.world.level.block.Block;
4646
import net.minecraft.world.level.block.Blocks;
4747
import net.minecraft.world.level.block.entity.BlockEntity;
48-
import net.minecraft.world.level.chunk.ChunkAccess;
4948
import net.minecraft.world.level.chunk.ChunkStatus;
5049
import net.minecraft.world.level.chunk.GlobalPalette;
5150
import net.minecraft.world.level.chunk.HashMapPalette;
@@ -57,13 +56,13 @@
5756
import net.minecraft.world.level.chunk.SingleValuePalette;
5857
import net.minecraft.world.level.entity.PersistentEntitySectionManager;
5958
import org.apache.logging.log4j.Logger;
59+
import org.bukkit.Chunk;
6060
import org.bukkit.craftbukkit.v1_20_R3.CraftChunk;
6161

6262
import javax.annotation.Nonnull;
6363
import javax.annotation.Nullable;
6464
import java.lang.invoke.MethodHandle;
6565
import java.lang.invoke.MethodHandles;
66-
import java.lang.invoke.MethodType;
6766
import java.lang.reflect.Constructor;
6867
import java.lang.reflect.Field;
6968
import java.lang.reflect.InvocationTargetException;
@@ -82,7 +81,6 @@
8281
import java.util.concurrent.Semaphore;
8382
import java.util.function.IntFunction;
8483

85-
import static java.lang.invoke.MethodType.methodType;
8684
import static net.minecraft.core.registries.Registries.BIOME;
8785

8886
public final class PaperweightPlatformAdapter extends NMSAdapter {
@@ -106,12 +104,6 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
106104
private static final MethodHandle methodRemoveGameEventListener;
107105
private static final MethodHandle methodremoveTickingBlockEntity;
108106

109-
/*
110-
* This is a workaround for the changes from https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/commits/1fddefce1cdce44010927b888432bf70c0e88cde#src/main/java/org/bukkit/craftbukkit/CraftChunk.java
111-
* and is only needed to support 1.19.4 versions before *and* after this change.
112-
*/
113-
private static final MethodHandle CRAFT_CHUNK_GET_HANDLE;
114-
115107
private static final Field fieldRemove;
116108

117109
private static final Logger LOGGER = LogManagerCompat.getLogger();
@@ -224,20 +216,6 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
224216
} catch (Exception e) {
225217
throw new RuntimeException(e);
226218
}
227-
MethodHandle craftChunkGetHandle;
228-
final MethodType type = methodType(LevelChunk.class);
229-
try {
230-
craftChunkGetHandle = lookup.findVirtual(CraftChunk.class, "getHandle", type);
231-
} catch (NoSuchMethodException | IllegalAccessException e) {
232-
try {
233-
final MethodType newType = methodType(ChunkAccess.class, ChunkStatus.class);
234-
craftChunkGetHandle = lookup.findVirtual(CraftChunk.class, "getHandle", newType);
235-
craftChunkGetHandle = MethodHandles.insertArguments(craftChunkGetHandle, 1, ChunkStatus.FULL);
236-
} catch (NoSuchMethodException | IllegalAccessException ex) {
237-
throw new RuntimeException(ex);
238-
}
239-
}
240-
CRAFT_CHUNK_GET_HANDLE = craftChunkGetHandle;
241219
}
242220

243221
static boolean setSectionAtomic(
@@ -291,7 +269,7 @@ public static CompletableFuture<LevelChunk> ensureLoaded(ServerLevel serverLevel
291269
.thenApply(chunk -> {
292270
addTicket(serverLevel, chunkX, chunkZ);
293271
try {
294-
return (LevelChunk) CRAFT_CHUNK_GET_HANDLE.invoke(chunk);
272+
return toLevelChunk(chunk);
295273
} catch (Throwable e) {
296274
LOGGER.error("Could not asynchronously load chunk at {},{}", chunkX, chunkZ, e);
297275
return null;
@@ -315,6 +293,10 @@ public static CompletableFuture<LevelChunk> ensureLoaded(ServerLevel serverLevel
315293
return CompletableFuture.supplyAsync(() -> TaskManager.taskManager().sync(() -> serverLevel.getChunk(chunkX, chunkZ)));
316294
}
317295

296+
private static LevelChunk toLevelChunk(Chunk chunk) {
297+
return (LevelChunk) ((CraftChunk) chunk).getHandle(ChunkStatus.FULL);
298+
}
299+
318300
public static @Nullable LevelChunk getChunkImmediatelyAsync(ServerLevel serverLevel, int chunkX, int chunkZ) {
319301
if (!PaperLib.isPaper()) {
320302
LevelChunk nmsChunk = serverLevel.getChunkSource().getChunk(chunkX, chunkZ, false);

worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightPlatformAdapter.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import net.minecraft.world.level.block.Block;
4545
import net.minecraft.world.level.block.Blocks;
4646
import net.minecraft.world.level.block.entity.BlockEntity;
47-
import net.minecraft.world.level.chunk.ChunkAccess;
4847
import net.minecraft.world.level.chunk.GlobalPalette;
4948
import net.minecraft.world.level.chunk.HashMapPalette;
5049
import net.minecraft.world.level.chunk.LevelChunk;
@@ -56,13 +55,13 @@
5655
import net.minecraft.world.level.chunk.status.ChunkStatus;
5756
import net.minecraft.world.level.entity.PersistentEntitySectionManager;
5857
import org.apache.logging.log4j.Logger;
58+
import org.bukkit.Chunk;
5959
import org.bukkit.craftbukkit.CraftChunk;
6060

6161
import javax.annotation.Nonnull;
6262
import javax.annotation.Nullable;
6363
import java.lang.invoke.MethodHandle;
6464
import java.lang.invoke.MethodHandles;
65-
import java.lang.invoke.MethodType;
6665
import java.lang.reflect.Constructor;
6766
import java.lang.reflect.Field;
6867
import java.lang.reflect.InvocationTargetException;
@@ -80,7 +79,6 @@
8079
import java.util.concurrent.Semaphore;
8180
import java.util.function.IntFunction;
8281

83-
import static java.lang.invoke.MethodType.methodType;
8482
import static net.minecraft.core.registries.Registries.BIOME;
8583

8684
public final class PaperweightPlatformAdapter extends NMSAdapter {
@@ -104,12 +102,6 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
104102
private static final MethodHandle methodRemoveGameEventListener;
105103
private static final MethodHandle methodremoveTickingBlockEntity;
106104

107-
/*
108-
* This is a workaround for the changes from https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/commits/1fddefce1cdce44010927b888432bf70c0e88cde#src/main/java/org/bukkit/craftbukkit/CraftChunk.java
109-
* and is only needed to support 1.19.4 versions before *and* after this change.
110-
*/
111-
private static final MethodHandle CRAFT_CHUNK_GET_HANDLE;
112-
113105
private static final Field fieldRemove;
114106

115107
private static final Logger LOGGER = LogManagerCompat.getLogger();
@@ -222,20 +214,6 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
222214
} catch (Exception e) {
223215
throw new RuntimeException(e);
224216
}
225-
MethodHandle craftChunkGetHandle;
226-
final MethodType type = methodType(LevelChunk.class);
227-
try {
228-
craftChunkGetHandle = lookup.findVirtual(CraftChunk.class, "getHandle", type);
229-
} catch (NoSuchMethodException | IllegalAccessException e) {
230-
try {
231-
final MethodType newType = methodType(ChunkAccess.class, ChunkStatus.class);
232-
craftChunkGetHandle = lookup.findVirtual(CraftChunk.class, "getHandle", newType);
233-
craftChunkGetHandle = MethodHandles.insertArguments(craftChunkGetHandle, 1, ChunkStatus.FULL);
234-
} catch (NoSuchMethodException | IllegalAccessException ex) {
235-
throw new RuntimeException(ex);
236-
}
237-
}
238-
CRAFT_CHUNK_GET_HANDLE = craftChunkGetHandle;
239217
}
240218

241219
static boolean setSectionAtomic(
@@ -289,7 +267,7 @@ public static CompletableFuture<LevelChunk> ensureLoaded(ServerLevel serverLevel
289267
.thenApply(chunk -> {
290268
addTicket(serverLevel, chunkX, chunkZ);
291269
try {
292-
return (LevelChunk) CRAFT_CHUNK_GET_HANDLE.invoke(chunk);
270+
return toLevelChunk(chunk);
293271
} catch (Throwable e) {
294272
LOGGER.error("Could not asynchronously load chunk at {},{}", chunkX, chunkZ, e);
295273
return null;
@@ -313,6 +291,10 @@ public static CompletableFuture<LevelChunk> ensureLoaded(ServerLevel serverLevel
313291
return CompletableFuture.supplyAsync(() -> TaskManager.taskManager().sync(() -> serverLevel.getChunk(chunkX, chunkZ)));
314292
}
315293

294+
private static LevelChunk toLevelChunk(Chunk chunk) {
295+
return (LevelChunk) ((CraftChunk) chunk).getHandle(ChunkStatus.FULL);
296+
}
297+
316298
public static @Nullable LevelChunk getChunkImmediatelyAsync(ServerLevel serverLevel, int chunkX, int chunkZ) {
317299
if (!PaperLib.isPaper()) {
318300
LevelChunk nmsChunk = serverLevel.getChunkSource().getChunk(chunkX, chunkZ, false);

worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightPlatformAdapter.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import net.minecraft.world.level.block.Block;
4444
import net.minecraft.world.level.block.Blocks;
4545
import net.minecraft.world.level.block.entity.BlockEntity;
46-
import net.minecraft.world.level.chunk.ChunkAccess;
4746
import net.minecraft.world.level.chunk.GlobalPalette;
4847
import net.minecraft.world.level.chunk.HashMapPalette;
4948
import net.minecraft.world.level.chunk.LevelChunk;
@@ -55,13 +54,13 @@
5554
import net.minecraft.world.level.chunk.status.ChunkStatus;
5655
import net.minecraft.world.level.entity.PersistentEntitySectionManager;
5756
import org.apache.logging.log4j.Logger;
57+
import org.bukkit.Chunk;
5858
import org.bukkit.craftbukkit.CraftChunk;
5959

6060
import javax.annotation.Nonnull;
6161
import javax.annotation.Nullable;
6262
import java.lang.invoke.MethodHandle;
6363
import java.lang.invoke.MethodHandles;
64-
import java.lang.invoke.MethodType;
6564
import java.lang.reflect.Constructor;
6665
import java.lang.reflect.Field;
6766
import java.lang.reflect.InvocationTargetException;
@@ -79,7 +78,6 @@
7978
import java.util.concurrent.Semaphore;
8079
import java.util.function.IntFunction;
8180

82-
import static java.lang.invoke.MethodType.methodType;
8381
import static net.minecraft.core.registries.Registries.BIOME;
8482

8583
public final class PaperweightPlatformAdapter extends NMSAdapter {
@@ -103,12 +101,6 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
103101
private static final MethodHandle methodRemoveGameEventListener;
104102
private static final MethodHandle methodremoveTickingBlockEntity;
105103

106-
/*
107-
* This is a workaround for the changes from https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/commits/1fddefce1cdce44010927b888432bf70c0e88cde#src/main/java/org/bukkit/craftbukkit/CraftChunk.java
108-
* and is only needed to support 1.19.4 versions before *and* after this change.
109-
*/
110-
private static final MethodHandle CRAFT_CHUNK_GET_HANDLE;
111-
112104
private static final Field fieldRemove;
113105

114106
private static final Logger LOGGER = LogManagerCompat.getLogger();
@@ -206,20 +198,6 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
206198
} catch (Exception e) {
207199
throw new RuntimeException(e);
208200
}
209-
MethodHandle craftChunkGetHandle;
210-
final MethodType type = methodType(LevelChunk.class);
211-
try {
212-
craftChunkGetHandle = lookup.findVirtual(CraftChunk.class, "getHandle", type);
213-
} catch (NoSuchMethodException | IllegalAccessException e) {
214-
try {
215-
final MethodType newType = methodType(ChunkAccess.class, ChunkStatus.class);
216-
craftChunkGetHandle = lookup.findVirtual(CraftChunk.class, "getHandle", newType);
217-
craftChunkGetHandle = MethodHandles.insertArguments(craftChunkGetHandle, 1, ChunkStatus.FULL);
218-
} catch (NoSuchMethodException | IllegalAccessException ex) {
219-
throw new RuntimeException(ex);
220-
}
221-
}
222-
CRAFT_CHUNK_GET_HANDLE = craftChunkGetHandle;
223201
}
224202

225203
static boolean setSectionAtomic(
@@ -273,7 +251,7 @@ public static CompletableFuture<LevelChunk> ensureLoaded(ServerLevel serverLevel
273251
.thenApply(chunk -> {
274252
addTicket(serverLevel, chunkX, chunkZ);
275253
try {
276-
return (LevelChunk) CRAFT_CHUNK_GET_HANDLE.invoke(chunk);
254+
return toLevelChunk(chunk);
277255
} catch (Throwable e) {
278256
LOGGER.error("Could not asynchronously load chunk at {},{}", chunkX, chunkZ, e);
279257
return null;
@@ -297,6 +275,10 @@ public static CompletableFuture<LevelChunk> ensureLoaded(ServerLevel serverLevel
297275
return CompletableFuture.supplyAsync(() -> TaskManager.taskManager().sync(() -> serverLevel.getChunk(chunkX, chunkZ)));
298276
}
299277

278+
private static LevelChunk toLevelChunk(Chunk chunk) {
279+
return (LevelChunk) ((CraftChunk) chunk).getHandle(ChunkStatus.FULL);
280+
}
281+
300282
public static @Nullable LevelChunk getChunkImmediatelyAsync(ServerLevel serverLevel, int chunkX, int chunkZ) {
301283
if (!PaperLib.isPaper()) {
302284
LevelChunk nmsChunk = serverLevel.getChunkSource().getChunk(chunkX, chunkZ, false);

0 commit comments

Comments
 (0)