Skip to content

Commit f86192c

Browse files
authored
feat: add option to skip entity events when creating (#3253)
1 parent 3a749cd commit f86192c

File tree

41 files changed

+420
-139
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+420
-139
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,8 @@ private ResourceKey<LevelStem> getWorldDimKey(Environment env) {
872872
SideEffect.HISTORY,
873873
SideEffect.HEIGHTMAPS,
874874
SideEffect.LIGHTING,
875-
SideEffect.NEIGHBORS
875+
SideEffect.NEIGHBORS,
876+
SideEffect.ENTITY_EVENTS
876877
//FAWE end
877878
);
878879

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ public BaseBlock getFullBlock(final Location location) {
333333
SideEffect.HISTORY,
334334
SideEffect.HEIGHTMAPS,
335335
SideEffect.LIGHTING,
336-
SideEffect.NEIGHBORS
336+
SideEffect.NEIGHBORS,
337+
SideEffect.ENTITY_EVENTS
337338
);
338339

339340
@Override

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -670,15 +670,34 @@ protected <T extends Future<T>> T internalCall(
670670
entity.load(tag);
671671
entity.absMoveTo(x, y, z, yaw, pitch);
672672
entity.setUUID(NbtUtils.uuid(nativeTag));
673+
Runnable onError = () -> LOGGER.warn(
674+
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
675+
id,
676+
nmsWorld.getWorld().getName(),
677+
x,
678+
y,
679+
z
680+
);
681+
if (!set.getSideEffectSet().shouldApply(SideEffect.ENTITY_EVENTS)) {
682+
entity.spawnReason = CreatureSpawnEvent.SpawnReason.CUSTOM;
683+
entity.generation = false;
684+
if (PaperLib.isPaper()) {
685+
if (!nmsWorld.getEntityLookup().addNewEntity(entity)) {
686+
onError.run();
687+
}
688+
continue;
689+
}
690+
// Not paper
691+
try {
692+
PaperweightPlatformAdapter.getEntitySectionManager(nmsWorld).addNewEntity(entity);
693+
continue;
694+
} catch (IllegalAccessException e) {
695+
// Fallback
696+
LOGGER.warn("Error bypassing entity events on spawn on Spigot", e);
697+
}
698+
}
673699
if (!nmsWorld.addFreshEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
674-
LOGGER.warn(
675-
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
676-
id,
677-
nmsWorld.getWorld().getName(),
678-
x,
679-
y,
680-
z
681-
);
700+
onError.run();
682701
// Unsuccessful create should not be saved to history
683702
iterator.remove();
684703
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,15 +694,22 @@ static List<Entity> getEntities(LevelChunk chunk) {
694694
}
695695
}
696696
try {
697-
//noinspection unchecked
698-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
697+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
699698
} catch (IllegalAccessException e) {
700699
collector.add(new RuntimeException("Failed to lookup entities [PAPER=false]", e));
701700
}
702701
collector.throwIfPresent();
703702
return List.of();
704703
}
705704

705+
/**
706+
* Spigot only
707+
*/
708+
static PersistentEntitySectionManager<Entity> getEntitySectionManager(ServerLevel level) throws IllegalAccessException {
709+
//noinspection unchecked
710+
return (PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(level));
711+
}
712+
706713
record FakeIdMapBlock(int size) implements IdMap<net.minecraft.world.level.block.state.BlockState> {
707714

708715
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,8 @@ private ResourceKey<LevelStem> getWorldDimKey(Environment env) {
871871
SideEffect.HISTORY,
872872
SideEffect.HEIGHTMAPS,
873873
SideEffect.LIGHTING,
874-
SideEffect.NEIGHBORS
874+
SideEffect.NEIGHBORS,
875+
SideEffect.ENTITY_EVENTS
875876
//FAWE end
876877
);
877878

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ public BaseBlock getFullBlock(final Location location) {
332332
SideEffect.HISTORY,
333333
SideEffect.HEIGHTMAPS,
334334
SideEffect.LIGHTING,
335-
SideEffect.NEIGHBORS
335+
SideEffect.NEIGHBORS,
336+
SideEffect.ENTITY_EVENTS
336337
);
337338

338339
@Override

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -671,15 +671,34 @@ protected <T extends Future<T>> T internalCall(
671671
entity.load(tag);
672672
entity.absMoveTo(x, y, z, yaw, pitch);
673673
entity.setUUID(NbtUtils.uuid(nativeTag));
674+
Runnable onError = () -> LOGGER.warn(
675+
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
676+
id,
677+
nmsWorld.getWorld().getName(),
678+
x,
679+
y,
680+
z
681+
);
682+
if (!set.getSideEffectSet().shouldApply(SideEffect.ENTITY_EVENTS)) {
683+
entity.spawnReason = CreatureSpawnEvent.SpawnReason.CUSTOM;
684+
entity.generation = false;
685+
if (PaperLib.isPaper()) {
686+
if (!nmsWorld.getEntityLookup().addNewEntity(entity)) {
687+
onError.run();
688+
}
689+
continue;
690+
}
691+
// Not paper
692+
try {
693+
PaperweightPlatformAdapter.getEntitySectionManager(nmsWorld).addNewEntity(entity);
694+
continue;
695+
} catch (IllegalAccessException e) {
696+
// Fallback
697+
LOGGER.warn("Error bypassing entity events on spawn on Spigot", e);
698+
}
699+
}
674700
if (!nmsWorld.addFreshEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
675-
LOGGER.warn(
676-
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
677-
id,
678-
nmsWorld.getWorld().getName(),
679-
x,
680-
y,
681-
z
682-
);
701+
onError.run();
683702
// Unsuccessful create should not be saved to history
684703
iterator.remove();
685704
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,15 +694,22 @@ static List<Entity> getEntities(LevelChunk chunk) {
694694
}
695695
}
696696
try {
697-
//noinspection unchecked
698-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
697+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
699698
} catch (IllegalAccessException e) {
700699
collector.add(new RuntimeException("Failed to lookup entities [PAPER=false]", e));
701700
}
702701
collector.throwIfPresent();
703702
return List.of();
704703
}
705704

705+
/**
706+
* Spigot only
707+
*/
708+
static PersistentEntitySectionManager<Entity> getEntitySectionManager(ServerLevel level) throws IllegalAccessException {
709+
//noinspection unchecked
710+
return (PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(level));
711+
}
712+
706713
record FakeIdMapBlock(int size) implements IdMap<net.minecraft.world.level.block.state.BlockState> {
707714

708715
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,8 @@ private ResourceKey<LevelStem> getWorldDimKey(Environment env) {
895895
SideEffect.HISTORY,
896896
SideEffect.HEIGHTMAPS,
897897
SideEffect.LIGHTING,
898-
SideEffect.NEIGHBORS
898+
SideEffect.NEIGHBORS,
899+
SideEffect.ENTITY_EVENTS
899900
//FAWE end
900901
);
901902

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ public BaseBlock getFullBlock(final Location location) {
340340
SideEffect.HISTORY,
341341
SideEffect.HEIGHTMAPS,
342342
SideEffect.LIGHTING,
343-
SideEffect.NEIGHBORS
343+
SideEffect.NEIGHBORS,
344+
SideEffect.ENTITY_EVENTS
344345
);
345346

346347
@Override

0 commit comments

Comments
 (0)