Search in sources :

Example 16 with Advancement

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

the class LanternAdvancementProgress method invalidateAchievedState.

public void invalidateAchievedState() {
    // Invalidate the achieved state of all the criteria progress
    this.progress.values().forEach(AbstractCriterionProgress::invalidateAchievedState);
    // Get the new achieved state
    final boolean achievedState = achieved();
    if (!this.achievedState && achievedState) {
        // The advancement got granted
        this.dirtyVisibility = true;
        for (AbstractCriterionProgress progress : this.progress.values()) {
            progress.detachTrigger();
        }
        final Optional<DisplayInfo> optDisplay = this.advancement.getDisplayInfo();
        final boolean sendMessage = getPlayer().getWorld().getOrCreateRule(RuleTypes.ANNOUNCE_ADVANCEMENTS).getValue() && optDisplay.map(DisplayInfo::doesAnnounceToChat).orElse(false);
        final Text message = optDisplay.<Text>map(display -> {
            final Translation translation = tr("chat.type.advancement." + display.getType().getName().toLowerCase());
            return Text.of(translation, getPlayer().getName(), this.advancement.toText());
        }).orElseGet(() -> Text.of(getPlayer().getName() + " achieved ", this.advancement.toText()));
        final MessageEvent.MessageFormatter formatter = new MessageEvent.MessageFormatter(message);
        final Cause cause = CauseStack.current().getCurrentCause();
        final Instant instant = get().orElseThrow(() -> new IllegalStateException("Something funky happened"));
        final AdvancementEvent.Grant event = SpongeEventFactory.createAdvancementEventGrant(cause, MessageChannel.TO_ALL, Optional.of(MessageChannel.TO_ALL), this.advancement, formatter, getPlayer(), instant, !sendMessage);
        Sponge.getEventManager().post(event);
        if (!event.isMessageCancelled()) {
            event.getChannel().ifPresent(channel -> channel.send(event.getMessage()));
        }
    } else if (this.achievedState && !achievedState) {
        // The advancement got revoked
        this.dirtyVisibility = true;
        for (AbstractCriterionProgress progress : this.progress.values()) {
            if (!progress.achieved()) {
                progress.attachTrigger();
            }
        }
        final Cause cause = CauseStack.current().getCurrentCause();
        final AdvancementEvent.Revoke event = SpongeEventFactory.createAdvancementEventRevoke(cause, this.advancement, getPlayer());
        Sponge.getEventManager().post(event);
    }
    this.achievedState = achievedState;
    // The progress should be updated
    this.dirtyProgress = true;
    this.playerAdvancements.dirtyProgress.add(this);
}
Also used : DisplayInfo(org.spongepowered.api.advancement.DisplayInfo) AdvancementEvent(org.spongepowered.api.event.advancement.AdvancementEvent) AbstractCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.AbstractCriterionProgress) ScoreAdvancementCriterion(org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion) HashMap(java.util.HashMap) Object2LongMap(it.unimi.dsi.fastutil.objects.Object2LongMap) EmptyCriterion(org.lanternpowered.server.advancement.criteria.EmptyCriterion) CauseStack(org.lanternpowered.server.event.CauseStack) Text(org.spongepowered.api.text.Text) LanternOrCriterion(org.lanternpowered.server.advancement.criteria.LanternOrCriterion) AbstractCriterion(org.lanternpowered.server.advancement.criteria.AbstractCriterion) RuleTypes(org.lanternpowered.server.world.rules.RuleTypes) Map(java.util.Map) LanternAndCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.LanternAndCriterionProgress) AdvancementProgress(org.spongepowered.api.advancement.AdvancementProgress) Translation(org.spongepowered.api.text.translation.Translation) LanternCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.LanternCriterionProgress) LanternAndCriterion(org.lanternpowered.server.advancement.criteria.LanternAndCriterion) TranslationHelper.tr(org.lanternpowered.server.text.translation.TranslationHelper.tr) LanternScoreCriterion(org.lanternpowered.server.advancement.criteria.LanternScoreCriterion) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Sponge(org.spongepowered.api.Sponge) LanternScoreCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.LanternScoreCriterionProgress) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) Instant(java.time.Instant) ScoreCriterionProgress(org.spongepowered.api.advancement.criteria.ScoreCriterionProgress) Cause(org.spongepowered.api.event.cause.Cause) LanternOrCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.LanternOrCriterionProgress) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) LanternCriterion(org.lanternpowered.server.advancement.criteria.LanternCriterion) LanternEmptyCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.LanternEmptyCriterionProgress) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) Optional(java.util.Optional) CriterionProgress(org.spongepowered.api.advancement.criteria.CriterionProgress) Advancement(org.spongepowered.api.advancement.Advancement) MessageEvent(org.spongepowered.api.event.message.MessageEvent) Object2LongOpenHashMap(it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap) Translation(org.spongepowered.api.text.translation.Translation) DisplayInfo(org.spongepowered.api.advancement.DisplayInfo) MessageEvent(org.spongepowered.api.event.message.MessageEvent) AdvancementEvent(org.spongepowered.api.event.advancement.AdvancementEvent) Instant(java.time.Instant) Text(org.spongepowered.api.text.Text) AbstractCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.AbstractCriterionProgress) Cause(org.spongepowered.api.event.cause.Cause)

Example 17 with Advancement

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

the class LanternPlayerAdvancements method update.

private void update(LanternAdvancementProgress progress, @Nullable List<MessagePlayOutAdvancements.AdvStruct> added, @Nullable List<String> removed, @Nullable Map<String, Object2LongMap<String>> progressMap, boolean force) {
    final Advancement advancement = progress.getAdvancement();
    boolean updateParentAndChildren = false;
    if (progress.dirtyVisibility || force) {
        final boolean visible = shouldBeVisible(advancement);
        progress.dirtyVisibility = false;
        if (progress.visible != visible) {
            progress.visible = visible;
            if (visible && added != null) {
                added.add(createAdvancement(advancement));
                // The progress is now visible, send the complete data
                if (progressMap != null) {
                    final Object2LongMap<String> progressMap1 = progress.collectProgress();
                    if (!progressMap1.isEmpty()) {
                        progressMap.put(advancement.getId(), progressMap1);
                    }
                    // The progress is already updated, prevent from doing it again
                    progress.dirtyProgress = false;
                }
            } else if (removed != null) {
                removed.add(advancement.getId());
            }
            updateParentAndChildren = true;
        }
    }
    if (progress.visible && progress.dirtyProgress && progressMap != null) {
        progressMap.put(advancement.getId(), progress.collectProgress());
    }
    // Reset dirty state, even if nothing changed
    progress.dirtyProgress = false;
    if (updateParentAndChildren) {
        for (Advancement child : advancement.getChildren()) {
            update(get(child), added, removed, progressMap, true);
        }
        advancement.getParent().ifPresent(parent -> update(get(parent), added, removed, progressMap, true));
    }
}
Also used : Advancement(org.spongepowered.api.advancement.Advancement)

Example 18 with Advancement

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

the class LanternPlayerAdvancements method init0.

private void init0() {
    cleanup();
    // Load all the advancements into this progress tracker
    final AdvancementRegistryModule registryModule = AdvancementRegistryModule.get();
    for (Advancement advancement : registryModule.getAll()) {
        final LanternAdvancementProgress progress = get(advancement);
        // Update the visibility
        progress.dirtyVisibility = true;
        this.dirtyProgress.add(progress);
    }
}
Also used : Advancement(org.spongepowered.api.advancement.Advancement) AdvancementRegistryModule(org.lanternpowered.server.game.registry.type.advancement.AdvancementRegistryModule)

Example 19 with Advancement

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

the class LanternTreeLayout method generateNode.

private static TreeNode generateNode(Advancement advancement, TreeLayoutElement element, int column) {
    final TreeNode treeNode = new TreeNode(element, (double) column * HORIZONTAL_OFFSET);
    TreeNode previous = null;
    for (Advancement child : advancement.getChildren()) {
        final TreeLayoutElement childElement = ((LanternAdvancement) child).getLayoutElement();
        if (childElement != null) {
            final TreeNode childNode = generateNode(child, childElement, column + 1);
            childNode.previous = previous;
            childNode.parent = treeNode;
            if (previous != null) {
                previous.next = childNode;
            }
            treeNode.children.add(childNode);
            previous = childNode;
        }
    }
    return treeNode;
}
Also used : TreeLayoutElement(org.spongepowered.api.advancement.TreeLayoutElement) LanternAdvancement(org.lanternpowered.server.advancement.LanternAdvancement) LanternAdvancement(org.lanternpowered.server.advancement.LanternAdvancement) Advancement(org.spongepowered.api.advancement.Advancement)

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