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