Search in sources :

Example 1 with IMixinPlayerAdvancements

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

the class SpongeScoreCriterionProgress method set.

@Override
public Optional<Instant> set(int score) {
    checkState(score >= 0 && score <= getGoal(), "Score cannot be negative or greater than the goal.");
    int lastScore = getScore();
    if (lastScore == score) {
        return get();
    }
    final CriterionEvent.Score.Change event;
    final Cause cause = SpongeImpl.getCauseStackManager().getCurrentCause();
    final Advancement advancement = this.progress.getAdvancement();
    final Player player = ((IMixinPlayerAdvancements) ((IMixinAdvancementProgress) this.progress).getPlayerAdvancements()).getPlayer();
    if (lastScore == getGoal()) {
        event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, advancement, getCriterion(), player, lastScore, score);
    } else if (score == getGoal()) {
        event = SpongeEventFactory.createCriterionEventScoreGrant(cause, advancement, getCriterion(), player, Instant.now(), lastScore, score);
    } else {
        event = SpongeEventFactory.createCriterionEventScoreChange(cause, advancement, getCriterion(), player, lastScore, score);
    }
    if (SpongeImpl.postEvent(event)) {
        return get();
    }
    SpongeScoreCriterion.BYPASS_EVENT = true;
    // This is the only case a instant will be returned
    if (score == getGoal()) {
        Instant instant = null;
        for (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 (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 : Player(org.spongepowered.api.entity.living.player.Player) Instant(java.time.Instant) ScoreAdvancementCriterion(org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) IMixinPlayerAdvancements(org.spongepowered.common.interfaces.advancement.IMixinPlayerAdvancements) Cause(org.spongepowered.api.event.cause.Cause) Advancement(org.spongepowered.api.advancement.Advancement)

Example 2 with IMixinPlayerAdvancements

use of org.spongepowered.common.interfaces.advancement.IMixinPlayerAdvancements 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 3 with IMixinPlayerAdvancements

use of org.spongepowered.common.interfaces.advancement.IMixinPlayerAdvancements 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 4 with IMixinPlayerAdvancements

use of org.spongepowered.common.interfaces.advancement.IMixinPlayerAdvancements 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)

Aggregations

AdvancementCriterion (org.spongepowered.api.advancement.criteria.AdvancementCriterion)4 ScoreAdvancementCriterion (org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion)4 Player (org.spongepowered.api.entity.living.player.Player)4 Cause (org.spongepowered.api.event.cause.Cause)4 IMixinPlayerAdvancements (org.spongepowered.common.interfaces.advancement.IMixinPlayerAdvancements)4 CriterionEvent (org.spongepowered.api.event.advancement.CriterionEvent)3 IMixinCriterion (org.spongepowered.common.interfaces.advancement.IMixinCriterion)3 CriterionProgress (org.spongepowered.api.advancement.criteria.CriterionProgress)2 ScoreCriterionProgress (org.spongepowered.api.advancement.criteria.ScoreCriterionProgress)2 Overwrite (org.spongepowered.asm.mixin.Overwrite)2 ICriterionProgress (org.spongepowered.common.advancement.ICriterionProgress)2 SpongeAndCriterionProgress (org.spongepowered.common.advancement.SpongeAndCriterionProgress)2 SpongeOrCriterionProgress (org.spongepowered.common.advancement.SpongeOrCriterionProgress)2 SpongeScoreCriterion (org.spongepowered.common.advancement.SpongeScoreCriterion)2 SpongeScoreCriterionProgress (org.spongepowered.common.advancement.SpongeScoreCriterionProgress)2 IMixinCriterionProgress (org.spongepowered.common.interfaces.advancement.IMixinCriterionProgress)2 TypeToken (com.google.common.reflect.TypeToken)1 Instant (java.time.Instant)1 Advancement (net.minecraft.advancements.Advancement)1 Advancement (org.spongepowered.api.advancement.Advancement)1