Skip to content

Commit f14bba2

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

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
@@ -670,15 +670,32 @@ 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+
if (PaperLib.isPaper()) {
683+
if (!nmsWorld.getEntityLookup().addNewEntity(entity)) {
684+
onError.run();
685+
}
686+
return;
687+
}
688+
// Not paper
689+
try {
690+
PaperweightPlatformAdapter.getEntitySectionManager(nmsWorld).addNewEntity(entity);
691+
return;
692+
} catch (IllegalAccessException e) {
693+
// Fallback
694+
LOGGER.warn("Error bypassing entity events on spawn on Spigot", e);
695+
}
696+
}
673697
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-
);
698+
onError.run();
682699
// Unsuccessful create should not be saved to history
683700
iterator.remove();
684701
}

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/impl/fawe/v1_20_R3/PaperweightGetBlocks.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -671,15 +671,32 @@ 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+
if (PaperLib.isPaper()) {
684+
if (!nmsWorld.getEntityLookup().addNewEntity(entity)) {
685+
onError.run();
686+
}
687+
return;
688+
}
689+
// Not paper
690+
try {
691+
PaperweightPlatformAdapter.getEntitySectionManager(nmsWorld).addNewEntity(entity);
692+
return;
693+
} catch (IllegalAccessException e) {
694+
// Fallback
695+
LOGGER.warn("Error bypassing entity events on spawn on Spigot", e);
696+
}
697+
}
674698
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-
);
699+
onError.run();
683700
// Unsuccessful create should not be saved to history
684701
iterator.remove();
685702
}

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/impl/fawe/v1_20_R4/PaperweightGetBlocks.java

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

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
@@ -688,15 +688,22 @@ static List<Entity> getEntities(LevelChunk chunk) {
688688
}
689689
}
690690
try {
691-
//noinspection unchecked
692-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
691+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
693692
} catch (IllegalAccessException e) {
694693
collector.add(new RuntimeException("Failed to lookup entities [PAPER=false]", e));
695694
}
696695
collector.throwIfPresent();
697696
return List.of();
698697
}
699698

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

702709
@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
@@ -668,13 +668,20 @@ static List<Entity> getEntities(LevelChunk chunk) {
668668
}).orElse(Collections.emptyList());
669669
}
670670
try {
671-
//noinspection unchecked
672-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
671+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
673672
} catch (IllegalAccessException e) {
674673
throw new RuntimeException("Failed to lookup entities [PAPER=false]", e);
675674
}
676675
}
677676

677+
/**
678+
* Spigot only
679+
*/
680+
static PersistentEntitySectionManager<Entity> getEntitySectionManager(ServerLevel level) throws IllegalAccessException {
681+
//noinspection unchecked
682+
return (PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(level));
683+
}
684+
678685
record FakeIdMapBlock(int size) implements IdMap<net.minecraft.world.level.block.state.BlockState> {
679686

680687
@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
@@ -665,15 +665,32 @@ protected <T extends Future<T>> T internalCall(
665665
entity.load(tag);
666666
entity.absMoveTo(x, y, z, yaw, pitch);
667667
entity.setUUID(NbtUtils.uuid(nativeTag));
668+
Runnable onError = () -> LOGGER.warn(
669+
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
670+
id,
671+
nmsWorld.getWorld().getName(),
672+
x,
673+
y,
674+
z
675+
);
676+
if (!set.getSideEffectSet().shouldApply(SideEffect.ENTITY_EVENTS)) {
677+
if (PaperLib.isPaper()) {
678+
if (!nmsWorld.moonrise$getEntityLookup().addNewEntity(entity)) {
679+
onError.run();
680+
}
681+
return;
682+
}
683+
// Not paper
684+
try {
685+
PaperweightPlatformAdapter.getEntitySectionManager(nmsWorld).addNewEntity(entity);
686+
return;
687+
} catch (IllegalAccessException e) {
688+
// Fallback
689+
LOGGER.warn("Error bypassing entity events on spawn on Spigot", e);
690+
}
691+
}
668692
if (!nmsWorld.addFreshEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
669-
LOGGER.warn(
670-
"Error creating entity of type `{}` in world `{}` at location `{},{},{}`",
671-
id,
672-
nmsWorld.getWorld().getName(),
673-
x,
674-
y,
675-
z
676-
);
693+
onError.run();
677694
// Unsuccessful create should not be saved to history
678695
iterator.remove();
679696
}

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
@@ -647,13 +647,20 @@ static List<Entity> getEntities(LevelChunk chunk) {
647647
.getChunk(chunk.locX, chunk.locZ)).map(ChunkEntitySlices::getAllEntities).orElse(Collections.emptyList());
648648
}
649649
try {
650-
//noinspection unchecked
651-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
650+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
652651
} catch (IllegalAccessException e) {
653652
throw new RuntimeException("Failed to lookup entities [PAPER=false]", e);
654653
}
655654
}
656655

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

659666
@Override

0 commit comments

Comments
 (0)