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