use of org.spongepowered.api.advancement.Advancement in project SpongeCommon by SpongePowered.
the class SpongeAdvancementBuilder method build.
@Override
public Advancement build() {
checkState(this.id != null, "The id must be set");
final PluginContainer plugin = Sponge.getCauseStackManager().getCurrentCause().first(PluginContainer.class).get();
final Tuple<Map<String, Criterion>, String[][]> result = SpongeCriterionHelper.toVanillaCriteriaData(this.criterion);
final AdvancementRewards rewards = AdvancementRewards.EMPTY;
final ResourceLocation resourceLocation = new ResourceLocation(plugin.getId(), this.id);
final net.minecraft.advancements.DisplayInfo displayInfo = this.displayInfo == null ? null : // Create a copy
(net.minecraft.advancements.DisplayInfo) DisplayInfo.builder().from(this.displayInfo).build();
net.minecraft.advancements.Advancement parent = (net.minecraft.advancements.Advancement) this.parent;
if (parent == null) {
// Attach a dummy root until a tree is constructed
parent = DUMMY_ROOT_ADVANCEMENT;
}
final Advancement advancement = (Advancement) new net.minecraft.advancements.Advancement(resourceLocation, parent, displayInfo, rewards, result.getFirst(), result.getSecond());
((IMixinAdvancement) advancement).setCriterion(this.criterion);
if (StringUtils.isNotEmpty(this.name)) {
((IMixinAdvancement) advancement).setName(this.name);
}
return advancement;
}
use of org.spongepowered.api.advancement.Advancement in project SpongeCommon by SpongePowered.
the class SpongeScoreCriterionProgress method set.
@Override
public Optional<Instant> set(int score) {
checkState(score >= 0 && score <= getGoal(), "Score cannot be negative or greater than the goal.");
int lastScore = getScore();
if (lastScore == score) {
return get();
}
final CriterionEvent.Score.Change event;
final Cause cause = SpongeImpl.getCauseStackManager().getCurrentCause();
final Advancement advancement = this.progress.getAdvancement();
final Player player = ((IMixinPlayerAdvancements) ((IMixinAdvancementProgress) this.progress).getPlayerAdvancements()).getPlayer();
if (lastScore == getGoal()) {
event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, advancement, getCriterion(), player, lastScore, score);
} else if (score == getGoal()) {
event = SpongeEventFactory.createCriterionEventScoreGrant(cause, advancement, getCriterion(), player, Instant.now(), lastScore, score);
} else {
event = SpongeEventFactory.createCriterionEventScoreChange(cause, advancement, getCriterion(), player, lastScore, score);
}
if (SpongeImpl.postEvent(event)) {
return get();
}
SpongeScoreCriterion.BYPASS_EVENT = true;
// This is the only case a instant will be returned
if (score == getGoal()) {
Instant instant = null;
for (AdvancementCriterion criterion : this.criterion.internalCriteria) {
final org.spongepowered.api.advancement.criteria.CriterionProgress progress = this.progress.get(criterion).get();
if (!progress.achieved()) {
instant = progress.grant();
}
}
this.score = score;
return Optional.of(instant == null ? Instant.now() : instant);
}
for (AdvancementCriterion criterion : this.criterion.internalCriteria) {
final org.spongepowered.api.advancement.criteria.CriterionProgress progress = this.progress.get(criterion).get();
// We don't have enough score, grant more criteria
if (lastScore < score && !progress.achieved()) {
progress.grant();
lastScore++;
// We have too much score, revoke more criteria
} else if (lastScore > score && progress.achieved()) {
progress.revoke();
lastScore--;
}
// We reached the target score
if (lastScore == score) {
break;
}
}
this.score = score;
SpongeScoreCriterion.BYPASS_EVENT = false;
return Optional.empty();
}
use of org.spongepowered.api.advancement.Advancement in project LanternServer by LanternPowered.
the class LanternPlayerAdvancements method getUnlockedAdvancementTrees.
public Collection<AdvancementTree> getUnlockedAdvancementTrees() {
final ImmutableList.Builder<AdvancementTree> builder = ImmutableList.builder();
for (AdvancementTree tree : AdvancementTreeRegistryModule.get().getAll()) {
final Advancement advancement = tree.getRootAdvancement();
final LanternAdvancementProgress progress = get(advancement);
if ((!progress.dirtyVisibility && progress.visible) || (progress.dirtyVisibility && shouldBeVisible(advancement))) {
builder.add(tree);
}
}
return builder.build();
}
use of org.spongepowered.api.advancement.Advancement in project LanternServer by LanternPowered.
the class LanternScoreCriterionProgress method set.
public Optional<Instant> set(int score, Runnable invalidator) {
checkArgument(score >= 0, "Score to set may not be negative");
checkArgument(score <= getGoal(), "Score to set may not be greater than the goal");
if (score == this.score) {
return get();
}
final Cause cause = CauseStack.current().getCurrentCause();
final Advancement advancement = this.progress.getAdvancement();
final LanternPlayer player = this.progress.getPlayer();
CriterionEvent.Score.Change event;
if (score == getGoal()) {
event = SpongeEventFactory.createCriterionEventScoreGrant(cause, advancement, this.criterion, player, Instant.now(), score, this.score);
} else if (this.score == getGoal()) {
event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, advancement, this.criterion, player, score, this.score);
} else {
event = SpongeEventFactory.createCriterionEventScoreChange(cause, advancement, this.criterion, player, score, this.score);
}
Sponge.getEventManager().post(event);
if (event.isCancelled()) {
return get();
}
this.score = score;
if (event instanceof CriterionEvent.Grant) {
this.achievingTime = ((CriterionEvent.Grant) event).getTime();
detachTrigger();
} else if (event instanceof CriterionEvent.Revoke) {
this.achievingTime = null;
attachTrigger();
}
invalidator.run();
return Optional.ofNullable(this.achievingTime);
}
use of org.spongepowered.api.advancement.Advancement 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();
}
}
}
}
}
Aggregations