Search in sources :

Example 1 with PlayerAdvancementsBridge

use of org.spongepowered.common.bridge.server.PlayerAdvancementsBridge 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)

Example 2 with PlayerAdvancementsBridge

use of org.spongepowered.common.bridge.server.PlayerAdvancementsBridge in project SpongeCommon by SpongePowered.

the class CriterionTrigger_ListenerMixin method impl$callEvents.

// @formatter:on
@SuppressWarnings({ "unchecked", "rawtypes" })
@Inject(method = "run", at = @At("HEAD"), cancellable = true)
private void impl$callEvents(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.criterion);
    final CriterionBridge criterionBridge = (CriterionBridge) advancementCriterion;
    if (criterionBridge.bridge$getScoreCriterion() != null) {
        advancementCriterion = criterionBridge.bridge$getScoreCriterion();
    }
    if (!PlatformHooks.INSTANCE.getGeneralHooks().onServerThread()) {
        // Some mods do advancement granting on async threads, and we can't allow for the spam to be thrown.
        return;
    }
    // Sponge filters are always handled in the trigger method
    if (!(this.trigger instanceof SpongeFilteredTrigger)) {
        final FilteredTrigger<FilteredTriggerConfiguration> filteredTrigger = (FilteredTrigger) this.trigger;
        if (filteredTrigger.type() instanceof SpongeCriterionTrigger) {
            final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
            final ServerPlayer player = ((PlayerAdvancementsBridge) playerAdvancements).bridge$getPlayer();
            final TypeToken<FilteredTriggerConfiguration> typeToken = (TypeToken) TypeToken.get(filteredTrigger.type().configurationType());
            final CriterionEvent.Trigger event = SpongeEventFactory.createCriterionEventTrigger(cause, advancement, advancementCriterion, typeToken, player, filteredTrigger, true);
            SpongeCommon.post(event);
            if (!event.result()) {
                ci.cancel();
                return;
            }
        }
    }
    PhaseTracker.getCauseStackManager().pushCause(this.trigger);
    // the score be increased by one.
    if (advancementCriterion instanceof ScoreAdvancementCriterion) {
        ((PlayerAdvancementsBridge) playerAdvancements).bridge$getPlayer().progress(advancement).get((ScoreAdvancementCriterion) advancementCriterion).get().add(1);
        ci.cancel();
        PhaseTracker.getCauseStackManager().popCause();
    }
}
Also used : ScoreAdvancementCriterion(org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion) SpongeFilteredTrigger(org.spongepowered.common.advancement.SpongeFilteredTrigger) CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) PlayerAdvancementsBridge(org.spongepowered.common.bridge.server.PlayerAdvancementsBridge) ScoreAdvancementCriterion(org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) SpongeFilteredTrigger(org.spongepowered.common.advancement.SpongeFilteredTrigger) FilteredTrigger(org.spongepowered.api.advancement.criteria.trigger.FilteredTrigger) SpongeCriterionTrigger(org.spongepowered.common.advancement.SpongeCriterionTrigger) TypeToken(io.leangen.geantyref.TypeToken) Cause(org.spongepowered.api.event.Cause) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) FilteredTriggerConfiguration(org.spongepowered.api.advancement.criteria.trigger.FilteredTriggerConfiguration) Advancement(net.minecraft.advancements.Advancement) CriterionBridge(org.spongepowered.common.bridge.advancements.CriterionBridge) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 3 with PlayerAdvancementsBridge

use of org.spongepowered.common.bridge.server.PlayerAdvancementsBridge 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 4 with PlayerAdvancementsBridge

use of org.spongepowered.common.bridge.server.PlayerAdvancementsBridge in project SpongeCommon by SpongePowered.

the class AdvancementProgressMixin method impl$grantCriterion.

private boolean impl$grantCriterion(String rawCriterion) {
    final net.minecraft.advancements.CriterionProgress criterionProgress = this.criteria.get(rawCriterion);
    if (criterionProgress == null || criterionProgress.isDone()) {
        return false;
    }
    if (SpongeScoreCriterion.BYPASS_EVENT) {
        criterionProgress.grant();
        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;
    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, this.bridge$getAdvancement(), scoreCriterion, player, lastScore, score);
        } else if (score == scoreCriterion.goal()) {
            event = SpongeEventFactory.createCriterionEventScoreGrant(cause, this.bridge$getAdvancement(), scoreCriterion, player, Instant.now(), lastScore, score);
        } else {
            event = SpongeEventFactory.createCriterionEventScoreChange(cause, this.bridge$getAdvancement(), scoreCriterion, player, lastScore, score);
        }
    } else {
        event = SpongeEventFactory.createCriterionEventGrant(cause, this.bridge$getAdvancement(), criterion, player, Instant.now());
    }
    if (SpongeCommon.post(event)) {
        return false;
    }
    criterionProgress.grant();
    return true;
}
Also used : 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) 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)4 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)4 Cause (org.spongepowered.api.event.Cause)4 PlayerAdvancementsBridge (org.spongepowered.common.bridge.server.PlayerAdvancementsBridge)4 CriterionEvent (org.spongepowered.api.event.advancement.CriterionEvent)3 CriterionBridge (org.spongepowered.common.bridge.advancements.CriterionBridge)3 Advancement (org.spongepowered.api.advancement.Advancement)2 CriterionProgress (org.spongepowered.api.advancement.criteria.CriterionProgress)2 ScoreAdvancementCriterion (org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion)2 ImplementationBackedCriterionProgress (org.spongepowered.common.advancement.criterion.ImplementationBackedCriterionProgress)2 SpongeAndCriterionProgress (org.spongepowered.common.advancement.criterion.SpongeAndCriterionProgress)2 SpongeOrCriterionProgress (org.spongepowered.common.advancement.criterion.SpongeOrCriterionProgress)2 SpongeScoreCriterion (org.spongepowered.common.advancement.criterion.SpongeScoreCriterion)2 SpongeScoreCriterionProgress (org.spongepowered.common.advancement.criterion.SpongeScoreCriterionProgress)2 TypeToken (io.leangen.geantyref.TypeToken)1 Instant (java.time.Instant)1 Advancement (net.minecraft.advancements.Advancement)1 AdvancementProgress (net.minecraft.advancements.AdvancementProgress)1 FilteredTrigger (org.spongepowered.api.advancement.criteria.trigger.FilteredTrigger)1 FilteredTriggerConfiguration (org.spongepowered.api.advancement.criteria.trigger.FilteredTriggerConfiguration)1