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