Search in sources :

Example 11 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class CriterionTrigger_ListenerMixin method impl$callEvents.

// @formatter:on
@SuppressWarnings({ "unchecked", "rawtypes" })
@Inject(method = "run", at = @At("HEAD"), cancellable = true)
private void impl$callEvents(PlayerAdvancements playerAdvancements, CallbackInfo ci) {
    final org.spongepowered.api.advancement.Advancement advancement = (org.spongepowered.api.advancement.Advancement) this.advancement;
    AdvancementCriterion advancementCriterion = (AdvancementCriterion) this.advancement.getCriteria().get(this.criterion);
    final CriterionBridge criterionBridge = (CriterionBridge) advancementCriterion;
    if (criterionBridge.bridge$getScoreCriterion() != null) {
        advancementCriterion = criterionBridge.bridge$getScoreCriterion();
    }
    if (!PlatformHooks.INSTANCE.getGeneralHooks().onServerThread()) {
        // Some mods do advancement granting on async threads, and we can't allow for the spam to be thrown.
        return;
    }
    // Sponge filters are always handled in the trigger method
    if (!(this.trigger instanceof SpongeFilteredTrigger)) {
        final FilteredTrigger<FilteredTriggerConfiguration> filteredTrigger = (FilteredTrigger) this.trigger;
        if (filteredTrigger.type() instanceof SpongeCriterionTrigger) {
            final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
            final ServerPlayer player = ((PlayerAdvancementsBridge) playerAdvancements).bridge$getPlayer();
            final TypeToken<FilteredTriggerConfiguration> typeToken = (TypeToken) TypeToken.get(filteredTrigger.type().configurationType());
            final CriterionEvent.Trigger event = SpongeEventFactory.createCriterionEventTrigger(cause, advancement, advancementCriterion, typeToken, player, filteredTrigger, true);
            SpongeCommon.post(event);
            if (!event.result()) {
                ci.cancel();
                return;
            }
        }
    }
    PhaseTracker.getCauseStackManager().pushCause(this.trigger);
    // the score be increased by one.
    if (advancementCriterion instanceof ScoreAdvancementCriterion) {
        ((PlayerAdvancementsBridge) playerAdvancements).bridge$getPlayer().progress(advancement).get((ScoreAdvancementCriterion) advancementCriterion).get().add(1);
        ci.cancel();
        PhaseTracker.getCauseStackManager().popCause();
    }
}
Also used : ScoreAdvancementCriterion(org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion) SpongeFilteredTrigger(org.spongepowered.common.advancement.SpongeFilteredTrigger) CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) PlayerAdvancementsBridge(org.spongepowered.common.bridge.server.PlayerAdvancementsBridge) ScoreAdvancementCriterion(org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) SpongeFilteredTrigger(org.spongepowered.common.advancement.SpongeFilteredTrigger) FilteredTrigger(org.spongepowered.api.advancement.criteria.trigger.FilteredTrigger) SpongeCriterionTrigger(org.spongepowered.common.advancement.SpongeCriterionTrigger) TypeToken(io.leangen.geantyref.TypeToken) Cause(org.spongepowered.api.event.Cause) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) FilteredTriggerConfiguration(org.spongepowered.api.advancement.criteria.trigger.FilteredTriggerConfiguration) Advancement(net.minecraft.advancements.Advancement) CriterionBridge(org.spongepowered.common.bridge.advancements.CriterionBridge) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 12 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class BedBlockMixin method impl$onUseBed.

@Inject(method = "use", at = @At(value = "HEAD"), cancellable = true)
private void impl$onUseBed(final BlockState param0, final Level param1, final BlockPos param2, final Player param3, final InteractionHand param4, final BlockHitResult param5, final CallbackInfoReturnable<InteractionResult> cir) {
    if (!param1.isClientSide) {
        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.createSleepingEventPre(currentCause, snapshot, (Living) param3))) {
            cir.setReturnValue(InteractionResult.CONSUME);
        }
    }
}
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 13 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class CampfireBlockEntityMixin method impl$assembleCampfireResult.

@Surrogate
private void impl$assembleCampfireResult(final CallbackInfo ci, final int i, final ItemStack itemStack, final Container iInventory, final ItemStack itemStack1, final BlockPos blockPos) {
    final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
    final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(itemStack1);
    final CookingEvent.Finish event = SpongeEventFactory.createCookingEventFinish(cause, (Campfire) this, Collections.singletonList(snapshot), Optional.empty(), Optional.ofNullable((CookingRecipe) this.impl$cookingRecipe[i]));
    SpongeCommon.post(event);
    this.impl$cookingRecipe[i] = null;
}
Also used : CampfireCookingRecipe(net.minecraft.world.item.crafting.CampfireCookingRecipe) CookingRecipe(org.spongepowered.api.item.recipe.cooking.CookingRecipe) Cause(org.spongepowered.api.event.Cause) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) CookingEvent(org.spongepowered.api.event.block.entity.CookingEvent) Surrogate(org.spongepowered.asm.mixin.injection.Surrogate)

Example 14 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class CampfireBlockEntityMixin method impl$canCook.

// Tick up
@Inject(method = "cook", locals = LocalCapture.CAPTURE_FAILEXCEPTION, at = @At(value = "FIELD", target = "Lnet/minecraft/world/level/block/entity/CampfireBlockEntity;cookingProgress:[I", ordinal = 1))
private void impl$canCook(final CallbackInfo ci, final int i, final ItemStack itemStack) {
    final boolean isEmpty = itemStack.isEmpty();
    if (!isEmpty) {
        final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
        final ItemStackSnapshot stack = ItemStackUtil.snapshotOf(this.items.get(i));
        final CookingEvent.Tick event = SpongeEventFactory.createCookingEventTick(cause, (Campfire) this, stack, Optional.empty(), Optional.ofNullable((CookingRecipe) impl$cookingRecipe[i]));
        SpongeCommon.post(event);
        if (event.isCancelled()) {
            this.cookingProgress[i]--;
        }
    }
}
Also used : CampfireCookingRecipe(net.minecraft.world.item.crafting.CampfireCookingRecipe) CookingRecipe(org.spongepowered.api.item.recipe.cooking.CookingRecipe) Cause(org.spongepowered.api.event.Cause) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) CookingEvent(org.spongepowered.api.event.block.entity.CookingEvent) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 15 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class BrewingStandBlockEntityMixin method impl$callBrewEvents.

@Inject(method = "tick", locals = LocalCapture.CAPTURE_FAILEXCEPTION, slice = @Slice(from = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/entity/BrewingStandBlockEntity;isBrewable()Z")), at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/entity/BrewingStandBlockEntity;setChanged()V"))
private void impl$callBrewEvents(CallbackInfo ci, ItemStack fuelStack, boolean isBrewable, boolean isBrewing, ItemStack ingredientStack) {
    final Cause currentCause = Sponge.server().causeStackManager().currentCause();
    if (isBrewing) {
        if (this.brewTime == 0 && isBrewable) {
            List<ItemStackSnapshot> stacks = new ArrayList<>();
            for (int i = 0; i < 3; ++i) {
                stacks.add(ItemStackUtil.snapshotOf(this.items.get(i)));
            }
            final BrewingEvent.Finish event = SpongeEventFactory.createBrewingEventFinish(currentCause, Collections.unmodifiableList(stacks), (BrewingStand) this, ItemStackUtil.snapshotOf(ingredientStack));
            Sponge.eventManager().post(event);
        } else if (!isBrewable || this.ingredient != ingredientStack.getItem()) {
            final BrewingEvent.Interrupt event = SpongeEventFactory.createBrewingEventInterrupt(currentCause, (BrewingStand) this, ItemStackUtil.snapshotOf(ingredientStack));
            Sponge.eventManager().post(event);
        }
    } else if (isBrewable && this.fuel > 0) {
        final BrewingEvent.Start event = SpongeEventFactory.createBrewingEventStart(currentCause, (BrewingStand) this, ItemStackUtil.snapshotOf(ingredientStack));
        if (Sponge.eventManager().post(event)) {
            this.brewTime = 0;
            this.ingredient = Items.AIR;
            this.fuel++;
        }
    }
}
Also used : BrewingEvent(org.spongepowered.api.event.block.entity.BrewingEvent) Cause(org.spongepowered.api.event.Cause) BrewingStand(org.spongepowered.api.block.entity.carrier.BrewingStand) ArrayList(java.util.ArrayList) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

Cause (org.spongepowered.api.event.Cause)55 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)19 ServerWorld (org.spongepowered.api.world.server.ServerWorld)18 Inject (org.spongepowered.asm.mixin.injection.Inject)18 ArrayList (java.util.ArrayList)16 BlockPos (net.minecraft.core.BlockPos)15 CauseStackManager (org.spongepowered.api.event.CauseStackManager)15 List (java.util.List)13 Optional (java.util.Optional)12 ServerLocation (org.spongepowered.api.world.server.ServerLocation)12 Entity (net.minecraft.world.entity.Entity)11 Player (net.minecraft.world.entity.player.Player)11 Collections (java.util.Collections)10 Collection (java.util.Collection)9 Map (java.util.Map)9 LivingEntity (net.minecraft.world.entity.LivingEntity)9 EventContext (org.spongepowered.api.event.EventContext)9 HashMap (java.util.HashMap)8 BlockState (net.minecraft.world.level.block.state.BlockState)8 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)8