Search in sources :

Example 41 with ServerWorld

use of org.spongepowered.api.world.server.ServerWorld 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 42 with ServerWorld

use of org.spongepowered.api.world.server.ServerWorld 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 43 with ServerWorld

use of org.spongepowered.api.world.server.ServerWorld in project SpongeCommon by SpongePowered.

the class FireworkRocketItemMixin method impl$throwConstructPreEvent.

/**
 * Private method for bridging the duplicate between
 * {@link #spongeImpl$ThrowPreBeforeSpawning(World, PlayerEntity, Hand, CallbackInfoReturnable, ItemStack)} and
 * {@link #spongeImpl$ThrowPrimeEventsIfCancelled(ItemUseContext, CallbackInfoReturnable)}
 * since both follow the same logic, but differ in how they are called.
 *
 * @param world The world
 * @param player The player
 * @param usedItem The used item
 * @return True if the event is cancelled and the callback needs to be cancelled
 */
private boolean impl$throwConstructPreEvent(final Level world, final net.minecraft.world.entity.player.Player player, final ItemStack usedItem) {
    if (ShouldFire.CONSTRUCT_ENTITY_EVENT_PRE && !((LevelBridge) world).bridge$isFake()) {
        try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
            frame.addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(usedItem));
            frame.addContext(EventContextKeys.PROJECTILE_SOURCE, (ProjectileSource) player);
            frame.pushCause(player);
            final ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(frame.currentCause(), ServerLocation.of((ServerWorld) world, player.getX(), player.getY(), player.getZ()), new Vector3d(0, 0, 0), EntityTypes.FIREWORK_ROCKET.get());
            return SpongeCommon.post(event);
        }
    }
    return false;
}
Also used : ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) ServerWorld(org.spongepowered.api.world.server.ServerWorld) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) Vector3d(org.spongepowered.math.vector.Vector3d) CauseStackManager(org.spongepowered.api.event.CauseStackManager)

Aggregations

ServerWorld (org.spongepowered.api.world.server.ServerWorld)43 Vector3d (org.spongepowered.math.vector.Vector3d)13 BlockPos (net.minecraft.core.BlockPos)12 Cause (org.spongepowered.api.event.Cause)12 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)11 CauseStackManager (org.spongepowered.api.event.CauseStackManager)11 ServerLocation (org.spongepowered.api.world.server.ServerLocation)11 Inject (org.spongepowered.asm.mixin.injection.Inject)10 ArrayList (java.util.ArrayList)9 NonNull (org.checkerframework.checker.nullness.qual.NonNull)9 ResourceKey (org.spongepowered.api.ResourceKey)8 List (java.util.List)7 Optional (java.util.Optional)7 ServerLevel (net.minecraft.server.level.ServerLevel)7 Entity (net.minecraft.world.entity.Entity)7 Living (org.spongepowered.api.entity.living.Living)7 SpongeCommon (org.spongepowered.common.SpongeCommon)7 Component (net.kyori.adventure.text.Component)6 LivingEntity (net.minecraft.world.entity.LivingEntity)6 ItemStack (net.minecraft.world.item.ItemStack)6