Skip to content

Commit e0c24df

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

File tree

18 files changed

+260
-77
lines changed

18 files changed

+260
-77
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
@@ -692,15 +692,22 @@ static List<Entity> getEntities(LevelChunk chunk) {
692692
}
693693
}
694694
try {
695-
//noinspection unchecked
696-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
695+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
697696
} catch (IllegalAccessException e) {
698697
collector.add(new RuntimeException("Failed to lookup entities [PAPER=false]", e));
699698
}
700699
collector.throwIfPresent();
701700
return List.of();
702701
}
703702

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

706713
@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
@@ -692,15 +692,22 @@ static List<Entity> getEntities(LevelChunk chunk) {
692692
}
693693
}
694694
try {
695-
//noinspection unchecked
696-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
695+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
697696
} catch (IllegalAccessException e) {
698697
collector.add(new RuntimeException("Failed to lookup entities [PAPER=false]", e));
699698
}
700699
collector.throwIfPresent();
701700
return List.of();
702701
}
703702

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

706713
@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
@@ -685,15 +685,22 @@ static List<Entity> getEntities(LevelChunk chunk) {
685685
}
686686
}
687687
try {
688-
//noinspection unchecked
689-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
688+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
690689
} catch (IllegalAccessException e) {
691690
collector.add(new RuntimeException("Failed to lookup entities [PAPER=false]", e));
692691
}
693692
collector.throwIfPresent();
694693
return List.of();
695694
}
696695

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

699706
@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
@@ -661,13 +661,20 @@ static List<Entity> getEntities(LevelChunk chunk) {
661661
}
662662
}
663663
try {
664-
//noinspection unchecked
665-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
664+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
666665
} catch (IllegalAccessException e) {
667666
throw new RuntimeException("Failed to lookup entities [PAPER=false]", e);
668667
}
669668
}
670669

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

673680
@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
@@ -663,13 +663,20 @@ static List<Entity> getEntities(LevelChunk chunk) {
663663
}
664664
}
665665
try {
666-
//noinspection unchecked
667-
return ((PersistentEntitySectionManager<Entity>) (SERVER_LEVEL_ENTITY_MANAGER.get(chunk.level))).getEntities(chunk.getPos());
666+
return getEntitySectionManager(chunk.level).getEntities(chunk.getPos());
668667
} catch (IllegalAccessException e) {
669668
throw new RuntimeException("Failed to lookup entities [PAPER=false]", e);
670669
}
671670
}
672671

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

675682
@Override

0 commit comments

Comments
 (0)