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);
}
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;
}
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);
}
}
}
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;
}
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);
}
}
}
}
});
}
}
Aggregations