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();
}
}
}
}
}
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);
}
Aggregations