Search in sources :

Example 1 with DisplayInfo

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

the class LanternPlayerAdvancements method createAdvancement.

@SuppressWarnings("ConstantConditions")
private static MessagePlayOutAdvancements.AdvStruct createAdvancement(Locale locale, Advancement advancement) {
    final String parentId = advancement.getParent().map(Advancement::getId).orElse(null);
    final String background = parentId == null ? advancement.getTree().get().getBackgroundPath() : null;
    final DisplayInfo displayInfo = advancement.getDisplayInfo().orElse(null);
    final TreeLayoutElement layoutElement = ((LanternAdvancement) advancement).getLayoutElement();
    final String[][] criteriaRequirements = ((LanternAdvancement) advancement).clientCriteria.getSecond();
    final Set<String> criteria = new HashSet<>();
    for (String[] array : criteriaRequirements) {
        Collections.addAll(criteria, array);
    }
    return new MessagePlayOutAdvancements.AdvStruct(advancement.getId(), parentId, displayInfo == null ? null : createDisplay(locale, displayInfo, layoutElement, background), criteria, criteriaRequirements);
}
Also used : TreeLayoutElement(org.spongepowered.api.advancement.TreeLayoutElement) DisplayInfo(org.spongepowered.api.advancement.DisplayInfo) HashSet(java.util.HashSet)

Example 2 with DisplayInfo

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

the class SpongeDisplayInfoBuilder method build.

@Override
public DisplayInfo build() {
    checkState(this.title != null, "Title has not been set");
    checkState(this.icon != null, "Icon has not been set");
    final ITextComponent title = SpongeTexts.toComponent(this.title);
    final ITextComponent description = SpongeTexts.toComponent(this.description);
    final FrameType frameType = (FrameType) (Object) this.advancementType;
    final net.minecraft.item.ItemStack icon = (net.minecraft.item.ItemStack) this.icon.createStack();
    return (DisplayInfo) new net.minecraft.advancements.DisplayInfo(icon, title, description, null, frameType, this.showToast, this.announceToChat, this.hidden);
}
Also used : DisplayInfo(org.spongepowered.api.advancement.DisplayInfo) FrameType(net.minecraft.advancements.FrameType) ITextComponent(net.minecraft.util.text.ITextComponent)

Example 3 with DisplayInfo

use of org.spongepowered.api.advancement.DisplayInfo 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)

Aggregations

DisplayInfo (org.spongepowered.api.advancement.DisplayInfo)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Object2LongMap (it.unimi.dsi.fastutil.objects.Object2LongMap)1 Object2LongOpenHashMap (it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap)1 Instant (java.time.Instant)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Optional (java.util.Optional)1 FrameType (net.minecraft.advancements.FrameType)1 ITextComponent (net.minecraft.util.text.ITextComponent)1 AbstractCriterion (org.lanternpowered.server.advancement.criteria.AbstractCriterion)1 EmptyCriterion (org.lanternpowered.server.advancement.criteria.EmptyCriterion)1 LanternAndCriterion (org.lanternpowered.server.advancement.criteria.LanternAndCriterion)1 LanternCriterion (org.lanternpowered.server.advancement.criteria.LanternCriterion)1 LanternOrCriterion (org.lanternpowered.server.advancement.criteria.LanternOrCriterion)1 LanternScoreCriterion (org.lanternpowered.server.advancement.criteria.LanternScoreCriterion)1 AbstractCriterionProgress (org.lanternpowered.server.advancement.criteria.progress.AbstractCriterionProgress)1 LanternAndCriterionProgress (org.lanternpowered.server.advancement.criteria.progress.LanternAndCriterionProgress)1 LanternCriterionProgress (org.lanternpowered.server.advancement.criteria.progress.LanternCriterionProgress)1