Search in sources :

Example 1 with FilteredTrigger

use of org.spongepowered.api.advancement.criteria.trigger.FilteredTrigger in project SpongeCommon by SpongePowered.

the class MixinICriterionTriggerListener method onGrantCriterion.

@SuppressWarnings("unchecked")
@Inject(method = "grantCriterion", at = @At("HEAD"), cancellable = true)
private void onGrantCriterion(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.criterionName);
    final IMixinCriterion mixinCriterion = (IMixinCriterion) advancementCriterion;
    if (mixinCriterion.getScoreCriterion() != null) {
        advancementCriterion = mixinCriterion.getScoreCriterion();
    }
    // Sponge filters are always handled in the trigger method
    if (!(this.criterionInstance instanceof SpongeFilteredTrigger)) {
        final FilteredTrigger filteredTrigger = (FilteredTrigger) this.criterionInstance;
        if (filteredTrigger.getType() instanceof SpongeTrigger) {
            final Cause cause = Sponge.getCauseStackManager().getCurrentCause();
            final Player player = ((IMixinPlayerAdvancements) playerAdvancements).getPlayer();
            final TypeToken typeToken = TypeToken.of(filteredTrigger.getType().getConfigurationType());
            final CriterionEvent.Trigger event = SpongeEventFactory.createCriterionEventTrigger(cause, advancement, advancementCriterion, typeToken, player, filteredTrigger, true);
            SpongeImpl.postEvent(event);
            if (!event.getResult()) {
                ci.cancel();
                return;
            }
        }
    }
    SpongeImpl.getCauseStackManager().pushCause(this.criterionInstance);
    // the score be increased by one.
    if (advancementCriterion instanceof ScoreAdvancementCriterion) {
        ((IMixinPlayerAdvancements) playerAdvancements).getPlayer().getProgress(advancement).get((ScoreAdvancementCriterion) advancementCriterion).get().add(1);
        ci.cancel();
        SpongeImpl.getCauseStackManager().popCause();
    }
}
Also used : ScoreAdvancementCriterion(org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion) SpongeFilteredTrigger(org.spongepowered.common.advancement.SpongeFilteredTrigger) Player(org.spongepowered.api.entity.living.player.Player) CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) ScoreAdvancementCriterion(org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) SpongeTrigger(org.spongepowered.common.advancement.SpongeTrigger) IMixinPlayerAdvancements(org.spongepowered.common.interfaces.advancement.IMixinPlayerAdvancements) SpongeFilteredTrigger(org.spongepowered.common.advancement.SpongeFilteredTrigger) FilteredTrigger(org.spongepowered.api.advancement.criteria.trigger.FilteredTrigger) IMixinCriterion(org.spongepowered.common.interfaces.advancement.IMixinCriterion) TypeToken(com.google.common.reflect.TypeToken) Cause(org.spongepowered.api.event.cause.Cause) Advancement(net.minecraft.advancements.Advancement) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with FilteredTrigger

use of org.spongepowered.api.advancement.criteria.trigger.FilteredTrigger in project LanternServer by LanternPowered.

the class LanternTrigger method trigger.

@SuppressWarnings("unchecked")
@Override
public void trigger(Player player) {
    final LanternPlayerAdvancements playerAdvancements = ((LanternPlayer) player).getAdvancementsProgress();
    final Collection<AbstractCriterionProgress> collection = this.progress.get(playerAdvancements);
    if (!collection.isEmpty()) {
        final Cause cause = CauseStack.current().getCurrentCause();
        for (AbstractCriterionProgress progress : new ArrayList<>(this.progress.get(playerAdvancements))) {
            final Advancement advancement = progress.getAdvancementProgress().getAdvancement();
            final AdvancementCriterion criterion = progress.getCriterion();
            final FilteredTrigger filteredTrigger = criterion.getTrigger().get();
            final CriterionEvent.Trigger event = SpongeEventFactory.createCriterionEventTrigger(cause, advancement, criterion, this.configTypeToken, player, filteredTrigger, this.eventHandler != null);
            if (this.eventHandler != null) {
                this.eventHandler.accept(event);
            }
            Sponge.getEventManager().post(event);
            if (event.getResult()) {
                if (progress instanceof ScoreCriterionProgress) {
                    ((ScoreCriterionProgress) progress).add(1);
                } else {
                    progress.grant();
                }
            }
        }
    }
}
Also used : ScoreCriterionProgress(org.spongepowered.api.advancement.criteria.ScoreCriterionProgress) CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) AbstractCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.AbstractCriterionProgress) Cause(org.spongepowered.api.event.cause.Cause) ArrayList(java.util.ArrayList) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) LanternPlayerAdvancements(org.lanternpowered.server.advancement.LanternPlayerAdvancements) FilteredTrigger(org.spongepowered.api.advancement.criteria.trigger.FilteredTrigger) Advancement(org.spongepowered.api.advancement.Advancement) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Aggregations

AdvancementCriterion (org.spongepowered.api.advancement.criteria.AdvancementCriterion)2 FilteredTrigger (org.spongepowered.api.advancement.criteria.trigger.FilteredTrigger)2 CriterionEvent (org.spongepowered.api.event.advancement.CriterionEvent)2 Cause (org.spongepowered.api.event.cause.Cause)2 TypeToken (com.google.common.reflect.TypeToken)1 ArrayList (java.util.ArrayList)1 Advancement (net.minecraft.advancements.Advancement)1 LanternPlayerAdvancements (org.lanternpowered.server.advancement.LanternPlayerAdvancements)1 AbstractCriterionProgress (org.lanternpowered.server.advancement.criteria.progress.AbstractCriterionProgress)1 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)1 Advancement (org.spongepowered.api.advancement.Advancement)1 ScoreAdvancementCriterion (org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion)1 ScoreCriterionProgress (org.spongepowered.api.advancement.criteria.ScoreCriterionProgress)1 Player (org.spongepowered.api.entity.living.player.Player)1 Inject (org.spongepowered.asm.mixin.injection.Inject)1 SpongeFilteredTrigger (org.spongepowered.common.advancement.SpongeFilteredTrigger)1 SpongeTrigger (org.spongepowered.common.advancement.SpongeTrigger)1 IMixinCriterion (org.spongepowered.common.interfaces.advancement.IMixinCriterion)1 IMixinPlayerAdvancements (org.spongepowered.common.interfaces.advancement.IMixinPlayerAdvancements)1