Search in sources :

Example 21 with AdvancementCriterion

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

the class SpongeTrigger method trigger.

@Override
public void trigger(Player player) {
    final PlayerAdvancements playerAdvancements = ((EntityPlayerMP) player).getAdvancements();
    final Cause cause = Sponge.getCauseStackManager().getCurrentCause();
    final TypeToken typeToken = TypeToken.of(this.triggerConfigurationClass);
    for (Listener listener : new ArrayList<>(this.listeners.get(playerAdvancements))) {
        final IMixinICriterionTriggerListener mixinListener = (IMixinICriterionTriggerListener) listener;
        final Advancement advancement = (Advancement) mixinListener.getAdvancement();
        final AdvancementCriterion advancementCriterion = (AdvancementCriterion) ((net.minecraft.advancements.Advancement) advancement).getCriteria().get(mixinListener.getCriterionName());
        final CriterionEvent.Trigger event = SpongeEventFactory.createCriterionEventTrigger(cause, advancement, advancementCriterion, typeToken, player, (FilteredTrigger) listener.getCriterionInstance(), this.eventHandler == null);
        if (this.eventHandler != null) {
            this.eventHandler.accept(event);
            if (!event.getResult()) {
                continue;
            }
        }
        SpongeImpl.postEvent(event);
        if (event.getResult()) {
            listener.grantCriterion(playerAdvancements);
        }
    }
}
Also used : IMixinICriterionTriggerListener(org.spongepowered.common.interfaces.advancement.IMixinICriterionTriggerListener) PlayerAdvancements(net.minecraft.advancements.PlayerAdvancements) CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) TypeToken(com.google.common.reflect.TypeToken) Cause(org.spongepowered.api.event.cause.Cause) IMixinICriterionTriggerListener(org.spongepowered.common.interfaces.advancement.IMixinICriterionTriggerListener) ArrayList(java.util.ArrayList) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) Advancement(org.spongepowered.api.advancement.Advancement)

Example 22 with AdvancementCriterion

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

the class SpongeScoreCriterionProgress method set.

@Override
public Optional<Instant> set(final int score) {
    checkState(score >= 0 && score <= this.goal(), "Score cannot be negative or greater than the goal.");
    int lastScore = this.score();
    if (lastScore == score) {
        return this.get();
    }
    final CriterionEvent.Score.Change event;
    final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
    final Advancement advancement = this.progress.advancement();
    final ServerPlayer player = ((PlayerAdvancementsBridge) ((AdvancementProgressBridge) this.progress).bridge$getPlayerAdvancements()).bridge$getPlayer();
    if (lastScore == this.goal()) {
        event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, advancement, this.criterion(), player, lastScore, score);
    } else if (score == this.goal()) {
        event = SpongeEventFactory.createCriterionEventScoreGrant(cause, advancement, this.criterion(), player, Instant.now(), lastScore, score);
    } else {
        event = SpongeEventFactory.createCriterionEventScoreChange(cause, advancement, this.criterion(), player, lastScore, score);
    }
    if (SpongeCommon.post(event)) {
        return this.get();
    }
    SpongeScoreCriterion.BYPASS_EVENT = true;
    // This is the only case a instant will be returned
    if (score == this.goal()) {
        Instant instant = null;
        for (final AdvancementCriterion criterion : this.criterion.internalCriteria) {
            final org.spongepowered.api.advancement.criteria.CriterionProgress progress = this.progress.get(criterion).get();
            if (!progress.achieved()) {
                instant = progress.grant();
            }
        }
        this.score = score;
        return Optional.of(instant == null ? Instant.now() : instant);
    }
    for (final AdvancementCriterion criterion : this.criterion.internalCriteria) {
        final org.spongepowered.api.advancement.criteria.CriterionProgress progress = this.progress.get(criterion).get();
        // We don't have enough score, grant more criteria
        if (lastScore < score && !progress.achieved()) {
            progress.grant();
            lastScore++;
        // We have too much score, revoke more criteria
        } else if (lastScore > score && progress.achieved()) {
            progress.revoke();
            lastScore--;
        }
        // We reached the target score
        if (lastScore == score) {
            break;
        }
    }
    this.score = score;
    SpongeScoreCriterion.BYPASS_EVENT = false;
    return Optional.empty();
}
Also used : PlayerAdvancementsBridge(org.spongepowered.common.bridge.server.PlayerAdvancementsBridge) Instant(java.time.Instant) ScoreAdvancementCriterion(org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) Cause(org.spongepowered.api.event.Cause) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) Advancement(org.spongepowered.api.advancement.Advancement)

Example 23 with AdvancementCriterion

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

the class SpongeCriterionTrigger method bridge$trigger.

@Override
public void bridge$trigger(final ServerPlayer player) {
    final PlayerAdvancements playerAdvancements = ((net.minecraft.server.level.ServerPlayer) player).getAdvancements();
    final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
    // correct type verified in builder
    @SuppressWarnings("unchecked") final TypeToken<FilteredTriggerConfiguration> typeToken = (TypeToken<FilteredTriggerConfiguration>) TypeToken.get(this.triggerConfigurationType);
    for (final Listener listener : new ArrayList<>(this.listeners.get(playerAdvancements))) {
        final CriterionTrigger_ListenerAccessor mixinListener = (CriterionTrigger_ListenerAccessor) listener;
        final Advancement advancement = (Advancement) mixinListener.accessor$advancement();
        final AdvancementCriterion advancementCriterion = (AdvancementCriterion) ((net.minecraft.advancements.Advancement) advancement).getCriteria().get(mixinListener.accessor$criterion());
        final CriterionEvent.Trigger event = SpongeEventFactory.createCriterionEventTrigger(cause, advancement, advancementCriterion, typeToken, player, (FilteredTrigger) listener.getTriggerInstance(), this.eventHandler == null);
        if (this.eventHandler != null) {
            this.eventHandler.accept(event);
            if (!event.result()) {
                continue;
            }
        }
        SpongeCommon.post(event);
        if (event.result()) {
            listener.run(playerAdvancements);
        }
    }
}
Also used : CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) ArrayList(java.util.ArrayList) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) PlayerAdvancements(net.minecraft.server.PlayerAdvancements) TypeToken(io.leangen.geantyref.TypeToken) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) Cause(org.spongepowered.api.event.Cause) FilteredTriggerConfiguration(org.spongepowered.api.advancement.criteria.trigger.FilteredTriggerConfiguration) CriterionTrigger_ListenerAccessor(org.spongepowered.common.accessor.advancements.CriterionTrigger_ListenerAccessor) Advancement(org.spongepowered.api.advancement.Advancement)

Example 24 with AdvancementCriterion

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

the class SpongeCriterionBuilder method build0.

@SuppressWarnings("ConstantConditions")
@Override
AdvancementCriterion build0() {
    CriterionTriggerInstance trigger = (CriterionTriggerInstance) this.trigger;
    if (this.trigger == null) {
        trigger = SpongeDummyTrigger.Instance.dummy();
    }
    final Criterion criterion = new Criterion(trigger);
    ((CriterionBridge) criterion).bridge$setName(this.name);
    return (AdvancementCriterion) criterion;
}
Also used : CriterionTriggerInstance(net.minecraft.advancements.CriterionTriggerInstance) Criterion(net.minecraft.advancements.Criterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) CriterionBridge(org.spongepowered.common.bridge.advancements.CriterionBridge)

Example 25 with AdvancementCriterion

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

the class SpongeOperatorCriterionProgress method grant.

@Override
public Instant grant() {
    Instant time = null;
    for (final AdvancementCriterion criterion : this.criterion.criteria()) {
        final Instant time1 = this.progress.get(criterion).get().grant();
        if (time == null || time1.isAfter(time)) {
            time = time1;
        }
    }
    // Should be impossible
    checkNotNull(time);
    return time;
}
Also used : Instant(java.time.Instant) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion)

Aggregations

AdvancementCriterion (org.spongepowered.api.advancement.criteria.AdvancementCriterion)31 ArrayList (java.util.ArrayList)10 CriterionEvent (org.spongepowered.api.event.advancement.CriterionEvent)9 ScoreAdvancementCriterion (org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion)8 Advancement (org.spongepowered.api.advancement.Advancement)7 CriterionBridge (org.spongepowered.common.bridge.advancements.CriterionBridge)7 IMixinCriterion (org.spongepowered.common.interfaces.advancement.IMixinCriterion)7 Instant (java.time.Instant)6 Advancement (net.minecraft.advancements.Advancement)6 Criterion (net.minecraft.advancements.Criterion)5 AndCriterion (org.spongepowered.api.advancement.criteria.AndCriterion)5 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)5 Cause (org.spongepowered.api.event.Cause)5 Cause (org.spongepowered.api.event.cause.Cause)5 SpongeScoreCriterion (org.spongepowered.common.advancement.criterion.SpongeScoreCriterion)5 AdvancementProgress (net.minecraft.advancements.AdvancementProgress)4 OrCriterion (org.spongepowered.api.advancement.criteria.OrCriterion)4 Player (org.spongepowered.api.entity.living.player.Player)4 Inject (org.spongepowered.asm.mixin.injection.Inject)4 PlayerAdvancementsBridge (org.spongepowered.common.bridge.server.PlayerAdvancementsBridge)4