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