use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.
the class UseItemPacketState method unwind.
@Override
public void unwind(BasicPacketContext context) {
final EntityPlayerMP player = context.getPacketPlayer();
final ItemStack itemStack = context.getItemUsed();
final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(itemStack);
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(player);
Sponge.getCauseStackManager().pushCause(snapshot);
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, itemStack.getType() == ItemTypes.SPAWN_EGG ? InternalSpawnTypes.SPAWN_EGG : InternalSpawnTypes.PLACEMENT);
context.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
SpongeImpl.postEvent(spawnEntityEvent);
if (!spawnEntityEvent.isCancelled()) {
processSpawnedEntities(player, spawnEntityEvent);
}
});
}
context.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(originalBlocks -> {
boolean success = TrackingUtil.processBlockCaptures(originalBlocks, this, context);
if (!success && snapshot != ItemTypeRegistryModule.NONE_SNAPSHOT) {
Sponge.getCauseStackManager().pushCause(player);
EnumHand hand = ((CPacketPlayerTryUseItem) context.getPacket()).getHand();
PacketPhaseUtil.handlePlayerSlotRestore(player, (net.minecraft.item.ItemStack) itemStack, hand);
}
});
}
use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.
the class BlockEventTickPhaseState method spawnEntityOrCapture.
@Override
public boolean spawnEntityOrCapture(BlockEventTickContext context, Entity entity, int chunkX, int chunkZ) {
final Optional<User> notifier = context.getNotifier();
final Optional<User> owner = context.getOwner();
final User entityCreator = notifier.orElseGet(() -> owner.orElse(null));
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.CUSTOM);
final List<Entity> entities = new ArrayList<>(1);
entities.add(entity);
final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
SpongeImpl.postEvent(spawnEntityEvent);
if (!spawnEntityEvent.isCancelled()) {
for (Entity anEntity : spawnEntityEvent.getEntities()) {
if (entityCreator != null) {
EntityUtil.toMixin(anEntity).setCreator(entityCreator.getUniqueId());
}
EntityUtil.getMixinWorld(anEntity).forceSpawnEntity(anEntity);
}
return true;
}
}
return false;
}
use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.
the class BlockEventTickPhaseState method unwind.
@Override
public void unwind(BlockEventTickContext phaseContext) {
final Optional<User> notifier = phaseContext.getNotifier();
final Optional<User> owner = phaseContext.getOwner();
final User entityCreator = notifier.orElseGet(() -> owner.orElse(null));
try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.CUSTOM);
phaseContext.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(blockSnapshots -> TrackingUtil.processBlockCaptures(blockSnapshots, this, phaseContext));
phaseContext.getCapturedItemsSupplier().acceptAndClearIfNotEmpty(items -> {
final ArrayList<Entity> capturedEntities = new ArrayList<>();
for (EntityItem entity : items) {
capturedEntities.add(EntityUtil.fromNative(entity));
}
final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), capturedEntities);
SpongeImpl.postEvent(spawnEntityEvent);
for (Entity entity : spawnEntityEvent.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 BlockTickPhaseState method unwind.
@Override
public void unwind(BlockTickContext context) {
final LocatableBlock locatableBlock = context.requireSource(LocatableBlock.class);
try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(locatableBlock);
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.DROPPED_ITEM);
final User entityCreator = context.getNotifier().orElseGet(() -> context.getOwner().orElse(null));
context.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(blockSnapshots -> TrackingUtil.processBlockCaptures(blockSnapshots, this, context));
context.getCapturedItemsSupplier().acceptAndClearIfNotEmpty(items -> {
final ArrayList<Entity> capturedEntities = new ArrayList<>();
for (EntityItem entity : items) {
capturedEntities.add(EntityUtil.fromNative(entity));
}
final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), capturedEntities);
SpongeImpl.postEvent(spawnEntityEvent);
for (Entity entity : spawnEntityEvent.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 DimensionTickPhaseState method spawnEntityOrCapture.
/*
@author - gabizou
non-javadoc
This is a stopgap to get dragon respawns working. Since there's 4 classes that interweave themselves
between various states including but not withstanding: respawning endercrystals, respawning the dragon,
locating the crystals, etc. it's best to not capture the spawns and simply spawn them in directly.
This is a todo until the dragon phases are completely configured and correctly managed (should be able to at some point restore
traditional ai logic to the dragon without the necessity for the dragon being summoned the manual way).
*/
@Override
public boolean spawnEntityOrCapture(DimensionContext 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);
try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLACEMENT);
final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
SpongeImpl.postEvent(event);
if (!event.isCancelled() && event.getEntities().size() > 0) {
for (Entity item : event.getEntities()) {
EntityUtil.getMixinWorld(entity).forceSpawnEntity(item);
}
return true;
}
}
return false;
}
Aggregations