Search in sources :

Example 1 with AbstractCriterionProgress

use of org.lanternpowered.server.advancement.criteria.progress.AbstractCriterionProgress 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)

Example 2 with AbstractCriterionProgress

use of org.lanternpowered.server.advancement.criteria.progress.AbstractCriterionProgress 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

AbstractCriterionProgress (org.lanternpowered.server.advancement.criteria.progress.AbstractCriterionProgress)2 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)2 Advancement (org.spongepowered.api.advancement.Advancement)2 AdvancementCriterion (org.spongepowered.api.advancement.criteria.AdvancementCriterion)2 ScoreCriterionProgress (org.spongepowered.api.advancement.criteria.ScoreCriterionProgress)2 Cause (org.spongepowered.api.event.cause.Cause)2 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 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 LanternPlayerAdvancements (org.lanternpowered.server.advancement.LanternPlayerAdvancements)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