Search in sources :

Example 16 with AdvancementCriterion

use of org.spongepowered.api.advancement.criteria.AdvancementCriterion 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 17 with AdvancementCriterion

use of org.spongepowered.api.advancement.criteria.AdvancementCriterion 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 18 with AdvancementCriterion

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

the class MixinAdvancement method onInit.

@SuppressWarnings("ConstantConditions")
@Inject(method = "<init>", at = @At("RETURN"))
private void onInit(ResourceLocation id, @Nullable Advancement parentIn, @Nullable DisplayInfo displayIn, AdvancementRewards rewardsIn, Map<String, Criterion> criteriaIn, String[][] requirementsIn, CallbackInfo ci) {
    if (displayIn != null) {
        ((IMixinDisplayInfo) displayIn).setAdvancement(this);
    }
    String path = id.getResourcePath();
    this.name = path.replace('/', '_');
    this.spongeId = id.getResourceDomain() + ':' + this.name;
    if (displayIn != null) {
        this.name = SpongeTexts.toPlain(displayIn.getTitle());
    }
    if (!AdvancementRegistryModule.INSIDE_REGISTER_EVENT) {
        AdvancementRegistryModule.getInstance().registerAdditionalCatalog(this);
    } else {
        // Wait to set the parent until the advancement is registered
        this.tempParent = parentIn;
        this.parent = SpongeAdvancementBuilder.DUMMY_ROOT_ADVANCEMENT;
    }
    // This is only possible when REGISTER_ADVANCEMENTS_ON_CONSTRUCT is true
    if (parentIn == null) {
        // Remove the root suffix from json file tree ids
        if (path.endsWith("/root")) {
            path = path.substring(0, path.lastIndexOf('/'));
        }
        path = path.replace('/', '_');
        String name = path;
        if (displayIn != null) {
            name = this.name;
        }
        path = id.getResourceDomain() + ':' + path;
        this.tree = new SpongeAdvancementTree(this, path, name);
        AdvancementTreeRegistryModule.getInstance().registerAdditionalCatalog(this.tree);
    } else {
        this.tree = ((org.spongepowered.api.advancement.Advancement) parentIn).getTree().orElse(null);
    }
    this.text = SpongeTexts.toText(this.displayText);
    final ImmutableList.Builder<Text> toastText = ImmutableList.builder();
    if (this.display != null) {
        final FrameType frameType = this.display.getFrame();
        toastText.add(Text.builder(new SpongeTranslation("advancements.toast." + frameType.getName())).format(((AdvancementType) (Object) frameType).getTextFormat()).build());
        toastText.add(getDisplayInfo().get().getTitle());
    } else {
        toastText.add(Text.of("Unlocked advancement"));
        toastText.add(Text.of(getId()));
    }
    this.toastText = toastText.build();
    final Set<String> scoreCriteria = new HashSet<>();
    final Map<String, ICriterion> criterionMap = new HashMap<>();
    for (Map.Entry<String, Criterion> entry : new HashMap<>(criteriaIn).entrySet()) {
        final IMixinCriterion mixinCriterion = (IMixinCriterion) entry.getValue();
        final ICriterion criterion;
        if (mixinCriterion.getScoreGoal() != null) {
            criterion = new SpongeScoreCriterion(entry.getKey(), mixinCriterion.getScoreGoal(), entry.getValue().getCriterionInstance());
            scoreCriteria.add(entry.getKey());
            ((SpongeScoreCriterion) criterion).internalCriteria.forEach(criterion1 -> criteriaIn.put(criterion1.getName(), (Criterion) criterion1));
        } else {
            criterion = (ICriterion) mixinCriterion;
            ((IMixinCriterion) criterion).setName(entry.getKey());
        }
        criterionMap.put(entry.getKey(), criterion);
    }
    final List<String[]> entries = new ArrayList<>();
    final List<AdvancementCriterion> andCriteria = new ArrayList<>();
    for (String[] array : requirementsIn) {
        final Set<AdvancementCriterion> orCriteria = new HashSet<>();
        for (String name : array) {
            final ICriterion criterion = criterionMap.get(name);
            if (criterion instanceof SpongeScoreCriterion) {
                ((SpongeScoreCriterion) criterion).internalCriteria.forEach(criterion1 -> entries.add(new String[] { criterion1.getName() }));
            } else {
                entries.add(new String[] { criterion.getName() });
            }
            orCriteria.add(criterion);
        }
        andCriteria.add(OrCriterion.of(orCriteria));
    }
    this.criterion = AndCriterion.of(andCriteria);
    if (!scoreCriteria.isEmpty()) {
        scoreCriteria.forEach(criteriaIn::remove);
        this.criteria = ImmutableMap.copyOf(criteriaIn);
        this.requirements = entries.toArray(new String[entries.size()][]);
    }
}
Also used : SpongeTranslation(org.spongepowered.common.text.translation.SpongeTranslation) SpongeAdvancementTree(org.spongepowered.common.advancement.SpongeAdvancementTree) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) SpongeScoreCriterion(org.spongepowered.common.advancement.SpongeScoreCriterion) IMixinDisplayInfo(org.spongepowered.common.interfaces.advancement.IMixinDisplayInfo) Criterion(net.minecraft.advancements.Criterion) AndCriterion(org.spongepowered.api.advancement.criteria.AndCriterion) OrCriterion(org.spongepowered.api.advancement.criteria.OrCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) ICriterion(org.spongepowered.common.advancement.ICriterion) SpongeScoreCriterion(org.spongepowered.common.advancement.SpongeScoreCriterion) IMixinCriterion(org.spongepowered.common.interfaces.advancement.IMixinCriterion) FrameType(net.minecraft.advancements.FrameType) ICriterion(org.spongepowered.common.advancement.ICriterion) HashSet(java.util.HashSet) Text(org.spongepowered.api.text.Text) IMixinCriterion(org.spongepowered.common.interfaces.advancement.IMixinCriterion) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Advancement(net.minecraft.advancements.Advancement) IMixinAdvancement(org.spongepowered.common.interfaces.advancement.IMixinAdvancement) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 19 with AdvancementCriterion

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

the class MixinPlayerAdvancements method onUnregisterListenersGetProgress.

@Nullable
@Redirect(method = "unregisterListeners", at = @At(value = "INVOKE", ordinal = 0, target = "Lnet/minecraft/advancements/AdvancementProgress;getCriterionProgress(Ljava/lang/String;)Lnet/minecraft/advancements/CriterionProgress;"))
private CriterionProgress onUnregisterListenersGetProgress(AdvancementProgress advancementProgress, String criterion) {
    final org.spongepowered.api.advancement.Advancement advancement = ((org.spongepowered.api.advancement.AdvancementProgress) advancementProgress).getAdvancement();
    final AdvancementCriterion advancementCriterion = (AdvancementCriterion) ((Advancement) advancement).getCriteria().get(criterion);
    final IMixinCriterion mixinCriterion = (IMixinCriterion) advancementCriterion;
    // Only remove the trigger once the goal is reached
    if (mixinCriterion.getScoreCriterion() != null && !((org.spongepowered.api.advancement.AdvancementProgress) advancementProgress).get(mixinCriterion.getScoreCriterion()).get().achieved()) {
        return null;
    }
    return advancementProgress.getCriterionProgress(criterion);
}
Also used : AdvancementProgress(net.minecraft.advancements.AdvancementProgress) IMixinAdvancementProgress(org.spongepowered.common.interfaces.advancement.IMixinAdvancementProgress) IMixinCriterion(org.spongepowered.common.interfaces.advancement.IMixinCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) Advancement(net.minecraft.advancements.Advancement) Redirect(org.spongepowered.asm.mixin.injection.Redirect) Nullable(javax.annotation.Nullable)

Example 20 with AdvancementCriterion

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

the class SpongeCriterionBuilder method build0.

@Override
AdvancementCriterion build0() {
    final Criterion criterion = new Criterion((ICriterionInstance) this.trigger);
    ((IMixinCriterion) criterion).setName(this.name);
    return (AdvancementCriterion) criterion;
}
Also used : IMixinCriterion(org.spongepowered.common.interfaces.advancement.IMixinCriterion) IMixinCriterion(org.spongepowered.common.interfaces.advancement.IMixinCriterion) Criterion(net.minecraft.advancements.Criterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion)

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