Search in sources :

Example 1 with IMixinCriterion

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

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

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

Example 4 with IMixinCriterion

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

the class MixinPlayerAdvancements method onUnregisterListenersGetProgress.

@Redirect(method = "registerListeners(Lnet/minecraft/advancements/Advancement;)V", at = @At(value = "INVOKE", ordinal = 0, target = "Lnet/minecraft/advancements/CriterionProgress;isObtained()Z"))
private boolean onUnregisterListenersGetProgress(CriterionProgress progress) {
    final AdvancementCriterion criterion = ((org.spongepowered.api.advancement.criteria.CriterionProgress) progress).getCriterion();
    final IMixinCriterion mixinCriterion = (IMixinCriterion) criterion;
    // Only remove the trigger once the goal is reached
    if (mixinCriterion.getScoreCriterion() != null) {
        return ((IMixinCriterionProgress) progress).getAdvancementProgress().get(mixinCriterion.getScoreCriterion()).get().achieved();
    }
    return progress.isObtained();
}
Also used : IMixinCriterion(org.spongepowered.common.interfaces.advancement.IMixinCriterion) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) CriterionProgress(net.minecraft.advancements.CriterionProgress) IMixinCriterionProgress(org.spongepowered.common.interfaces.advancement.IMixinCriterionProgress) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 5 with IMixinCriterion

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

Aggregations

AdvancementCriterion (org.spongepowered.api.advancement.criteria.AdvancementCriterion)7 IMixinCriterion (org.spongepowered.common.interfaces.advancement.IMixinCriterion)7 Advancement (net.minecraft.advancements.Advancement)3 ScoreAdvancementCriterion (org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion)3 Player (org.spongepowered.api.entity.living.player.Player)3 CriterionEvent (org.spongepowered.api.event.advancement.CriterionEvent)3 Cause (org.spongepowered.api.event.cause.Cause)3 SpongeScoreCriterion (org.spongepowered.common.advancement.SpongeScoreCriterion)3 IMixinCriterionProgress (org.spongepowered.common.interfaces.advancement.IMixinCriterionProgress)3 IMixinPlayerAdvancements (org.spongepowered.common.interfaces.advancement.IMixinPlayerAdvancements)3 Criterion (net.minecraft.advancements.Criterion)2 CriterionProgress (org.spongepowered.api.advancement.criteria.CriterionProgress)2 ScoreCriterionProgress (org.spongepowered.api.advancement.criteria.ScoreCriterionProgress)2 Overwrite (org.spongepowered.asm.mixin.Overwrite)2 Inject (org.spongepowered.asm.mixin.injection.Inject)2 Redirect (org.spongepowered.asm.mixin.injection.Redirect)2 ICriterionProgress (org.spongepowered.common.advancement.ICriterionProgress)2 SpongeAndCriterionProgress (org.spongepowered.common.advancement.SpongeAndCriterionProgress)2 SpongeOrCriterionProgress (org.spongepowered.common.advancement.SpongeOrCriterionProgress)2 SpongeScoreCriterionProgress (org.spongepowered.common.advancement.SpongeScoreCriterionProgress)2