Search in sources :

Example 11 with AdvancementCriterion

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

the class AbstractOperatorCriterionProgress method grant.

@Override
Optional<Instant> grant(Runnable invalidator) {
    Instant time = null;
    final boolean[] change = new boolean[1];
    for (AdvancementCriterion criterion : this.criterion.getCriteria()) {
        final Optional<Instant> time1 = ((AbstractCriterionProgress<?>) this.progress.get(criterion).get()).grant(() -> change[0] = true);
        if (!time1.isPresent()) {
            time = Instant.MAX;
        } else if (time == null || time1.get().isAfter(time)) {
            time = time1.get();
        }
    }
    if (change[0]) {
        invalidator.run();
    }
    // Should be impossible
    checkNotNull(time);
    if (time == Instant.MAX) {
        // Somebody prevented a criterion to be granted
        return Optional.empty();
    }
    return Optional.of(time);
}
Also used : Instant(java.time.Instant) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion)

Example 12 with AdvancementCriterion

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

the class AbstractOperatorCriterionProgress method revoke.

@Override
Optional<Instant> revoke(Runnable invalidator) {
    final Optional<Instant> previousState = get();
    final boolean[] change = new boolean[1];
    for (AdvancementCriterion criterion : this.criterion.getCriteria()) {
        ((AbstractCriterionProgress<?>) this.progress.get(criterion).get()).revoke(() -> change[0] = true);
    }
    if (change[0]) {
        invalidator.run();
    }
    return previousState;
}
Also used : Instant(java.time.Instant) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion)

Example 13 with AdvancementCriterion

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

the class PlayerAdvancementsMixin method impl$updateProgressOnUnregister.

@Nullable
@Redirect(method = "unregisterListeners", at = @At(value = "INVOKE", ordinal = 0, target = "Lnet/minecraft/advancements/AdvancementProgress;getCriterion(Ljava/lang/String;)Lnet/minecraft/advancements/CriterionProgress;"))
private CriterionProgress impl$updateProgressOnUnregister(final AdvancementProgress advancementProgress, final String criterion) {
    if (((PlatformEntityBridge) this.player).bridge$isFakePlayer()) {
        return advancementProgress.getCriterion(criterion);
    }
    final org.spongepowered.api.advancement.Advancement advancement = ((org.spongepowered.api.advancement.AdvancementProgress) advancementProgress).advancement();
    final AdvancementCriterion advancementCriterion = (AdvancementCriterion) ((Advancement) advancement).getCriteria().get(criterion);
    final CriterionBridge criterionBridge = (CriterionBridge) advancementCriterion;
    // Only remove the trigger once the goal is reached
    if (criterionBridge.bridge$getScoreCriterion() != null && !((org.spongepowered.api.advancement.AdvancementProgress) advancementProgress).get(criterionBridge.bridge$getScoreCriterion()).get().achieved()) {
        return null;
    }
    return advancementProgress.getCriterion(criterion);
}
Also used : AdvancementProgress(net.minecraft.advancements.AdvancementProgress) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) PlatformEntityBridge(org.spongepowered.common.bridge.world.entity.PlatformEntityBridge) Advancement(net.minecraft.advancements.Advancement) CriterionBridge(org.spongepowered.common.bridge.advancements.CriterionBridge) Redirect(org.spongepowered.asm.mixin.injection.Redirect) Nullable(javax.annotation.Nullable)

Example 14 with AdvancementCriterion

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

the class PlayerAdvancementsMixin method impl$onUnregisterListenersGetProgress.

@Redirect(method = "registerListeners(Lnet/minecraft/advancements/Advancement;)V", at = @At(value = "INVOKE", ordinal = 0, target = "Lnet/minecraft/advancements/CriterionProgress;isDone()Z"))
private boolean impl$onUnregisterListenersGetProgress(final CriterionProgress progress) {
    final CriterionProgressBridge mixinCriterionProgress = (CriterionProgressBridge) progress;
    if (((PlatformEntityBridge) this.player).bridge$isFakePlayer() || !mixinCriterionProgress.bridge$isCriterionAvailable()) {
        return progress.isDone();
    }
    final AdvancementCriterion criterion = ((org.spongepowered.api.advancement.criteria.CriterionProgress) progress).criterion();
    final CriterionBridge mixinCriterion = (CriterionBridge) criterion;
    // Only remove the trigger once the goal is reached
    if (mixinCriterion.bridge$getScoreCriterion() != null) {
        return ((CriterionProgressBridge) progress).bridge$getAdvancementProgress().get(mixinCriterion.bridge$getScoreCriterion()).get().achieved();
    }
    return progress.isDone();
}
Also used : AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) CriterionProgressBridge(org.spongepowered.common.bridge.advancements.CriterionProgressBridge) CriterionProgress(net.minecraft.advancements.CriterionProgress) CriterionBridge(org.spongepowered.common.bridge.advancements.CriterionBridge) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 15 with AdvancementCriterion

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

the class AdvancementProgressMixin method impl$revokeCriterion.

private boolean impl$revokeCriterion(String rawCriterion) {
    final net.minecraft.advancements.CriterionProgress criterionProgress = this.criteria.get(rawCriterion);
    if (criterionProgress == null || !criterionProgress.isDone()) {
        return false;
    }
    if (SpongeScoreCriterion.BYPASS_EVENT) {
        criterionProgress.revoke();
        return true;
    }
    final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
    final ServerPlayer player = ((PlayerAdvancementsBridge) this.impl$playerAdvancements).bridge$getPlayer();
    final CriterionProgress progress = (CriterionProgress) criterionProgress;
    final AdvancementCriterion criterion = progress.criterion();
    final CriterionBridge criterionBridge = (CriterionBridge) criterion;
    // The score criterion needs special care
    final SpongeScoreCriterion scoreCriterion = criterionBridge.bridge$getScoreCriterion();
    final CriterionEvent event;
    final Advancement advancement = ((org.spongepowered.api.advancement.AdvancementProgress) this).advancement();
    if (scoreCriterion != null) {
        final SpongeScoreCriterionProgress scoreProgress = (SpongeScoreCriterionProgress) this.impl$progressMap.get(scoreCriterion.name());
        final int lastScore = scoreProgress.score();
        final int score = lastScore + 1;
        if (lastScore == scoreCriterion.goal()) {
            event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, advancement, scoreCriterion, player, lastScore, score);
        } else if (score == scoreCriterion.goal()) {
            event = SpongeEventFactory.createCriterionEventScoreGrant(cause, advancement, scoreCriterion, player, Instant.now(), lastScore, score);
        } else {
            event = SpongeEventFactory.createCriterionEventScoreChange(cause, advancement, scoreCriterion, player, lastScore, score);
        }
    } else {
        event = SpongeEventFactory.createCriterionEventRevoke(cause, advancement, criterion, player);
    }
    if (SpongeCommon.post(event)) {
        return false;
    }
    criterionProgress.revoke();
    return true;
}
Also used : AdvancementProgress(net.minecraft.advancements.AdvancementProgress) CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) PlayerAdvancementsBridge(org.spongepowered.common.bridge.server.PlayerAdvancementsBridge) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) SpongeScoreCriterion(org.spongepowered.common.advancement.criterion.SpongeScoreCriterion) SpongeScoreCriterionProgress(org.spongepowered.common.advancement.criterion.SpongeScoreCriterionProgress) Cause(org.spongepowered.api.event.Cause) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) Advancement(org.spongepowered.api.advancement.Advancement) SpongeAndCriterionProgress(org.spongepowered.common.advancement.criterion.SpongeAndCriterionProgress) SpongeOrCriterionProgress(org.spongepowered.common.advancement.criterion.SpongeOrCriterionProgress) SpongeScoreCriterionProgress(org.spongepowered.common.advancement.criterion.SpongeScoreCriterionProgress) ImplementationBackedCriterionProgress(org.spongepowered.common.advancement.criterion.ImplementationBackedCriterionProgress) CriterionProgress(org.spongepowered.api.advancement.criteria.CriterionProgress) CriterionBridge(org.spongepowered.common.bridge.advancements.CriterionBridge)

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