use of org.spongepowered.api.advancement.criteria.AdvancementCriterion in project SpongeCommon by SpongePowered.
the class SpongeCriterionHelper method build.
static AdvancementCriterion build(Class<? extends OperatorCriterion> type, Function<Set<AdvancementCriterion>, AdvancementCriterion> function, AdvancementCriterion criterion, Iterable<AdvancementCriterion> criteria) {
checkNotNull(criteria, "criteria");
final List<AdvancementCriterion> builder = new ArrayList<>();
build(type, criterion, builder);
for (AdvancementCriterion criterion1 : criteria) {
build(type, criterion1, builder);
}
return builder.isEmpty() ? SpongeEmptyCriterion.INSTANCE : builder.size() == 1 ? builder.get(0) : function.apply(ImmutableSet.copyOf(builder));
}
use of org.spongepowered.api.advancement.criteria.AdvancementCriterion in project SpongeCommon by SpongePowered.
the class SpongeOperatorCriterionProgress method grant.
@Override
public Instant grant() {
Instant time = null;
for (AdvancementCriterion criterion : this.criterion.getCriteria()) {
final Instant time1 = this.progress.get(criterion).get().grant();
if (time == null || time1.isAfter(time)) {
time = time1;
}
}
// Should be impossible
checkNotNull(time);
return time;
}
use of org.spongepowered.api.advancement.criteria.AdvancementCriterion 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.criteria.AdvancementCriterion in project SpongeCommon by SpongePowered.
the class MixinAdvancementProgress method revokeCriterion.
/**
* @author Cybermaxke
* @reason Rewrite the method to add support for triggering
* score criteria and calling revoke events.
*/
@Overwrite
public boolean revokeCriterion(String criterionIn) {
final net.minecraft.advancements.CriterionProgress criterionProgress = this.criteria.get(criterionIn);
if (criterionProgress == null || !criterionProgress.isObtained()) {
return false;
}
if (SpongeScoreCriterion.BYPASS_EVENT) {
criterionProgress.reset();
return true;
}
final Cause cause = SpongeImpl.getCauseStackManager().getCurrentCause();
final Player player = ((IMixinPlayerAdvancements) this.playerAdvancements).getPlayer();
final CriterionProgress progress = (CriterionProgress) criterionProgress;
final AdvancementCriterion criterion = progress.getCriterion();
final IMixinCriterion mixinCriterion = (IMixinCriterion) criterion;
// The score criterion needs special care
final SpongeScoreCriterion scoreCriterion = mixinCriterion.getScoreCriterion();
CriterionEvent event;
if (scoreCriterion != null) {
final SpongeScoreCriterionProgress scoreProgress = (SpongeScoreCriterionProgress) get(scoreCriterion).get();
final int lastScore = scoreProgress.getScore();
final int score = lastScore + 1;
if (lastScore == scoreCriterion.getGoal()) {
event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, getAdvancement(), scoreCriterion, player, lastScore, score);
} else if (score == scoreCriterion.getGoal()) {
event = SpongeEventFactory.createCriterionEventScoreGrant(cause, getAdvancement(), scoreCriterion, player, Instant.now(), lastScore, score);
} else {
event = SpongeEventFactory.createCriterionEventScoreChange(cause, getAdvancement(), scoreCriterion, player, lastScore, score);
}
} else {
event = SpongeEventFactory.createCriterionEventRevoke(cause, getAdvancement(), criterion, player);
}
if (SpongeImpl.postEvent(event)) {
return false;
}
criterionProgress.reset();
return true;
}
use of org.spongepowered.api.advancement.criteria.AdvancementCriterion in project SpongeCommon by SpongePowered.
the class MixinAdvancementProgress method grantCriterion.
/**
* @author Cybermaxke
* @reason Rewrite the method to add support for triggering
* score criteria and calling grant events.
*/
@Overwrite
public boolean grantCriterion(String criterionIn) {
final net.minecraft.advancements.CriterionProgress criterionProgress = this.criteria.get(criterionIn);
if (criterionProgress == null || criterionProgress.isObtained()) {
return false;
}
if (SpongeScoreCriterion.BYPASS_EVENT) {
criterionProgress.obtain();
return true;
}
final Cause cause = SpongeImpl.getCauseStackManager().getCurrentCause();
final Player player = ((IMixinPlayerAdvancements) this.playerAdvancements).getPlayer();
final CriterionProgress progress = (CriterionProgress) criterionProgress;
final AdvancementCriterion criterion = progress.getCriterion();
final IMixinCriterion mixinCriterion = (IMixinCriterion) criterion;
// The score criterion needs special care
final SpongeScoreCriterion scoreCriterion = mixinCriterion.getScoreCriterion();
CriterionEvent event;
if (scoreCriterion != null) {
final SpongeScoreCriterionProgress scoreProgress = (SpongeScoreCriterionProgress) get(scoreCriterion).get();
final int lastScore = scoreProgress.getScore();
final int score = lastScore + 1;
if (lastScore == scoreCriterion.getGoal()) {
event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, getAdvancement(), scoreCriterion, player, lastScore, score);
} else if (score == scoreCriterion.getGoal()) {
event = SpongeEventFactory.createCriterionEventScoreGrant(cause, getAdvancement(), scoreCriterion, player, Instant.now(), lastScore, score);
} else {
event = SpongeEventFactory.createCriterionEventScoreChange(cause, getAdvancement(), scoreCriterion, player, lastScore, score);
}
} else {
event = SpongeEventFactory.createCriterionEventGrant(cause, getAdvancement(), criterion, player, Instant.now());
}
if (SpongeImpl.postEvent(event)) {
return false;
}
criterionProgress.obtain();
return true;
}
Aggregations