Skip to content

Commit 070e1a9

Browse files
committed
feat: add option to skip entity events when creating
- untested right now as I cannot compile
1 parent 92a7185 commit 070e1a9

File tree

18 files changed

+260
-76
lines changed

18 files changed

+260
-76
lines changed

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -681,15 +681,32 @@ protected <T extends Future<T>> T internalCall(
681681
entity.load(tag);
682682
entity.absMoveTo(x, y, z, yaw, pitch);
683683
entity.setUUID(NbtUtils.uuid(nativeTag));
684+
Runnable onError = () -> LOGGER.warn(
685+
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
686+
id,
687+
nmsWorld.getWorld().getName(),
688+
x,
689+
y,
690+
z
691+
);
692+
if (!set.getSideEffectSet().shouldApply(SideEffect.ENTITY_EVENTS)) {
693+
if (PaperLib.isPaper()) {
694+
if (!nmsWorld.getEntityLookup().addNewEntity(entity)) {
695+
onError.run();
696+
}
697+
return;
698+
}
699+
// Not paper
700+
try {
701+
PaperweightPlatformAdapter.getEntitySectionManager(nmsWorld).addNewEntity(entity);
702+
return;
703+
} catch (IllegalAccessException e) {
704+
// Fallback
705+
LOGGER.warn("Error bypassing entity events on spawn on Spigot", e);
706+
}
707+
}
684708
if (!nmsWorld.addFreshEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
685-
LOGGER.warn(
686-
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
687-
id,
688-
nmsWorld.getWorld().getName(),
689-
x,
690-
y,
691-
z
692-
);
709+
onError.run();
693710
// Unsuccessful create should not be saved to history
694711
iterator.remove();
695712
}

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
@@ -696,15 +696,22 @@ static List<Entity> getEntities(LevelChunk chunk) {
696696
}
697697
}
698698
try {
699-
//noinspection unchecked
700-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
699+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
701700
} catch (IllegalAccessException e) {
702701
collector.add(new RuntimeException("Failed to lookup entities [PAPER=false]", e));
703702
}
704703
collector.throwIfPresent();
705704
return List.of();
706705
}
707706

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

710717
@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: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -682,15 +682,32 @@ protected <T extends Future<T>> T internalCall(
682682
entity.load(tag);
683683
entity.absMoveTo(x, y, z, yaw, pitch);
684684
entity.setUUID(NbtUtils.uuid(nativeTag));
685+
Runnable onError = () -> LOGGER.warn(
686+
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
687+
id,
688+
nmsWorld.getWorld().getName(),
689+
x,
690+
y,
691+
z
692+
);
693+
if (!set.getSideEffectSet().shouldApply(SideEffect.ENTITY_EVENTS)) {
694+
if (PaperLib.isPaper()) {
695+
if (!nmsWorld.getEntityLookup().addNewEntity(entity)) {
696+
onError.run();
697+
}
698+
return;
699+
}
700+
// Not paper
701+
try {
702+
PaperweightPlatformAdapter.getEntitySectionManager(nmsWorld).addNewEntity(entity);
703+
return;
704+
} catch (IllegalAccessException e) {
705+
// Fallback
706+
LOGGER.warn("Error bypassing entity events on spawn on Spigot", e);
707+
}
708+
}
685709
if (!nmsWorld.addFreshEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
686-
LOGGER.warn(
687-
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
688-
id,
689-
nmsWorld.getWorld().getName(),
690-
x,
691-
y,
692-
z
693-
);
710+
onError.run();
694711
// Unsuccessful create should not be saved to history
695712
iterator.remove();
696713
}

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
@@ -696,15 +696,22 @@ static List<Entity> getEntities(LevelChunk chunk) {
696696
}
697697
}
698698
try {
699-
//noinspection unchecked
700-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
699+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
701700
} catch (IllegalAccessException e) {
702701
collector.add(new RuntimeException("Failed to lookup entities [PAPER=false]", e));
703702
}
704703
collector.throwIfPresent();
705704
return List.of();
706705
}
707706

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

710717
@Override

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -683,15 +683,32 @@ protected <T extends Future<T>> T internalCall(
683683
entity.load(tag);
684684
entity.absMoveTo(x, y, z, yaw, pitch);
685685
entity.setUUID(NbtUtils.uuid(nativeTag));
686+
Runnable onError = () -> LOGGER.warn(
687+
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
688+
id,
689+
nmsWorld.getWorld().getName(),
690+
x,
691+
y,
692+
z
693+
);
694+
if (!set.getSideEffectSet().shouldApply(SideEffect.ENTITY_EVENTS)) {
695+
if (PaperLib.isPaper()) {
696+
if (!nmsWorld.getEntityLookup().addNewEntity(entity)) {
697+
onError.run();
698+
}
699+
return;
700+
}
701+
// Not paper
702+
try {
703+
PaperweightPlatformAdapter.getEntitySectionManager(nmsWorld).addNewEntity(entity);
704+
return;
705+
} catch (IllegalAccessException e) {
706+
// Fallback
707+
LOGGER.warn("Error bypassing entity events on spawn on Spigot", e);
708+
}
709+
}
686710
if (!nmsWorld.addFreshEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
687-
LOGGER.warn(
688-
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
689-
id,
690-
nmsWorld.getWorld().getName(),
691-
x,
692-
y,
693-
z
694-
);
711+
onError.run();
695712
// Unsuccessful create should not be saved to history
696713
iterator.remove();
697714
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,15 +690,22 @@ static List<Entity> getEntities(LevelChunk chunk) {
690690
}
691691
}
692692
try {
693-
//noinspection unchecked
694-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
693+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
695694
} catch (IllegalAccessException e) {
696695
collector.add(new RuntimeException("Failed to lookup entities [PAPER=false]", e));
697696
}
698697
collector.throwIfPresent();
699698
return List.of();
700699
}
701700

701+
/**
702+
* Spigot only
703+
*/
704+
static PersistentEntitySectionManager<Entity> getEntitySectionManager(ServerLevel level) throws IllegalAccessException {
705+
//noinspection unchecked
706+
return (PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(level));
707+
}
708+
702709
record FakeIdMapBlock(int size) implements IdMap<net.minecraft.world.level.block.state.BlockState> {
703710

704711
@Override

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -680,15 +680,32 @@ protected <T extends Future<T>> T internalCall(
680680
entity.load(tag);
681681
entity.absMoveTo(x, y, z, yaw, pitch);
682682
entity.setUUID(NbtUtils.uuid(nativeTag));
683+
Runnable onError = () -> LOGGER.warn(
684+
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
685+
id,
686+
nmsWorld.getWorld().getName(),
687+
x,
688+
y,
689+
z
690+
);
691+
if (!set.getSideEffectSet().shouldApply(SideEffect.ENTITY_EVENTS)) {
692+
if (PaperLib.isPaper()) {
693+
if (!nmsWorld.moonrise$getEntityLookup().addNewEntity(entity)) {
694+
onError.run();
695+
}
696+
return;
697+
}
698+
// Not paper
699+
try {
700+
PaperweightPlatformAdapter.getEntitySectionManager(nmsWorld).addNewEntity(entity);
701+
return;
702+
} catch (IllegalAccessException e) {
703+
// Fallback
704+
LOGGER.warn("Error bypassing entity events on spawn on Spigot", e);
705+
}
706+
}
683707
if (!nmsWorld.addFreshEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
684-
LOGGER.warn(
685-
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
686-
id,
687-
nmsWorld.getWorld().getName(),
688-
x,
689-
y,
690-
z
691-
);
708+
onError.run();
692709
// Unsuccessful create should not be saved to history
693710
iterator.remove();
694711
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,13 +664,20 @@ static List<Entity> getEntities(LevelChunk chunk) {
664664
}).orElse(Collections.emptyList());
665665
}
666666
try {
667-
//noinspection unchecked
668-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
667+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
669668
} catch (IllegalAccessException e) {
670669
throw new RuntimeException("Failed to lookup entities [PAPER=false]", e);
671670
}
672671
}
673672

673+
/**
674+
* Spigot only
675+
*/
676+
static PersistentEntitySectionManager<Entity> getEntitySectionManager(ServerLevel level) throws IllegalAccessException {
677+
//noinspection unchecked
678+
return (PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(level));
679+
}
680+
674681
record FakeIdMapBlock(int size) implements IdMap<net.minecraft.world.level.block.state.BlockState> {
675682

676683
@Override

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -676,15 +676,32 @@ protected <T extends Future<T>> T internalCall(
676676
entity.load(tag);
677677
entity.absMoveTo(x, y, z, yaw, pitch);
678678
entity.setUUID(NbtUtils.uuid(nativeTag));
679+
Runnable onError = () -> LOGGER.warn(
680+
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
681+
id,
682+
nmsWorld.getWorld().getName(),
683+
x,
684+
y,
685+
z
686+
);
687+
if (!set.getSideEffectSet().shouldApply(SideEffect.ENTITY_EVENTS)) {
688+
if (PaperLib.isPaper()) {
689+
if (!nmsWorld.moonrise$getEntityLookup().addNewEntity(entity)) {
690+
onError.run();
691+
}
692+
return;
693+
}
694+
// Not paper
695+
try {
696+
PaperweightPlatformAdapter.getEntitySectionManager(nmsWorld).addNewEntity(entity);
697+
return;
698+
} catch (IllegalAccessException e) {
699+
// Fallback
700+
LOGGER.warn("Error bypassing entity events on spawn on Spigot", e);
701+
}
702+
}
679703
if (!nmsWorld.addFreshEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
680-
LOGGER.warn(
681-
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
682-
id,
683-
nmsWorld.getWorld().getName(),
684-
x,
685-
y,
686-
z
687-
);
704+
onError.run();
688705
// Unsuccessful create should not be saved to history
689706
iterator.remove();
690707
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,13 +649,20 @@ static List<Entity> getEntities(LevelChunk chunk) {
649649
.getChunk(chunk.locX, chunk.locZ)).map(ChunkEntitySlices::getAllEntities).orElse(Collections.emptyList());
650650
}
651651
try {
652-
//noinspection unchecked
653-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
652+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
654653
} catch (IllegalAccessException e) {
655654
throw new RuntimeException("Failed to lookup entities [PAPER=false]", e);
656655
}
657656
}
658657

658+
/**
659+
* Spigot only
660+
*/
661+
static PersistentEntitySectionManager<Entity> getEntitySectionManager(ServerLevel level) throws IllegalAccessException {
662+
//noinspection unchecked
663+
return (PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(level));
664+
}
665+
659666
record FakeIdMapBlock(int size) implements IdMap<net.minecraft.world.level.block.state.BlockState> {
660667

661668
@Override

0 commit comments

Comments
 (0)