use of org.spongepowered.api.event.CauseStackManager.StackFrame in project SpongeCommon by SpongePowered.
the class MixinWorldServer method spongeLoadEntities.
/**
* @author gabizou - February 7th, 2016
* @author gabizou - September 3rd, 2016 - Moved from MixinWorld since WorldServer overrides the method.
*
* This will short circuit all other patches such that we control the
* entities being loaded by chunkloading and can throw our bulk entity
* event. This will bypass Forge's hook for individual entity events,
* but the SpongeModEventManager will still successfully throw the
* appropriate event and cancel the entities otherwise contained.
*
* @param entities The entities being loaded
* @param callbackInfo The callback info
*/
@Final
@Inject(method = "loadEntities", at = @At("HEAD"), cancellable = true)
private void spongeLoadEntities(Collection<net.minecraft.entity.Entity> entities, CallbackInfo callbackInfo) {
if (entities.isEmpty()) {
// just return, no entities to load!
callbackInfo.cancel();
return;
}
List<Entity> entityList = new ArrayList<>();
for (net.minecraft.entity.Entity entity : entities) {
// Make sure no entities load in invalid positions
if (((IMixinBlockPos) entity.getPosition()).isInvalidYPosition()) {
entity.setDead();
continue;
}
if (this.canAddEntity(entity)) {
entityList.add((Entity) entity);
}
}
try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.CHUNK_LOAD);
Sponge.getCauseStackManager().pushCause(this);
SpawnEntityEvent.ChunkLoad chunkLoad = SpongeEventFactory.createSpawnEntityEventChunkLoad(Sponge.getCauseStackManager().getCurrentCause(), Lists.newArrayList(entityList));
SpongeImpl.postEvent(chunkLoad);
if (!chunkLoad.isCancelled() && chunkLoad.getEntities().size() > 0) {
for (Entity successful : chunkLoad.getEntities()) {
this.loadedEntityList.add((net.minecraft.entity.Entity) successful);
this.onEntityAdded((net.minecraft.entity.Entity) successful);
}
}
// This prevents invisible entities from loading into the world and blocking the position.
for (Entity entity : entityList) {
if (!chunkLoad.getEntities().contains(entity)) {
((net.minecraft.world.World) (Object) this).removeEntityDangerously((net.minecraft.entity.Entity) entity);
}
}
callbackInfo.cancel();
}
}
Aggregations