Search in sources :

Example 36 with StackFrame

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();
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) IMixinBlockPos(org.spongepowered.common.interfaces.util.math.IMixinBlockPos) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) ArrayList(java.util.ArrayList) NonNullArrayList(org.spongepowered.common.util.NonNullArrayList) World(net.minecraft.world.World) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) Inject(org.spongepowered.asm.mixin.injection.Inject) Final(org.spongepowered.asm.mixin.Final)

Aggregations

StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)36 Entity (org.spongepowered.api.entity.Entity)18 ArrayList (java.util.ArrayList)15 SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)13 User (org.spongepowered.api.entity.living.player.User)12 EntityItem (net.minecraft.entity.item.EntityItem)11 EntityPlayer (net.minecraft.entity.player.EntityPlayer)8 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)8 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)6 ItemStack (net.minecraft.item.ItemStack)5 BlockPos (net.minecraft.util.math.BlockPos)5 WorldServer (net.minecraft.world.WorldServer)5 LocatableBlock (org.spongepowered.api.world.LocatableBlock)5 IMixinTileEntity (org.spongepowered.common.interfaces.block.tile.IMixinTileEntity)5 List (java.util.List)4 UUID (java.util.UUID)4 Collectors (java.util.stream.Collectors)4 EntityLivingBase (net.minecraft.entity.EntityLivingBase)4 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)4 Sponge (org.spongepowered.api.Sponge)4