Search in sources :

Example 6 with Advancement

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

the class LanternCriterionProgress method grant.

@Override
Optional<Instant> grant(Runnable invalidator) {
    if (this.achievingTime != null) {
        return Optional.of(this.achievingTime);
    }
    final Cause cause = CauseStack.current().getCurrentCause();
    final Advancement advancement = this.progress.getAdvancement();
    final CriterionEvent.Grant event = SpongeEventFactory.createCriterionEventGrant(cause, advancement, this.criterion, this.progress.getPlayer(), Instant.now());
    Sponge.getEventManager().post(event);
    if (event.isCancelled()) {
        return Optional.empty();
    }
    this.achievingTime = event.getTime();
    detachTrigger();
    invalidator.run();
    return Optional.of(this.achievingTime);
}
Also used : CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) Cause(org.spongepowered.api.event.cause.Cause) Advancement(org.spongepowered.api.advancement.Advancement)

Example 7 with Advancement

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

the class LanternCriterionProgress method revoke.

@Override
Optional<Instant> revoke(Runnable invalidator) {
    if (this.achievingTime == null) {
        return Optional.empty();
    }
    final Cause cause = CauseStack.current().getCurrentCause();
    final Advancement advancement = this.progress.getAdvancement();
    final CriterionEvent.Revoke event = SpongeEventFactory.createCriterionEventRevoke(cause, advancement, this.criterion, this.progress.getPlayer());
    Sponge.getEventManager().post(event);
    if (event.isCancelled()) {
        return Optional.empty();
    }
    final Instant achievingTime = this.achievingTime;
    this.achievingTime = null;
    attachTrigger();
    invalidator.run();
    return Optional.of(achievingTime);
}
Also used : CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) Cause(org.spongepowered.api.event.cause.Cause) Instant(java.time.Instant) Advancement(org.spongepowered.api.advancement.Advancement)

Example 8 with Advancement

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

the class SpongeTrigger method trigger.

@Override
public void trigger(Player player) {
    final PlayerAdvancements playerAdvancements = ((EntityPlayerMP) player).getAdvancements();
    final Cause cause = Sponge.getCauseStackManager().getCurrentCause();
    final TypeToken typeToken = TypeToken.of(this.triggerConfigurationClass);
    for (Listener listener : new ArrayList<>(this.listeners.get(playerAdvancements))) {
        final IMixinICriterionTriggerListener mixinListener = (IMixinICriterionTriggerListener) listener;
        final Advancement advancement = (Advancement) mixinListener.getAdvancement();
        final AdvancementCriterion advancementCriterion = (AdvancementCriterion) ((net.minecraft.advancements.Advancement) advancement).getCriteria().get(mixinListener.getCriterionName());
        final CriterionEvent.Trigger event = SpongeEventFactory.createCriterionEventTrigger(cause, advancement, advancementCriterion, typeToken, player, (FilteredTrigger) listener.getCriterionInstance(), this.eventHandler == null);
        if (this.eventHandler != null) {
            this.eventHandler.accept(event);
            if (!event.getResult()) {
                continue;
            }
        }
        SpongeImpl.postEvent(event);
        if (event.getResult()) {
            listener.grantCriterion(playerAdvancements);
        }
    }
}
Also used : IMixinICriterionTriggerListener(org.spongepowered.common.interfaces.advancement.IMixinICriterionTriggerListener) PlayerAdvancements(net.minecraft.advancements.PlayerAdvancements) CriterionEvent(org.spongepowered.api.event.advancement.CriterionEvent) TypeToken(com.google.common.reflect.TypeToken) Cause(org.spongepowered.api.event.cause.Cause) IMixinICriterionTriggerListener(org.spongepowered.common.interfaces.advancement.IMixinICriterionTriggerListener) ArrayList(java.util.ArrayList) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) Advancement(org.spongepowered.api.advancement.Advancement)

Example 9 with Advancement

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

the class AdvancementRegistryModule method registerAdditionalCatalog.

@Override
public void registerAdditionalCatalog(Advancement advancement) {
    super.register(advancement);
    ((IMixinAdvancement) advancement).setRegistered();
    if (INSIDE_REGISTER_EVENT) {
        final net.minecraft.advancements.Advancement mcAdv = (net.minecraft.advancements.Advancement) advancement;
        final IMixinAdvancementList advancementList = (IMixinAdvancementList) AdvancementManager.ADVANCEMENT_LIST;
        advancementList.getAdvancements().put(mcAdv.getId(), mcAdv);
        if (mcAdv.getParent() != SpongeAdvancementBuilder.DUMMY_ROOT_ADVANCEMENT) {
            advancementList.getNonRootsSet().add(mcAdv);
            final AdvancementList.Listener listener = advancementList.getListener();
            if (listener != null) {
                listener.nonRootAdvancementAdded(mcAdv);
            }
        }
    }
}
Also used : AdvancementList(net.minecraft.advancements.AdvancementList) IMixinAdvancementList(org.spongepowered.common.interfaces.advancement.IMixinAdvancementList) IMixinAdvancementList(org.spongepowered.common.interfaces.advancement.IMixinAdvancementList) IMixinAdvancement(org.spongepowered.common.interfaces.advancement.IMixinAdvancement) Advancement(org.spongepowered.api.advancement.Advancement) IMixinAdvancement(org.spongepowered.common.interfaces.advancement.IMixinAdvancement)

Example 10 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)

Aggregations

Advancement (org.spongepowered.api.advancement.Advancement)13 Cause (org.spongepowered.api.event.cause.Cause)7 AdvancementCriterion (org.spongepowered.api.advancement.criteria.AdvancementCriterion)4 CriterionEvent (org.spongepowered.api.event.advancement.CriterionEvent)4 Instant (java.time.Instant)3 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 AbstractCriterionProgress (org.lanternpowered.server.advancement.criteria.progress.AbstractCriterionProgress)2 ScoreAdvancementCriterion (org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion)2 ScoreCriterionProgress (org.spongepowered.api.advancement.criteria.ScoreCriterionProgress)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 Object2LongMap (it.unimi.dsi.fastutil.objects.Object2LongMap)1 Object2LongOpenHashMap (it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 AdvancementList (net.minecraft.advancements.AdvancementList)1