Skip to content

Commit 25bcf94

Browse files
authored
fix: account for ChunkEntitySlices nullability (#3248)
- fixes #3233
1 parent 4af9ecd commit 25bcf94

File tree

7 files changed

+60
-87
lines changed

7 files changed

+60
-87
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: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -676,12 +676,16 @@ static List<Entity> getEntities(LevelChunk chunk) {
676676
ExceptionCollector<RuntimeException> collector = new ExceptionCollector<>();
677677
if (PaperLib.isPaper()) {
678678
if (POST_CHUNK_REWRITE) {
679-
try {
680-
//noinspection unchecked
681-
return (List<Entity>) PAPER_CHUNK_GEN_ALL_ENTITIES.invoke(chunk.level.getEntityLookup().getChunk(chunk.locX, chunk.locZ));
682-
} catch (IllegalAccessException | InvocationTargetException e) {
683-
throw new RuntimeException("Failed to lookup entities [POST_CHUNK_REWRITE=true]", e);
684-
}
679+
return Optional.ofNullable(chunk.level
680+
.getEntityLookup()
681+
.getChunk(chunk.locX, chunk.locZ)).map(c -> {
682+
try {
683+
//noinspection unchecked
684+
return (List<Entity>) PAPER_CHUNK_GEN_ALL_ENTITIES.invoke(c);
685+
} catch (IllegalAccessException | InvocationTargetException e) {
686+
throw new RuntimeException("Failed to lookup entities [PAPER=true]", e);
687+
}
688+
}).orElse(Collections.emptyList());
685689
}
686690
try {
687691
EntityList entityList = (EntityList) LEVEL_CHUNK_ENTITIES.get(chunk);

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -676,12 +676,16 @@ static List<Entity> getEntities(LevelChunk chunk) {
676676
ExceptionCollector<RuntimeException> collector = new ExceptionCollector<>();
677677
if (PaperLib.isPaper()) {
678678
if (POST_CHUNK_REWRITE) {
679-
try {
680-
//noinspection unchecked
681-
return (List<Entity>) PAPER_CHUNK_GEN_ALL_ENTITIES.invoke(chunk.level.getEntityLookup().getChunk(chunk.locX, chunk.locZ));
682-
} catch (IllegalAccessException | InvocationTargetException e) {
683-
throw new RuntimeException("Failed to lookup entities [POST_CHUNK_REWRITE=true]", e);
684-
}
679+
return Optional.ofNullable(chunk.level
680+
.getEntityLookup()
681+
.getChunk(chunk.locX, chunk.locZ)).map(c -> {
682+
try {
683+
//noinspection unchecked
684+
return (List<Entity>) PAPER_CHUNK_GEN_ALL_ENTITIES.invoke(c);
685+
} catch (IllegalAccessException | InvocationTargetException e) {
686+
throw new RuntimeException("Failed to lookup entities [PAPER=true]", e);
687+
}
688+
}).orElse(Collections.emptyList());
685689
}
686690
try {
687691
EntityList entityList = (EntityList) LEVEL_CHUNK_ENTITIES.get(chunk);

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import java.util.LinkedList;
7575
import java.util.List;
7676
import java.util.Map;
77+
import java.util.Optional;
7778
import java.util.concurrent.CompletableFuture;
7879
import java.util.concurrent.ExecutionException;
7980
import java.util.concurrent.Semaphore;
@@ -669,12 +670,16 @@ static List<Entity> getEntities(LevelChunk chunk) {
669670
ExceptionCollector<RuntimeException> collector = new ExceptionCollector<>();
670671
if (PaperLib.isPaper()) {
671672
if (POST_CHUNK_REWRITE) {
672-
try {
673-
//noinspection unchecked
674-
return (List<Entity>) PAPER_CHUNK_GEN_ALL_ENTITIES.invoke(chunk.level.getEntityLookup().getChunk(chunk.locX, chunk.locZ));
675-
} catch (IllegalAccessException | InvocationTargetException e) {
676-
throw new RuntimeException("Failed to lookup entities [POST_CHUNK_REWRITE=true]", e);
677-
}
673+
return Optional.ofNullable(chunk.level
674+
.getEntityLookup()
675+
.getChunk(chunk.locX, chunk.locZ)).map(c -> {
676+
try {
677+
//noinspection unchecked
678+
return (List<Entity>) PAPER_CHUNK_GEN_ALL_ENTITIES.invoke(c);
679+
} catch (IllegalAccessException | InvocationTargetException e) {
680+
throw new RuntimeException("Failed to lookup entities [PAPER=true]", e);
681+
}
682+
}).orElse(Collections.emptyList());
678683
}
679684
try {
680685
EntityList entityList = (EntityList) LEVEL_CHUNK_ENTITIES.get(chunk);

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import java.util.LinkedList;
7474
import java.util.List;
7575
import java.util.Map;
76+
import java.util.Optional;
7677
import java.util.concurrent.CompletableFuture;
7778
import java.util.concurrent.ExecutionException;
7879
import java.util.concurrent.Semaphore;
@@ -651,14 +652,16 @@ static void removeBeacon(BlockEntity beacon, LevelChunk levelChunk) {
651652

652653
static List<Entity> getEntities(LevelChunk chunk) {
653654
if (PaperLib.isPaper()) {
654-
try {
655-
//noinspection unchecked
656-
return (List<Entity>) PAPER_CHUNK_GEN_ALL_ENTITIES.invoke(chunk.level
657-
.moonrise$getEntityLookup()
658-
.getChunk(chunk.locX, chunk.locZ));
659-
} catch (IllegalAccessException | InvocationTargetException e) {
660-
throw new RuntimeException("Failed to lookup entities [PAPER=true]", e);
661-
}
655+
return Optional.ofNullable(chunk.level
656+
.moonrise$getEntityLookup()
657+
.getChunk(chunk.locX, chunk.locZ)).map(c -> {
658+
try {
659+
//noinspection unchecked
660+
return (List<Entity>) PAPER_CHUNK_GEN_ALL_ENTITIES.invoke(c);
661+
} catch (IllegalAccessException | InvocationTargetException e) {
662+
throw new RuntimeException("Failed to lookup entities [PAPER=true]", e);
663+
}
664+
}).orElse(Collections.emptyList());
662665
}
663666
try {
664667
//noinspection unchecked

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

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import net.minecraft.util.ZeroBitStorage;
3838
import net.minecraft.world.entity.Entity;
3939
import net.minecraft.world.level.ChunkPos;
40-
import net.minecraft.world.level.Level;
4140
import net.minecraft.world.level.LevelAccessor;
4241
import net.minecraft.world.level.biome.Biome;
4342
import net.minecraft.world.level.block.Block;
@@ -73,6 +72,7 @@
7372
import java.util.LinkedList;
7473
import java.util.List;
7574
import java.util.Map;
75+
import java.util.Optional;
7676
import java.util.concurrent.CompletableFuture;
7777
import java.util.concurrent.ExecutionException;
7878
import java.util.concurrent.Semaphore;
@@ -105,7 +105,6 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
105105

106106
private static final Logger LOGGER = LogManagerCompat.getLogger();
107107

108-
private static Method PAPER_CHUNK_GEN_ALL_ENTITIES;
109108
private static Field SERVER_LEVEL_ENTITY_MANAGER;
110109

111110
static final MethodHandle PALETTED_CONTAINER_GET;
@@ -153,6 +152,8 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
153152
fieldThreadingDetector.setAccessible(true);
154153
fieldLock = ThreadingDetector.class.getDeclaredField(Refraction.pickName("lock", "c"));
155154
fieldLock.setAccessible(true);
155+
SERVER_LEVEL_ENTITY_MANAGER = ServerLevel.class.getDeclaredField(Refraction.pickName("entityManager", "N"));
156+
SERVER_LEVEL_ENTITY_MANAGER.setAccessible(true);
156157
} else {
157158
// in paper, the used methods are synchronized properly
158159
fieldThreadingDetector = null;
@@ -179,16 +180,6 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
179180
fieldRemove = BlockEntity.class.getDeclaredField(Refraction.pickName("remove", "p"));
180181
fieldRemove.setAccessible(true);
181182

182-
try {
183-
Level.class.getDeclaredMethod("moonrise$getEntityLookup");
184-
PAPER_CHUNK_GEN_ALL_ENTITIES = ChunkEntitySlices.class.getDeclaredMethod("getAllEntities");
185-
PAPER_CHUNK_GEN_ALL_ENTITIES.setAccessible(true);
186-
} catch (NoSuchMethodException ignored) {
187-
// Non-Paper
188-
SERVER_LEVEL_ENTITY_MANAGER = ServerLevel.class.getDeclaredField(Refraction.pickName("entityManager", "N"));
189-
SERVER_LEVEL_ENTITY_MANAGER.setAccessible(true);
190-
}
191-
192183
Method palettedContaienrGet = PalettedContainer.class.getDeclaredMethod(
193184
Refraction.pickName("get", "a"),
194185
int.class
@@ -653,14 +644,9 @@ static void removeBeacon(BlockEntity beacon, LevelChunk levelChunk) {
653644

654645
static List<Entity> getEntities(LevelChunk chunk) {
655646
if (PaperLib.isPaper()) {
656-
try {
657-
//noinspection unchecked
658-
return (List<Entity>) PAPER_CHUNK_GEN_ALL_ENTITIES.invoke(chunk.level
659-
.moonrise$getEntityLookup()
660-
.getChunk(chunk.locX, chunk.locZ));
661-
} catch (IllegalAccessException | InvocationTargetException e) {
662-
throw new RuntimeException("Failed to lookup entities [PAPER=true]", e);
663-
}
647+
return Optional.ofNullable(chunk.level
648+
.moonrise$getEntityLookup()
649+
.getChunk(chunk.locX, chunk.locZ)).map(ChunkEntitySlices::getAllEntities).orElse(Collections.emptyList());
664650
}
665651
try {
666652
//noinspection unchecked

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@
3333
import net.minecraft.util.BitStorage;
3434
import net.minecraft.util.SimpleBitStorage;
3535
import net.minecraft.util.ThreadingDetector;
36-
import net.minecraft.util.Unit;
3736
import net.minecraft.util.ZeroBitStorage;
3837
import net.minecraft.world.entity.Entity;
3938
import net.minecraft.world.level.ChunkPos;
40-
import net.minecraft.world.level.Level;
4139
import net.minecraft.world.level.LevelAccessor;
4240
import net.minecraft.world.level.biome.Biome;
4341
import net.minecraft.world.level.block.Block;
@@ -73,6 +71,7 @@
7371
import java.util.LinkedList;
7472
import java.util.List;
7573
import java.util.Map;
74+
import java.util.Optional;
7675
import java.util.concurrent.CompletableFuture;
7776
import java.util.concurrent.ExecutionException;
7877
import java.util.concurrent.Semaphore;
@@ -105,7 +104,6 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
105104

106105
private static final Logger LOGGER = LogManagerCompat.getLogger();
107106

108-
private static Method PAPER_CHUNK_GEN_ALL_ENTITIES;
109107
private static Field SERVER_LEVEL_ENTITY_MANAGER;
110108

111109
static final MethodHandle PALETTED_CONTAINER_GET;
@@ -153,6 +151,8 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
153151
fieldThreadingDetector.setAccessible(true);
154152
fieldLock = ThreadingDetector.class.getDeclaredField(Refraction.pickName("lock", "c"));
155153
fieldLock.setAccessible(true);
154+
SERVER_LEVEL_ENTITY_MANAGER = ServerLevel.class.getDeclaredField(Refraction.pickName("entityManager", "O"));
155+
SERVER_LEVEL_ENTITY_MANAGER.setAccessible(true);
156156
} else {
157157
// in paper, the used methods are synchronized properly
158158
fieldThreadingDetector = null;
@@ -179,16 +179,6 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
179179
fieldRemove = BlockEntity.class.getDeclaredField(Refraction.pickName("remove", "p"));
180180
fieldRemove.setAccessible(true);
181181

182-
try {
183-
Level.class.getDeclaredMethod("moonrise$getEntityLookup");
184-
PAPER_CHUNK_GEN_ALL_ENTITIES = ChunkEntitySlices.class.getDeclaredMethod("getAllEntities");
185-
PAPER_CHUNK_GEN_ALL_ENTITIES.setAccessible(true);
186-
} catch (NoSuchMethodException ignored) {
187-
// Non-Paper
188-
SERVER_LEVEL_ENTITY_MANAGER = ServerLevel.class.getDeclaredField(Refraction.pickName("entityManager", "O"));
189-
SERVER_LEVEL_ENTITY_MANAGER.setAccessible(true);
190-
}
191-
192182
Method palettedContainerGet = PalettedContainer.class.getDeclaredMethod(
193183
Refraction.pickName("get", "a"),
194184
int.class
@@ -653,14 +643,9 @@ static void removeBeacon(BlockEntity beacon, LevelChunk levelChunk) {
653643

654644
static List<Entity> getEntities(LevelChunk chunk) {
655645
if (PaperLib.isPaper()) {
656-
try {
657-
//noinspection unchecked
658-
return (List<Entity>) PAPER_CHUNK_GEN_ALL_ENTITIES.invoke(chunk.level
659-
.moonrise$getEntityLookup()
660-
.getChunk(chunk.locX, chunk.locZ));
661-
} catch (IllegalAccessException | InvocationTargetException e) {
662-
throw new RuntimeException("Failed to lookup entities [PAPER=true]", e);
663-
}
646+
return Optional.ofNullable(chunk.level
647+
.moonrise$getEntityLookup()
648+
.getChunk(chunk.locX, chunk.locZ)).map(ChunkEntitySlices::getAllEntities).orElse(Collections.emptyList());
664649
}
665650
try {
666651
//noinspection unchecked

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

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import net.minecraft.util.ZeroBitStorage;
4040
import net.minecraft.world.entity.Entity;
4141
import net.minecraft.world.level.ChunkPos;
42-
import net.minecraft.world.level.Level;
4342
import net.minecraft.world.level.LevelAccessor;
4443
import net.minecraft.world.level.biome.Biome;
4544
import net.minecraft.world.level.block.Block;
@@ -78,6 +77,7 @@
7877
import java.util.LinkedList;
7978
import java.util.List;
8079
import java.util.Map;
80+
import java.util.Optional;
8181
import java.util.concurrent.CompletableFuture;
8282
import java.util.concurrent.ExecutionException;
8383
import java.util.concurrent.Semaphore;
@@ -110,7 +110,6 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
110110

111111
private static final Logger LOGGER = LogManagerCompat.getLogger();
112112

113-
private static Method PAPER_CHUNK_GEN_ALL_ENTITIES;
114113
private static Field SERVER_LEVEL_ENTITY_MANAGER;
115114

116115
static final MethodHandle PALETTED_CONTAINER_GET;
@@ -158,6 +157,8 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
158157
fieldThreadingDetector.setAccessible(true);
159158
fieldLock = ThreadingDetector.class.getDeclaredField(Refraction.pickName("lock", "c"));
160159
fieldLock.setAccessible(true);
160+
SERVER_LEVEL_ENTITY_MANAGER = ServerLevel.class.getDeclaredField(Refraction.pickName("entityManager", "P"));
161+
SERVER_LEVEL_ENTITY_MANAGER.setAccessible(true);
161162
} else {
162163
// in paper, the used methods are synchronized properly
163164
fieldThreadingDetector = null;
@@ -184,16 +185,6 @@ public final class PaperweightPlatformAdapter extends NMSAdapter {
184185
fieldRemove = BlockEntity.class.getDeclaredField(Refraction.pickName("remove", "p"));
185186
fieldRemove.setAccessible(true);
186187

187-
try {
188-
Level.class.getDeclaredMethod("moonrise$getEntityLookup");
189-
PAPER_CHUNK_GEN_ALL_ENTITIES = ChunkEntitySlices.class.getDeclaredMethod("getAllEntities");
190-
PAPER_CHUNK_GEN_ALL_ENTITIES.setAccessible(true);
191-
} catch (NoSuchMethodException ignored) {
192-
// Non-Paper
193-
SERVER_LEVEL_ENTITY_MANAGER = ServerLevel.class.getDeclaredField(Refraction.pickName("entityManager", "P"));
194-
SERVER_LEVEL_ENTITY_MANAGER.setAccessible(true);
195-
}
196-
197188
Method palettedContainerGet = PalettedContainer.class.getDeclaredMethod(
198189
Refraction.pickName("get", "a"),
199190
int.class
@@ -681,14 +672,9 @@ static void removeBeacon(BlockEntity beacon, LevelChunk levelChunk) {
681672

682673
static List<Entity> getEntities(LevelChunk chunk) {
683674
if (PaperLib.isPaper()) {
684-
try {
685-
//noinspection unchecked
686-
return (List<Entity>) PAPER_CHUNK_GEN_ALL_ENTITIES.invoke(chunk.level
687-
.moonrise$getEntityLookup()
688-
.getChunk(chunk.locX, chunk.locZ));
689-
} catch (IllegalAccessException | InvocationTargetException e) {
690-
throw new RuntimeException("Failed to lookup entities [PAPER=true]", e);
691-
}
675+
return Optional.ofNullable(chunk.level
676+
.moonrise$getEntityLookup()
677+
.getChunk(chunk.locX, chunk.locZ)).map(ChunkEntitySlices::getAllEntities).orElse(Collections.emptyList());
692678
}
693679
try {
694680
//noinspection unchecked

0 commit comments

Comments
 (0)