Search in sources :

Example 66 with Living

use of org.spongepowered.api.entity.living.Living in project SpongeCommon by SpongePowered.

the class BedBlockMixin method impl$onExplodeBed.

@Inject(method = "use", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/Level;removeBlock(Lnet/minecraft/core/BlockPos;Z)Z"), cancellable = true)
private void impl$onExplodeBed(final BlockState param0, final Level param1, final BlockPos param2, final Player param3, final InteractionHand param4, final BlockHitResult param5, final CallbackInfoReturnable<InteractionResult> cir) {
    final Cause currentCause = Sponge.server().causeStackManager().currentCause();
    final BlockPos bedLocation = param5.getBlockPos();
    final BlockSnapshot snapshot = ((ServerWorld) param1).createSnapshot(bedLocation.getX(), bedLocation.getY(), bedLocation.getZ());
    if (Sponge.eventManager().post(SpongeEventFactory.createSleepingEventFailed(currentCause, snapshot, (Living) param3))) {
        param3.startSleepInBed(param2).ifLeft((param1x) -> {
            if (param1x != null) {
                param3.displayClientMessage(param1x.getMessage(), true);
            }
        });
        cir.setReturnValue(InteractionResult.SUCCESS);
    }
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) Living(org.spongepowered.api.entity.living.Living) Cause(org.spongepowered.api.event.Cause) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) BlockPos(net.minecraft.core.BlockPos) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 67 with Living

use of org.spongepowered.api.entity.living.Living in project SpongeCommon by SpongePowered.

the class VillagerMixin method impl$callPreSleepingEvent.

@Inject(method = "startSleeping", at = @At("HEAD"), cancellable = true)
private void impl$callPreSleepingEvent(BlockPos param0, CallbackInfo ci) {
    final Cause currentCause = Sponge.server().causeStackManager().currentCause();
    final BlockSnapshot snapshot = ((ServerWorld) this.level).createSnapshot(param0.getX(), param0.getY(), param0.getZ());
    if (Sponge.eventManager().post(SpongeEventFactory.createSleepingEventPre(currentCause, snapshot, (Living) this))) {
        ci.cancel();
    }
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) Living(org.spongepowered.api.entity.living.Living) Cause(org.spongepowered.api.event.Cause) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 68 with Living

use of org.spongepowered.api.entity.living.Living in project SpongeCommon by SpongePowered.

the class PlayerMixin method impl$postSleepingEvent.

@Redirect(method = "tick()V", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Player;isSleeping()Z"))
private boolean impl$postSleepingEvent(final net.minecraft.world.entity.player.Player self) {
    if (self.isSleeping()) {
        if (!((LevelBridge) this.level).bridge$isFake()) {
            final CauseStackManager csm = PhaseTracker.getCauseStackManager();
            csm.pushCause(this);
            final BlockPos bedLocation = this.shadow$getSleepingPos().get();
            final BlockSnapshot snapshot = ((ServerWorld) this.level).createSnapshot(bedLocation.getX(), bedLocation.getY(), bedLocation.getZ());
            SpongeCommon.post(SpongeEventFactory.createSleepingEventTick(csm.currentCause(), snapshot, (Living) this));
            csm.popCause();
        }
        return true;
    }
    return false;
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Living(org.spongepowered.api.entity.living.Living) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) BlockPos(net.minecraft.core.BlockPos) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 69 with Living

use of org.spongepowered.api.entity.living.Living in project LanternServer by LanternPowered.

the class LanternItem method tryToPickupItems.

private void tryToPickupItems() {
    final Set<Entity> entities = getWorld().getIntersectingEntities(getBoundingBox().get().expand(2.0, 0.5, 2.0), entity -> entity != this && entity instanceof Carrier);
    if (entities.isEmpty()) {
        return;
    }
    ItemStack itemStack = get(Keys.REPRESENTED_ITEM).map(ItemStackSnapshot::createStack).orElse(null);
    if (itemStack == null) {
        remove();
        return;
    }
    // TODO: Call pre pickup event
    for (Entity entity : entities) {
        // Ignore dead entities
        if (entity instanceof LanternLiving && ((LanternLiving) entity).isDead()) {
            continue;
        }
        Inventory inventory = ((Carrier) entity).getInventory();
        if (inventory instanceof PlayerInventory) {
            inventory = ((PlayerInventory) inventory).getMain();
        }
        final PeekedOfferTransactionResult peekResult = ((IInventory) inventory).peekOffer(itemStack);
        final ItemStack rejected = peekResult.getRejectedItem().orElse(null);
        final CauseStack causeStack = CauseStack.current();
        final ChangeInventoryEvent.Pickup event;
        try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
            frame.addContext(LanternEventContextKeys.ORIGINAL_ITEM_STACK, itemStack);
            if (rejected != null) {
                frame.addContext(LanternEventContextKeys.REST_ITEM_STACK, rejected);
            }
            event = SpongeEventFactory.createChangeInventoryEventPickup(causeStack.getCurrentCause(), inventory, peekResult.getTransactions());
            event.setCancelled(!peekResult.isSuccess());
            Sponge.getEventManager().post(event);
        }
        if (event.isCancelled() && !isRemoved()) {
            // Don't continue if the entity was removed during the event
            continue;
        }
        event.getTransactions().stream().filter(Transaction::isValid).forEach(transaction -> transaction.getSlot().set(transaction.getFinal().createStack()));
        final int added;
        if (rejected != null) {
            added = itemStack.getQuantity() - rejected.getQuantity();
            itemStack = rejected;
        } else {
            added = itemStack.getQuantity();
        }
        if (added != 0 && entity instanceof Living) {
            triggerEvent(new CollectEntityEvent((Living) entity, added));
        }
        if (rejected == null || isRemoved()) {
            itemStack = null;
        }
        if (itemStack == null) {
            break;
        }
    }
    if (itemStack != null) {
        offer(Keys.REPRESENTED_ITEM, itemStack.createSnapshot());
    } else {
        remove();
    }
}
Also used : IInventory(org.lanternpowered.server.inventory.IInventory) Entity(org.spongepowered.api.entity.Entity) CauseStack(org.lanternpowered.server.event.CauseStack) Living(org.spongepowered.api.entity.living.Living) PeekedOfferTransactionResult(org.lanternpowered.server.inventory.PeekedOfferTransactionResult) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) CollectEntityEvent(org.lanternpowered.server.entity.event.CollectEntityEvent) ChangeInventoryEvent(org.spongepowered.api.event.item.inventory.ChangeInventoryEvent) Carrier(org.spongepowered.api.item.inventory.Carrier) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) IInventory(org.lanternpowered.server.inventory.IInventory) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory)

Example 70 with Living

use of org.spongepowered.api.entity.living.Living in project LanternServer by LanternPowered.

the class DoorBehavior method tryPlace.

@Override
public BehaviorResult tryPlace(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
    final Location<World> location = context.requireContext(ContextKeys.BLOCK_LOCATION);
    final Direction face = context.requireContext(ContextKeys.INTERACTION_FACE);
    // Door can only be placed by clicking in the floor
    if (face != Direction.DOWN) {
        return BehaviorResult.PASS;
    }
    final Location<World> down = location.getBlockRelative(Direction.DOWN);
    final SolidCubeProperty solidProp = down.getProperty(SolidCubeProperty.class).get();
    // The door must be placed on a solid block
    if (!solidProp.getValue()) {
        return BehaviorResult.PASS;
    }
    final Location<World> up = location.getBlockRelative(Direction.UP);
    final ReplaceableProperty replaceableProp = up.getProperty(ReplaceableProperty.class).get();
    if (!replaceableProp.getValue()) {
        return BehaviorResult.PASS;
    }
    final BlockSnapshot snapshot = context.getContext(ContextKeys.BLOCK_SNAPSHOT).orElseThrow(() -> new IllegalStateException("The BlockSnapshotRetrieveBehavior BlockSnapshot isn't present."));
    final BlockSnapshotBuilder builder = BlockSnapshotBuilder.create().from(snapshot);
    context.populateBlockSnapshot(builder, BehaviorContext.PopulationFlags.CREATOR_AND_NOTIFIER);
    Direction facing = Direction.NORTH;
    Vector3i left = Vector3i.UNIT_X;
    final Optional<Entity> optSource = context.first(Entity.class);
    if (optSource.isPresent()) {
        final Entity source = optSource.get();
        final Vector3d rotVector;
        if (source instanceof Living) {
            rotVector = ((Living) source).getHeadRotation();
        } else {
            rotVector = optSource.get().getRotation();
        }
        // Calculate the direction the entity is looking
        final Vector3d dir = Quaternions.fromAxesAnglesDeg(rotVector.mul(-1)).rotate(Vector3d.FORWARD);
        facing = Direction.getClosestHorizontal(dir, Direction.Division.CARDINAL);
        left = LEFT_ANGLE.rotate(facing.asOffset()).toInt();
        facing = facing.getOpposite();
    }
    builder.add(Keys.DIRECTION, facing);
    // TODO: Hinges
    context.addBlockChange(builder.location(location).build());
    context.addBlockChange(builder.add(LanternKeys.DOOR_HALF, LanternDoorHalf.UPPER).location(up).build());
    return BehaviorResult.SUCCESS;
}
Also used : Entity(org.spongepowered.api.entity.Entity) Living(org.spongepowered.api.entity.living.Living) SolidCubeProperty(org.spongepowered.api.data.property.block.SolidCubeProperty) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) Vector3d(com.flowpowered.math.vector.Vector3d) ReplaceableProperty(org.spongepowered.api.data.property.block.ReplaceableProperty) Vector3i(com.flowpowered.math.vector.Vector3i) BlockSnapshotBuilder(org.lanternpowered.server.block.BlockSnapshotBuilder)

Aggregations

Living (org.spongepowered.api.entity.living.Living)72 Entity (org.spongepowered.api.entity.Entity)36 Player (org.spongepowered.api.entity.living.player.Player)18 Instruction (com.skelril.openboss.Instruction)15 Listener (org.spongepowered.api.event.Listener)14 ZoneBossDetail (com.skelril.skree.content.zone.ZoneBossDetail)13 World (org.spongepowered.api.world.World)12 NamedBindInstruction (com.skelril.skree.content.zone.group.catacombs.instruction.bossmove.NamedBindInstruction)10 HealthBindInstruction (com.skelril.skree.content.zone.group.freakyfour.boss.bossmove.HealthBindInstruction)10 DamageEntityEvent (org.spongepowered.api.event.entity.DamageEntityEvent)8 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)7 EntityDamageSource (org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource)7 Vector3d (com.flowpowered.math.vector.Vector3d)6 PlayerCombatParser (com.skelril.nitro.combat.PlayerCombatParser)6 CuboidContainmentPredicate (com.skelril.nitro.position.CuboidContainmentPredicate)6 BackTeleportInstruction (com.skelril.skree.content.zone.group.freakyfour.boss.bossmove.BackTeleportInstruction)6 HealableInstruction (com.skelril.skree.content.zone.group.freakyfour.boss.bossmove.HealableInstruction)6 IndirectEntityDamageSource (org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource)6 Vector3i (com.flowpowered.math.vector.Vector3i)5 IntegratedRunnable (com.skelril.nitro.time.IntegratedRunnable)5