Search in sources :

Example 26 with SpawnEntityEvent

use of org.spongepowered.api.event.entity.SpawnEntityEvent in project LanternServer by LanternPowered.

the class VanillaContainerInteractionBehavior method updateCraftingGrid.

private void updateCraftingGrid(Player player, CraftingInventory craftingInventory, MatrixResult matrixResult, List<SlotTransaction> transactions) {
    final CraftingMatrix matrix = matrixResult.getCraftingMatrix();
    final CraftingGridInventory grid = craftingInventory.getCraftingGrid();
    for (int x = 0; x < matrix.width(); x++) {
        for (int y = 0; y < matrix.height(); y++) {
            final ItemStack itemStack = matrix.get(x, y);
            final Slot slot = grid.getSlot(x, y).get();
            transactions.add(new SlotTransaction(slot, slot.peek().map(LanternItemStackSnapshot::wrap).orElse(LanternItemStackSnapshot.none()), LanternItemStackSnapshot.wrap(itemStack)));
        }
    }
    final CauseStack causeStack = CauseStack.current();
    causeStack.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
    final Transform<World> transform = player.getTransform();
    final List<Entity> entities = LanternEventHelper.handlePreDroppedItemSpawning(matrixResult.getRest().stream().map(itemStack -> new Tuple<ItemStackSnapshot, Transform<World>>(LanternItemStackSnapshot.wrap(itemStack), transform)).collect(Collectors.toList()));
    final SpawnEntityEvent event = SpongeEventFactory.createDropItemEventDispense(causeStack.getCurrentCause(), entities);
    Sponge.getEventManager().post(event);
    // Spawn all the entities in the world if the event isn't cancelled
    LanternWorld.finishSpawnEntityEvent(event);
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) Entity(org.spongepowered.api.entity.Entity) CraftingMatrix(org.lanternpowered.server.item.recipe.crafting.CraftingMatrix) World(org.spongepowered.api.world.World) LanternWorld(org.lanternpowered.server.world.LanternWorld) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) OutputSlot(org.spongepowered.api.item.inventory.slot.OutputSlot) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) ClientSlot(org.lanternpowered.server.inventory.client.ClientSlot) AbstractInventorySlot(org.lanternpowered.server.inventory.AbstractInventorySlot) Slot(org.spongepowered.api.item.inventory.Slot) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) LanternItemStackSnapshot(org.lanternpowered.server.inventory.LanternItemStackSnapshot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) Transform(org.spongepowered.api.entity.Transform) CraftingGridInventory(org.spongepowered.api.item.inventory.crafting.CraftingGridInventory)

Example 27 with SpawnEntityEvent

use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.

the class IPhaseState method spawnEntityOrCapture.

/**
 * This is Step 3 of entity spawning. It is used for the sole purpose of capturing an entity spawn
 * and doesn't actually spawn an entity into the world until the current phase is unwound.
 * The method itself should technically capture entity spawns, however, in the event it
 * is required that the entity cannot be captured, returning {@code false} will mark it
 * to spawn into the world, bypassing any of the bulk spawn events or capturing.
 *
 * <p>NOTE: This method should only be called and handled if and only if {@link IPhaseState#allowEntitySpawns()}
 * returns {@code true}. Violation of this will have unforseen consequences.</p>
 *
 * @param context The current context
 * @param entity The entity being captured
 * @param chunkX The chunk x position
 * @param chunkZ The chunk z position
 * @return True if the entity was successfully captured
 */
default boolean spawnEntityOrCapture(C context, org.spongepowered.api.entity.Entity entity, int chunkX, int chunkZ) {
    final User user = context.getNotifier().orElseGet(() -> context.getOwner().orElse(null));
    if (user != null) {
        entity.setCreator(user.getUniqueId());
    }
    final ArrayList<org.spongepowered.api.entity.Entity> entities = new ArrayList<>(1);
    entities.add(entity);
    final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
    SpongeImpl.postEvent(event);
    if (!event.isCancelled() && event.getEntities().size() > 0) {
        for (org.spongepowered.api.entity.Entity item : event.getEntities()) {
            ((IMixinWorldServer) item.getWorld()).forceSpawnEntity(item);
        }
        return true;
    }
    return false;
}
Also used : Entity(net.minecraft.entity.Entity) User(org.spongepowered.api.entity.living.player.User) ArrayList(java.util.ArrayList) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent)

Example 28 with SpawnEntityEvent

use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.

the class TrackingUtil method spawnEntitiesForBlock.

public static void spawnEntitiesForBlock(Collection<net.minecraft.entity.Entity> entities, BlockSnapshot newBlockSnapshot, PhaseContext<?> phaseContext, IPhaseState<?> phaseState) {
    // Now we can spawn the entity items appropriately
    final List<Entity> entitiesSpawned = entities.stream().map(EntityUtil::fromNative).collect(Collectors.toList());
    final Optional<User> owner = phaseContext.getOwner();
    final Optional<User> notifier = phaseContext.getNotifier();
    final User entityCreator = notifier.orElseGet(() -> owner.orElse(null));
    final SpawnEntityEvent destruct = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entitiesSpawned);
    SpongeImpl.postEvent(destruct);
    if (!destruct.isCancelled()) {
        for (Entity entity : destruct.getEntities()) {
            if (entityCreator != null) {
                EntityUtil.toMixin(entity).setCreator(entityCreator.getUniqueId());
            }
            EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
        }
    }
}
Also used : IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) Entity(org.spongepowered.api.entity.Entity) User(org.spongepowered.api.entity.living.player.User) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent)

Example 29 with SpawnEntityEvent

use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.

the class RestoringBlockPhaseState method spawnEntityOrCapture.

@Override
public boolean spawnEntityOrCapture(GeneralizedContext context, Entity entity, int chunkX, int chunkZ) {
    final User user = context.getNotifier().orElseGet(() -> context.getOwner().orElse(null));
    if (user != null) {
        entity.setCreator(user.getUniqueId());
    }
    final ArrayList<Entity> entities = new ArrayList<>(1);
    entities.add(entity);
    final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
    SpongeImpl.postEvent(event);
    if (!event.isCancelled() && event.getEntities().size() > 0) {
        for (org.spongepowered.api.entity.Entity item : event.getEntities()) {
            ((IMixinWorldServer) item.getWorld()).forceSpawnEntity(item);
        }
        return true;
    }
    return false;
}
Also used : Entity(org.spongepowered.api.entity.Entity) User(org.spongepowered.api.entity.living.player.User) ArrayList(java.util.ArrayList) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) Entity(org.spongepowered.api.entity.Entity)

Example 30 with SpawnEntityEvent

use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.

the class CommandState method unwind.

@Override
public void unwind(CommandPhaseContext phaseContext) {
    Optional<EntityPlayer> playerSource = phaseContext.getSource(EntityPlayer.class);
    if (playerSource.isPresent()) {
        // Post event for inventory changes
        ((IMixinInventoryPlayer) playerSource.get().inventory).setCapture(false);
        List<SlotTransaction> list = ((IMixinInventoryPlayer) playerSource.get().inventory).getCapturedTransactions();
        if (!list.isEmpty()) {
            ChangeInventoryEvent event = SpongeEventFactory.createChangeInventoryEvent(Sponge.getCauseStackManager().getCurrentCause(), ((Inventory) playerSource.get().inventory), list);
            SpongeImpl.postEvent(event);
            PacketPhaseUtil.handleSlotRestore(playerSource.get(), null, list, event.isCancelled());
            list.clear();
        }
    }
    final CommandSource sender = phaseContext.getSource(CommandSource.class).orElseThrow(TrackingUtil.throwWithContext("Expected to be capturing a Command Sender, but none found!", phaseContext));
    phaseContext.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(list -> TrackingUtil.processBlockCaptures(list, this, phaseContext));
    try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(sender);
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.PLACEMENT);
        phaseContext.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
            // TODO the entity spawn causes are not likely valid,
            // need to investigate further.
            final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
            SpongeImpl.postEvent(spawnEntityEvent);
            if (!spawnEntityEvent.isCancelled()) {
                final boolean isPlayer = sender instanceof Player;
                final Player player = isPlayer ? (Player) sender : null;
                for (Entity entity : spawnEntityEvent.getEntities()) {
                    if (isPlayer) {
                        EntityUtil.toMixin(entity).setCreator(player.getUniqueId());
                    }
                    EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
                }
            }
        });
    }
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(sender);
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.DROPPED_ITEM);
        phaseContext.getCapturedEntityDropSupplier().acceptIfNotEmpty(uuidItemStackMultimap -> {
            for (Map.Entry<UUID, Collection<ItemDropData>> entry : uuidItemStackMultimap.asMap().entrySet()) {
                final UUID key = entry.getKey();
                @Nullable net.minecraft.entity.Entity foundEntity = null;
                for (WorldServer worldServer : WorldManager.getWorlds()) {
                    final net.minecraft.entity.Entity entityFromUuid = worldServer.getEntityFromUuid(key);
                    if (entityFromUuid != null) {
                        foundEntity = entityFromUuid;
                        break;
                    }
                }
                final Optional<Entity> affectedEntity = Optional.ofNullable((Entity) foundEntity);
                if (!affectedEntity.isPresent()) {
                    continue;
                }
                final Collection<ItemDropData> itemStacks = entry.getValue();
                if (itemStacks.isEmpty()) {
                    return;
                }
                final List<ItemDropData> items = new ArrayList<>();
                items.addAll(itemStacks);
                itemStacks.clear();
                final WorldServer minecraftWorld = EntityUtil.getMinecraftWorld(affectedEntity.get());
                if (!items.isEmpty()) {
                    final List<Entity> itemEntities = items.stream().map(data -> data.create(minecraftWorld)).map(EntityUtil::fromNative).collect(Collectors.toList());
                    Sponge.getCauseStackManager().pushCause(affectedEntity.get());
                    final DropItemEvent.Destruct destruct = SpongeEventFactory.createDropItemEventDestruct(Sponge.getCauseStackManager().getCurrentCause(), itemEntities);
                    SpongeImpl.postEvent(destruct);
                    Sponge.getCauseStackManager().popCause();
                    if (!destruct.isCancelled()) {
                        final boolean isPlayer = sender instanceof Player;
                        final Player player = isPlayer ? (Player) sender : null;
                        for (Entity entity : destruct.getEntities()) {
                            if (isPlayer) {
                                EntityUtil.toMixin(entity).setCreator(player.getUniqueId());
                            }
                            EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
                        }
                    }
                }
            }
        });
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) ArrayList(java.util.ArrayList) WorldServer(net.minecraft.world.WorldServer) CauseStackManager(org.spongepowered.api.event.CauseStackManager) UUID(java.util.UUID) DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) IMixinInventoryPlayer(org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Player(org.spongepowered.api.entity.living.player.Player) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) CommandSource(org.spongepowered.api.command.CommandSource) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) IMixinInventoryPlayer(org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) Collection(java.util.Collection) ItemDropData(org.spongepowered.common.event.tracking.context.ItemDropData) Map(java.util.Map) Inventory(org.spongepowered.api.item.inventory.Inventory) Nullable(javax.annotation.Nullable)

Aggregations

SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)44 Entity (org.spongepowered.api.entity.Entity)41 ArrayList (java.util.ArrayList)30 CauseStackManager (org.spongepowered.api.event.CauseStackManager)23 User (org.spongepowered.api.entity.living.player.User)18 EntityItem (net.minecraft.entity.item.EntityItem)17 StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)15 List (java.util.List)11 DropItemEvent (org.spongepowered.api.event.item.inventory.DropItemEvent)11 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)11 Sponge (org.spongepowered.api.Sponge)10 SpongeEventFactory (org.spongepowered.api.event.SpongeEventFactory)10 SpongeImpl (org.spongepowered.common.SpongeImpl)10 EntityUtil (org.spongepowered.common.entity.EntityUtil)10 World (org.spongepowered.api.world.World)9 Collection (java.util.Collection)8 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)8 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)8 ItemStack (org.spongepowered.api.item.inventory.ItemStack)8 ItemDropData (org.spongepowered.common.event.tracking.context.ItemDropData)8