Search in sources :

Example 1 with SpongeScoreCriterionProgress

use of org.spongepowered.common.advancement.SpongeScoreCriterionProgress in project SpongeCommon by SpongePowered.

the class MixinAdvancementProgress method revokeCriterion.

/**
 * @author Cybermaxke
 * @reason Rewrite the method to add support for triggering
 *         score criteria and calling revoke events.
 */
@Overwrite
public boolean revokeCriterion(String criterionIn) {
    final net.minecraft.advancements.CriterionProgress criterionProgress = this.criteria.get(criterionIn);
    if (criterionProgress == null || !criterionProgress.isObtained()) {
        return false;
    }
    if (SpongeScoreCriterion.BYPASS_EVENT) {
        criterionProgress.reset();
        return true;
    }
    final Cause cause = SpongeImpl.getCauseStackManager().getCurrentCause();
    final Player player = ((IMixinPlayerAdvancements) this.playerAdvancements).getPlayer();
    final CriterionProgress progress = (CriterionProgress) criterionProgress;
    final AdvancementCriterion criterion = progress.getCriterion();
    final IMixinCriterion mixinCriterion = (IMixinCriterion) criterion;
    // The score criterion needs special care
    final SpongeScoreCriterion scoreCriterion = mixinCriterion.getScoreCriterion();
    CriterionEvent event;
    if (scoreCriterion != null) {
        final SpongeScoreCriterionProgress scoreProgress = (SpongeScoreCriterionProgress) get(scoreCriterion).get();
        final int lastScore = scoreProgress.getScore();
        final int score = lastScore + 1;
        if (lastScore == scoreCriterion.getGoal()) {
            event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, getAdvancement(), scoreCriterion, player, lastScore, score);
        } else if (score == scoreCriterion.getGoal()) {
            event = SpongeEventFactory.createCriterionEventScoreGrant(cause, getAdvancement(), scoreCriterion, player, Instant.now(), lastScore, score);
        } else {
            event = SpongeEventFactory.createCriterionEventScoreChange(cause, getAdvancement(), scoreCriterion, player, lastScore, score);
        }
    } else {
        event = SpongeEventFactory.createCriterionEventRevoke(cause, getAdvancement(), criterion, player);
    }
    if (SpongeImpl.postEvent(event)) {
        return false;
    }
    criterionProgress.reset();
    return true;
}
Also used : 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) SpongeScoreCriterion(org.spongepowered.common.advancement.SpongeScoreCriterion) IMixinPlayerAdvancements(org.spongepowered.common.interfaces.advancement.IMixinPlayerAdvancements) SpongeScoreCriterionProgress(org.spongepowered.common.advancement.SpongeScoreCriterionProgress) IMixinCriterion(org.spongepowered.common.interfaces.advancement.IMixinCriterion) Cause(org.spongepowered.api.event.cause.Cause) SpongeOrCriterionProgress(org.spongepowered.common.advancement.SpongeOrCriterionProgress) IMixinCriterionProgress(org.spongepowered.common.interfaces.advancement.IMixinCriterionProgress) SpongeScoreCriterionProgress(org.spongepowered.common.advancement.SpongeScoreCriterionProgress) ScoreCriterionProgress(org.spongepowered.api.advancement.criteria.ScoreCriterionProgress) ICriterionProgress(org.spongepowered.common.advancement.ICriterionProgress) SpongeAndCriterionProgress(org.spongepowered.common.advancement.SpongeAndCriterionProgress) CriterionProgress(org.spongepowered.api.advancement.criteria.CriterionProgress) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 2 with SpongeScoreCriterionProgress

use of org.spongepowered.common.advancement.SpongeScoreCriterionProgress in project SpongeCommon by SpongePowered.

the class MixinAdvancementProgress method grantCriterion.

/**
 * @author Cybermaxke
 * @reason Rewrite the method to add support for triggering
 *         score criteria and calling grant events.
 */
@Overwrite
public boolean grantCriterion(String criterionIn) {
    final net.minecraft.advancements.CriterionProgress criterionProgress = this.criteria.get(criterionIn);
    if (criterionProgress == null || criterionProgress.isObtained()) {
        return false;
    }
    if (SpongeScoreCriterion.BYPASS_EVENT) {
        criterionProgress.obtain();
        return true;
    }
    final Cause cause = SpongeImpl.getCauseStackManager().getCurrentCause();
    final Player player = ((IMixinPlayerAdvancements) this.playerAdvancements).getPlayer();
    final CriterionProgress progress = (CriterionProgress) criterionProgress;
    final AdvancementCriterion criterion = progress.getCriterion();
    final IMixinCriterion mixinCriterion = (IMixinCriterion) criterion;
    // The score criterion needs special care
    final SpongeScoreCriterion scoreCriterion = mixinCriterion.getScoreCriterion();
    CriterionEvent event;
    if (scoreCriterion != null) {
        final SpongeScoreCriterionProgress scoreProgress = (SpongeScoreCriterionProgress) get(scoreCriterion).get();
        final int lastScore = scoreProgress.getScore();
        final int score = lastScore + 1;
        if (lastScore == scoreCriterion.getGoal()) {
            event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, getAdvancement(), scoreCriterion, player, lastScore, score);
        } else if (score == scoreCriterion.getGoal()) {
            event = SpongeEventFactory.createCriterionEventScoreGrant(cause, getAdvancement(), scoreCriterion, player, Instant.now(), lastScore, score);
        } else {
            event = SpongeEventFactory.createCriterionEventScoreChange(cause, getAdvancement(), scoreCriterion, player, lastScore, score);
        }
    } else {
        event = SpongeEventFactory.createCriterionEventGrant(cause, getAdvancement(), criterion, player, Instant.now());
    }
    if (SpongeImpl.postEvent(event)) {
        return false;
    }
    criterionProgress.obtain();
    return true;
}
Also used : 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) SpongeScoreCriterion(org.spongepowered.common.advancement.SpongeScoreCriterion) IMixinPlayerAdvancements(org.spongepowered.common.interfaces.advancement.IMixinPlayerAdvancements) SpongeScoreCriterionProgress(org.spongepowered.common.advancement.SpongeScoreCriterionProgress) IMixinCriterion(org.spongepowered.common.interfaces.advancement.IMixinCriterion) Cause(org.spongepowered.api.event.cause.Cause) SpongeOrCriterionProgress(org.spongepowered.common.advancement.SpongeOrCriterionProgress) IMixinCriterionProgress(org.spongepowered.common.interfaces.advancement.IMixinCriterionProgress) SpongeScoreCriterionProgress(org.spongepowered.common.advancement.SpongeScoreCriterionProgress) ScoreCriterionProgress(org.spongepowered.api.advancement.criteria.ScoreCriterionProgress) ICriterionProgress(org.spongepowered.common.advancement.ICriterionProgress) SpongeAndCriterionProgress(org.spongepowered.common.advancement.SpongeAndCriterionProgress) CriterionProgress(org.spongepowered.api.advancement.criteria.CriterionProgress) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 3 with SpongeScoreCriterionProgress

use of org.spongepowered.common.advancement.SpongeScoreCriterionProgress in project SpongeCommon by SpongePowered.

the class MixinAdvancementProgress method processProgressMap.

private void processProgressMap(AdvancementCriterion criterion, Map<AdvancementCriterion, ICriterionProgress> progressMap) {
    if (criterion instanceof OperatorCriterion) {
        ((OperatorCriterion) criterion).getCriteria().forEach(child -> processProgressMap(child, progressMap));
        if (criterion instanceof AndCriterion) {
            progressMap.put(criterion, new SpongeAndCriterionProgress(this, (SpongeAndCriterion) criterion));
        } else if (criterion instanceof OrCriterion) {
            progressMap.put(criterion, new SpongeOrCriterionProgress(this, (SpongeOrCriterion) criterion));
        }
    } else if (criterion instanceof SpongeScoreCriterion) {
        final SpongeScoreCriterion scoreCriterion = (SpongeScoreCriterion) criterion;
        for (AdvancementCriterion internalCriterion : scoreCriterion.internalCriteria) {
            final IMixinCriterionProgress progress = (IMixinCriterionProgress) this.criteria.get(internalCriterion.getName());
            progress.setCriterion(internalCriterion);
            progressMap.put(internalCriterion, (ICriterionProgress) progress);
        }
        progressMap.put(scoreCriterion, new SpongeScoreCriterionProgress(this, scoreCriterion));
    } else if (criterion != SpongeEmptyCriterion.INSTANCE) {
        final IMixinCriterionProgress progress = (IMixinCriterionProgress) this.criteria.get(criterion.getName());
        progress.setCriterion(criterion);
        progressMap.put(criterion, (ICriterionProgress) progress);
    }
}
Also used : SpongeAndCriterion(org.spongepowered.common.advancement.SpongeAndCriterion) OrCriterion(org.spongepowered.api.advancement.criteria.OrCriterion) SpongeOrCriterion(org.spongepowered.common.advancement.SpongeOrCriterion) SpongeAndCriterionProgress(org.spongepowered.common.advancement.SpongeAndCriterionProgress) ScoreAdvancementCriterion(org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) OperatorCriterion(org.spongepowered.api.advancement.criteria.OperatorCriterion) SpongeScoreCriterion(org.spongepowered.common.advancement.SpongeScoreCriterion) SpongeAndCriterion(org.spongepowered.common.advancement.SpongeAndCriterion) AndCriterion(org.spongepowered.api.advancement.criteria.AndCriterion) SpongeOrCriterionProgress(org.spongepowered.common.advancement.SpongeOrCriterionProgress) SpongeScoreCriterionProgress(org.spongepowered.common.advancement.SpongeScoreCriterionProgress) IMixinCriterionProgress(org.spongepowered.common.interfaces.advancement.IMixinCriterionProgress) ICriterionProgress(org.spongepowered.common.advancement.ICriterionProgress)

Aggregations

AdvancementCriterion (org.spongepowered.api.advancement.criteria.AdvancementCriterion)3 ScoreAdvancementCriterion (org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion)3 ICriterionProgress (org.spongepowered.common.advancement.ICriterionProgress)3 SpongeAndCriterionProgress (org.spongepowered.common.advancement.SpongeAndCriterionProgress)3 SpongeOrCriterionProgress (org.spongepowered.common.advancement.SpongeOrCriterionProgress)3 SpongeScoreCriterion (org.spongepowered.common.advancement.SpongeScoreCriterion)3 SpongeScoreCriterionProgress (org.spongepowered.common.advancement.SpongeScoreCriterionProgress)3 IMixinCriterionProgress (org.spongepowered.common.interfaces.advancement.IMixinCriterionProgress)3 CriterionProgress (org.spongepowered.api.advancement.criteria.CriterionProgress)2 ScoreCriterionProgress (org.spongepowered.api.advancement.criteria.ScoreCriterionProgress)2 Player (org.spongepowered.api.entity.living.player.Player)2 CriterionEvent (org.spongepowered.api.event.advancement.CriterionEvent)2 Cause (org.spongepowered.api.event.cause.Cause)2 Overwrite (org.spongepowered.asm.mixin.Overwrite)2 IMixinCriterion (org.spongepowered.common.interfaces.advancement.IMixinCriterion)2 IMixinPlayerAdvancements (org.spongepowered.common.interfaces.advancement.IMixinPlayerAdvancements)2 AndCriterion (org.spongepowered.api.advancement.criteria.AndCriterion)1 OperatorCriterion (org.spongepowered.api.advancement.criteria.OperatorCriterion)1 OrCriterion (org.spongepowered.api.advancement.criteria.OrCriterion)1 SpongeAndCriterion (org.spongepowered.common.advancement.SpongeAndCriterion)1