Search in sources :

Example 1 with Advancement

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

the class SpongeAdvancementBuilder method build.

@Override
public Advancement build() {
    checkState(this.id != null, "The id must be set");
    final PluginContainer plugin = Sponge.getCauseStackManager().getCurrentCause().first(PluginContainer.class).get();
    final Tuple<Map<String, Criterion>, String[][]> result = SpongeCriterionHelper.toVanillaCriteriaData(this.criterion);
    final AdvancementRewards rewards = AdvancementRewards.EMPTY;
    final ResourceLocation resourceLocation = new ResourceLocation(plugin.getId(), this.id);
    final net.minecraft.advancements.DisplayInfo displayInfo = this.displayInfo == null ? null : // Create a copy
    (net.minecraft.advancements.DisplayInfo) DisplayInfo.builder().from(this.displayInfo).build();
    net.minecraft.advancements.Advancement parent = (net.minecraft.advancements.Advancement) this.parent;
    if (parent == null) {
        // Attach a dummy root until a tree is constructed
        parent = DUMMY_ROOT_ADVANCEMENT;
    }
    final Advancement advancement = (Advancement) new net.minecraft.advancements.Advancement(resourceLocation, parent, displayInfo, rewards, result.getFirst(), result.getSecond());
    ((IMixinAdvancement) advancement).setCriterion(this.criterion);
    if (StringUtils.isNotEmpty(this.name)) {
        ((IMixinAdvancement) advancement).setName(this.name);
    }
    return advancement;
}
Also used : PluginContainer(org.spongepowered.api.plugin.PluginContainer) AdvancementRewards(net.minecraft.advancements.AdvancementRewards) ResourceLocation(net.minecraft.util.ResourceLocation) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Advancement(org.spongepowered.api.advancement.Advancement) IMixinAdvancement(org.spongepowered.common.interfaces.advancement.IMixinAdvancement) IMixinAdvancement(org.spongepowered.common.interfaces.advancement.IMixinAdvancement)

Example 2 with Advancement

use of org.spongepowered.api.advancement.Advancement 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 3 with Advancement

use of org.spongepowered.api.advancement.Advancement in project LanternServer by LanternPowered.

the class LanternPlayerAdvancements method getUnlockedAdvancementTrees.

public Collection<AdvancementTree> getUnlockedAdvancementTrees() {
    final ImmutableList.Builder<AdvancementTree> builder = ImmutableList.builder();
    for (AdvancementTree tree : AdvancementTreeRegistryModule.get().getAll()) {
        final Advancement advancement = tree.getRootAdvancement();
        final LanternAdvancementProgress progress = get(advancement);
        if ((!progress.dirtyVisibility && progress.visible) || (progress.dirtyVisibility && shouldBeVisible(advancement))) {
            builder.add(tree);
        }
    }
    return builder.build();
}
Also used : AdvancementTree(org.spongepowered.api.advancement.AdvancementTree) ImmutableList(com.google.common.collect.ImmutableList) Advancement(org.spongepowered.api.advancement.Advancement)

Example 4 with Advancement

use of org.spongepowered.api.advancement.Advancement in project LanternServer by LanternPowered.

the class LanternScoreCriterionProgress method set.

public Optional<Instant> set(int score, Runnable invalidator) {
    checkArgument(score >= 0, "Score to set may not be negative");
    checkArgument(score <= getGoal(), "Score to set may not be greater than the goal");
    if (score == this.score) {
        return get();
    }
    final Cause cause = CauseStack.current().getCurrentCause();
    final Advancement advancement = this.progress.getAdvancement();
    final LanternPlayer player = this.progress.getPlayer();
    CriterionEvent.Score.Change event;
    if (score == getGoal()) {
        event = SpongeEventFactory.createCriterionEventScoreGrant(cause, advancement, this.criterion, player, Instant.now(), score, this.score);
    } else if (this.score == getGoal()) {
        event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, advancement, this.criterion, player, score, this.score);
    } else {
        event = SpongeEventFactory.createCriterionEventScoreChange(cause, advancement, this.criterion, player, score, this.score);
    }
    Sponge.getEventManager().post(event);
    if (event.isCancelled()) {
        return get();
    }
    this.score = score;
    if (event instanceof CriterionEvent.Grant) {
        this.achievingTime = ((CriterionEvent.Grant) event).getTime();
        detachTrigger();
    } else if (event instanceof CriterionEvent.Revoke) {
        this.achievingTime = null;
        attachTrigger();
    }
    invalidator.run();
    return Optional.ofNullable(this.achievingTime);
}
Also used : CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) Cause(org.spongepowered.api.event.cause.Cause) Advancement(org.spongepowered.api.advancement.Advancement) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 5 with Advancement

use of org.spongepowered.api.advancement.Advancement in project LanternServer by LanternPowered.

the class LanternTrigger method trigger.

@SuppressWarnings("unchecked")
@Override
public void trigger(Player player) {
    final LanternPlayerAdvancements playerAdvancements = ((LanternPlayer) player).getAdvancementsProgress();
    final Collection<AbstractCriterionProgress> collection = this.progress.get(playerAdvancements);
    if (!collection.isEmpty()) {
        final Cause cause = CauseStack.current().getCurrentCause();
        for (AbstractCriterionProgress progress : new ArrayList<>(this.progress.get(playerAdvancements))) {
            final Advancement advancement = progress.getAdvancementProgress().getAdvancement();
            final AdvancementCriterion criterion = progress.getCriterion();
            final FilteredTrigger filteredTrigger = criterion.getTrigger().get();
            final CriterionEvent.Trigger event = SpongeEventFactory.createCriterionEventTrigger(cause, advancement, criterion, this.configTypeToken, player, filteredTrigger, this.eventHandler != null);
            if (this.eventHandler != null) {
                this.eventHandler.accept(event);
            }
            Sponge.getEventManager().post(event);
            if (event.getResult()) {
                if (progress instanceof ScoreCriterionProgress) {
                    ((ScoreCriterionProgress) progress).add(1);
                } else {
                    progress.grant();
                }
            }
        }
    }
}
Also used : ScoreCriterionProgress(org.spongepowered.api.advancement.criteria.ScoreCriterionProgress) CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) AbstractCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.AbstractCriterionProgress) Cause(org.spongepowered.api.event.cause.Cause) ArrayList(java.util.ArrayList) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) LanternPlayerAdvancements(org.lanternpowered.server.advancement.LanternPlayerAdvancements) FilteredTrigger(org.spongepowered.api.advancement.criteria.trigger.FilteredTrigger) Advancement(org.spongepowered.api.advancement.Advancement) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Aggregations

Advancement (org.spongepowered.api.advancement.Advancement)19 AdvancementCriterion (org.spongepowered.api.advancement.criteria.AdvancementCriterion)8 CriterionEvent (org.spongepowered.api.event.advancement.CriterionEvent)7 Cause (org.spongepowered.api.event.cause.Cause)7 Instant (java.time.Instant)4 ScoreAdvancementCriterion (org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)3 CriterionProgress (org.spongepowered.api.advancement.criteria.CriterionProgress)3 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)3 Cause (org.spongepowered.api.event.Cause)3 AdvancementRewards (net.minecraft.advancements.AdvancementRewards)2 AbstractCriterionProgress (org.lanternpowered.server.advancement.criteria.progress.AbstractCriterionProgress)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 TypeToken (com.google.common.reflect.TypeToken)1 TypeToken (io.leangen.geantyref.TypeToken)1 Object2LongMap (it.unimi.dsi.fastutil.objects.Object2LongMap)1