Search in sources :

Example 1 with SpongeScoreCriterion

use of org.spongepowered.common.advancement.criterion.SpongeScoreCriterion 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 SpongeScoreCriterion

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

the class AdvancementProgressMixin method impl$processProgressMap.

private void impl$processProgressMap(AdvancementCriterion criterion, Map<String, ImplementationBackedCriterionProgress> progressMap) {
    if (criterion instanceof OperatorCriterion) {
        ((OperatorCriterion) criterion).criteria().forEach(child -> this.impl$processProgressMap(child, progressMap));
        if (criterion instanceof AndCriterion) {
            progressMap.put(criterion.name(), new SpongeAndCriterionProgress((org.spongepowered.api.advancement.AdvancementProgress) this, (SpongeAndCriterion) criterion));
        } else if (criterion instanceof OrCriterion) {
            progressMap.put(criterion.name(), new SpongeOrCriterionProgress((org.spongepowered.api.advancement.AdvancementProgress) this, (SpongeOrCriterion) criterion));
        }
    } else if (criterion instanceof SpongeScoreCriterion) {
        final SpongeScoreCriterion scoreCriterion = (SpongeScoreCriterion) criterion;
        for (final AdvancementCriterion internalCriterion : scoreCriterion.internalCriteria) {
            final CriterionProgressBridge progress = (CriterionProgressBridge) this.criteria.get(internalCriterion.name());
            progress.bridge$setCriterion(internalCriterion);
            progress.bridge$setAdvancementProgress((org.spongepowered.api.advancement.AdvancementProgress) this);
            progressMap.put(internalCriterion.name(), (ImplementationBackedCriterionProgress) progress);
        }
        progressMap.put(scoreCriterion.name(), new SpongeScoreCriterionProgress((org.spongepowered.api.advancement.AdvancementProgress) this, scoreCriterion));
    } else if (!(criterion instanceof SpongeEmptyCriterion)) {
        final CriterionProgressBridge progress = (CriterionProgressBridge) this.criteria.get(criterion.name());
        progress.bridge$setCriterion(criterion);
        progress.bridge$setAdvancementProgress((org.spongepowered.api.advancement.AdvancementProgress) this);
        progressMap.put(criterion.name(), (ImplementationBackedCriterionProgress) progress);
    }
}
Also used : AdvancementProgress(net.minecraft.advancements.AdvancementProgress) SpongeOrCriterion(org.spongepowered.common.advancement.criterion.SpongeOrCriterion) OrCriterion(org.spongepowered.api.advancement.criteria.OrCriterion) SpongeAndCriterionProgress(org.spongepowered.common.advancement.criterion.SpongeAndCriterionProgress) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) SpongeScoreCriterion(org.spongepowered.common.advancement.criterion.SpongeScoreCriterion) ImplementationBackedCriterionProgress(org.spongepowered.common.advancement.criterion.ImplementationBackedCriterionProgress) AndCriterion(org.spongepowered.api.advancement.criteria.AndCriterion) SpongeAndCriterion(org.spongepowered.common.advancement.criterion.SpongeAndCriterion) SpongeScoreCriterionProgress(org.spongepowered.common.advancement.criterion.SpongeScoreCriterionProgress) SpongeAndCriterion(org.spongepowered.common.advancement.criterion.SpongeAndCriterion) SpongeEmptyCriterion(org.spongepowered.common.advancement.criterion.SpongeEmptyCriterion) CriterionProgressBridge(org.spongepowered.common.bridge.advancements.CriterionProgressBridge) OperatorCriterion(org.spongepowered.api.advancement.criteria.OperatorCriterion) SpongeOrCriterionProgress(org.spongepowered.common.advancement.criterion.SpongeOrCriterionProgress)

Example 3 with SpongeScoreCriterion

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

the class SpongeCriterionUtil method collectCriteria.

private static List<List<String>> collectCriteria(final AdvancementCriterion criterion, final Map<String, Criterion> criteria) {
    List<List<String>> requirements = new ArrayList<>();
    if (criterion instanceof SpongeAndCriterion) {
        ((SpongeOperatorCriterion) criterion).criteria().forEach(c -> requirements.addAll(SpongeCriterionUtil.collectCriteria(c, criteria)));
    } else if (criterion instanceof SpongeOrCriterion) {
        // OR List of AND Criteria of OR Criteria
        final List<List<List<String>>> andRequirementsList = ((SpongeOperatorCriterion) criterion).criteria().stream().map(c -> SpongeCriterionUtil.collectCriteria(c, criteria)).collect(Collectors.toList());
        List<List<String>> finalList = new ArrayList<>();
        // For every AND Criteria
        for (List<List<String>> andRequirements : andRequirementsList) {
            if (finalList.isEmpty()) {
                // Just take the first one in as is
                finalList.addAll(andRequirements);
            } else {
                List<List<String>> workingList = new ArrayList<>();
                for (List<String> andRequirement : andRequirements) {
                    for (List<String> prevAndRequirement : finalList) {
                        // For every AND requirement and FOR every previous AND requirement
                        // combine their OR requirements
                        // !! this can get very big very quickly !!
                        // TODO limit requirement count?
                        final List<String> newAndRequirement = new ArrayList<>(prevAndRequirement);
                        newAndRequirement.addAll(andRequirement);
                        workingList.add(newAndRequirement);
                    }
                }
                finalList = workingList;
            }
        }
        requirements.addAll(finalList);
    } else if (criterion instanceof SpongeScoreCriterion) {
        final SpongeScoreCriterion scoreCriterion = (SpongeScoreCriterion) criterion;
        for (int i = 0; i < scoreCriterion.goal(); i++) {
            final DefaultedAdvancementCriterion internalCriterion = scoreCriterion.internalCriteria.get(i);
            criteria.put(internalCriterion.name(), ((Criterion) internalCriterion));
            requirements.add(Collections.singletonList(internalCriterion.name()));
        }
    } else {
        criteria.put(criterion.name(), (Criterion) criterion);
        requirements.add(Collections.singletonList(criterion.name()));
    }
    return requirements;
}
Also used : SpongeAndCriterion(org.spongepowered.common.advancement.criterion.SpongeAndCriterion) SpongeOperatorCriterion(org.spongepowered.common.advancement.criterion.SpongeOperatorCriterion) SpongeOrCriterion(org.spongepowered.common.advancement.criterion.SpongeOrCriterion) SpongeOperatorCriterion(org.spongepowered.common.advancement.criterion.SpongeOperatorCriterion) SpongeEmptyCriterion(org.spongepowered.common.advancement.criterion.SpongeEmptyCriterion) Criterion(net.minecraft.advancements.Criterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) DefaultedAdvancementCriterion(org.spongepowered.common.advancement.criterion.DefaultedAdvancementCriterion) OperatorCriterion(org.spongepowered.api.advancement.criteria.OperatorCriterion) SpongeAndCriterion(org.spongepowered.common.advancement.criterion.SpongeAndCriterion) SpongeScoreCriterion(org.spongepowered.common.advancement.criterion.SpongeScoreCriterion) DefaultedAdvancementCriterion(org.spongepowered.common.advancement.criterion.DefaultedAdvancementCriterion) ArrayList(java.util.ArrayList) SpongeOrCriterion(org.spongepowered.common.advancement.criterion.SpongeOrCriterion) ArrayList(java.util.ArrayList) List(java.util.List) SpongeScoreCriterion(org.spongepowered.common.advancement.criterion.SpongeScoreCriterion)

Example 4 with SpongeScoreCriterion

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

the class AdvancementMixin method impl$setUpSpongeFields.

@SuppressWarnings({ "ConstantConditions" })
@Inject(method = "<init>", at = @At("RETURN"))
private void impl$setUpSpongeFields(ResourceLocation location, @Nullable Advancement parent, @Nullable DisplayInfo displayInfo, AdvancementRewards rewards, Map<String, Criterion> criteria, String[][] requirements, CallbackInfo ci) {
    // Don't do anything on the client, unless we're performing registry initialization
    if (!PlatformHooks.INSTANCE.getGeneralHooks().onServerThread()) {
        return;
    }
    if (displayInfo != null) {
        ((DisplayInfoBridge) displayInfo).bridge$setAdvancement((org.spongepowered.api.advancement.Advancement) this);
    }
    this.impl$toastText = this.impl$generateToastText();
    final Map<String, DefaultedAdvancementCriterion> criteriaMap = new LinkedHashMap<>();
    final Map<String, List<DefaultedAdvancementCriterion>> scoreCriteria = new HashMap<>();
    for (Map.Entry<String, Criterion> entry : criteria.entrySet()) {
        final CriterionBridge mixinCriterion = (CriterionBridge) entry.getValue();
        final String groupName = mixinCriterion.bridge$getScoreCriterionName();
        if (groupName != null) {
            scoreCriteria.computeIfAbsent(groupName, k -> new ArrayList<>()).add((DefaultedAdvancementCriterion) entry.getValue());
        }
        criteriaMap.put(entry.getKey(), (DefaultedAdvancementCriterion) mixinCriterion);
        mixinCriterion.bridge$setName(entry.getKey());
    }
    for (Map.Entry<String, List<DefaultedAdvancementCriterion>> groupEntry : scoreCriteria.entrySet()) {
        criteriaMap.put(groupEntry.getKey(), new SpongeScoreCriterion(groupEntry.getKey(), groupEntry.getValue()));
        groupEntry.getValue().forEach(c -> criteriaMap.remove(c.name()));
    }
    final Set<AdvancementCriterion> andCriteria = new HashSet<>();
    for (final String[] array : requirements) {
        final Set<AdvancementCriterion> orCriteria = new HashSet<>();
        for (final String name : array) {
            DefaultedAdvancementCriterion criterion = criteriaMap.get(name);
            if (criterion == null && criteria.get(name) != null) {
                // internal removed by scoreCriterion
                criterion = criteriaMap.get(((CriterionBridge) criteria.get(name)).bridge$getScoreCriterionName());
            }
            orCriteria.add(criterion);
        }
        andCriteria.add(OrCriterion.of(orCriteria));
    }
    this.impl$criterion = AndCriterion.of(andCriteria);
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) Inject(org.spongepowered.asm.mixin.injection.Inject) Criterion(net.minecraft.advancements.Criterion) HashMap(java.util.HashMap) Overwrite(org.spongepowered.asm.mixin.Overwrite) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) DisplayInfoBridge(org.spongepowered.common.bridge.advancements.DisplayInfoBridge) ArrayList(java.util.ArrayList) FrameType(net.minecraft.advancements.FrameType) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) DefaultedAdvancementCriterion(org.spongepowered.common.advancement.criterion.DefaultedAdvancementCriterion) ImmutableList(com.google.common.collect.ImmutableList) DisplayInfo(net.minecraft.advancements.DisplayInfo) Mixin(org.spongepowered.asm.mixin.Mixin) Component(net.kyori.adventure.text.Component) Map(java.util.Map) AdvancementBridge(org.spongepowered.common.bridge.advancements.AdvancementBridge) Nullable(javax.annotation.Nullable) At(org.spongepowered.asm.mixin.injection.At) AdvancementRewards(net.minecraft.advancements.AdvancementRewards) Mutable(org.spongepowered.asm.mixin.Mutable) Advancement(net.minecraft.advancements.Advancement) AndCriterion(org.spongepowered.api.advancement.criteria.AndCriterion) OrCriterion(org.spongepowered.api.advancement.criteria.OrCriterion) Set(java.util.Set) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) CriterionBridge(org.spongepowered.common.bridge.advancements.CriterionBridge) Final(org.spongepowered.asm.mixin.Final) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) PlatformHooks(org.spongepowered.common.hooks.PlatformHooks) SpongeScoreCriterion(org.spongepowered.common.advancement.criterion.SpongeScoreCriterion) Shadow(org.spongepowered.asm.mixin.Shadow) Optional(java.util.Optional) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) DefaultedAdvancementCriterion(org.spongepowered.common.advancement.criterion.DefaultedAdvancementCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) SpongeScoreCriterion(org.spongepowered.common.advancement.criterion.SpongeScoreCriterion) DisplayInfoBridge(org.spongepowered.common.bridge.advancements.DisplayInfoBridge) LinkedHashMap(java.util.LinkedHashMap) Criterion(net.minecraft.advancements.Criterion) DefaultedAdvancementCriterion(org.spongepowered.common.advancement.criterion.DefaultedAdvancementCriterion) AndCriterion(org.spongepowered.api.advancement.criteria.AndCriterion) OrCriterion(org.spongepowered.api.advancement.criteria.OrCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) SpongeScoreCriterion(org.spongepowered.common.advancement.criterion.SpongeScoreCriterion) DefaultedAdvancementCriterion(org.spongepowered.common.advancement.criterion.DefaultedAdvancementCriterion) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CriterionBridge(org.spongepowered.common.bridge.advancements.CriterionBridge) HashSet(java.util.HashSet) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 5 with SpongeScoreCriterion

use of org.spongepowered.common.advancement.criterion.SpongeScoreCriterion 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)5 SpongeScoreCriterion (org.spongepowered.common.advancement.criterion.SpongeScoreCriterion)5 ImplementationBackedCriterionProgress (org.spongepowered.common.advancement.criterion.ImplementationBackedCriterionProgress)3 SpongeAndCriterionProgress (org.spongepowered.common.advancement.criterion.SpongeAndCriterionProgress)3 CriterionBridge (org.spongepowered.common.bridge.advancements.CriterionBridge)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AdvancementProgress (net.minecraft.advancements.AdvancementProgress)2 Criterion (net.minecraft.advancements.Criterion)2 AndCriterion (org.spongepowered.api.advancement.criteria.AndCriterion)2 CriterionProgress (org.spongepowered.api.advancement.criteria.CriterionProgress)2 OperatorCriterion (org.spongepowered.api.advancement.criteria.OperatorCriterion)2 OrCriterion (org.spongepowered.api.advancement.criteria.OrCriterion)2 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)2 Cause (org.spongepowered.api.event.Cause)2 CriterionEvent (org.spongepowered.api.event.advancement.CriterionEvent)2 DefaultedAdvancementCriterion (org.spongepowered.common.advancement.criterion.DefaultedAdvancementCriterion)2 SpongeAndCriterion (org.spongepowered.common.advancement.criterion.SpongeAndCriterion)2 SpongeOrCriterionProgress (org.spongepowered.common.advancement.criterion.SpongeOrCriterionProgress)2 SpongeScoreCriterionProgress (org.spongepowered.common.advancement.criterion.SpongeScoreCriterionProgress)2