use of org.spongepowered.api.advancement.criteria.AdvancementCriterion 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.criteria.AdvancementCriterion in project SpongeCommon by SpongePowered.
the class SpongeScoreCriterionProgress method set.
@Override
public Optional<Instant> set(final int score) {
checkState(score >= 0 && score <= this.goal(), "Score cannot be negative or greater than the goal.");
int lastScore = this.score();
if (lastScore == score) {
return this.get();
}
final CriterionEvent.Score.Change event;
final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
final Advancement advancement = this.progress.advancement();
final ServerPlayer player = ((PlayerAdvancementsBridge) ((AdvancementProgressBridge) this.progress).bridge$getPlayerAdvancements()).bridge$getPlayer();
if (lastScore == this.goal()) {
event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, advancement, this.criterion(), player, lastScore, score);
} else if (score == this.goal()) {
event = SpongeEventFactory.createCriterionEventScoreGrant(cause, advancement, this.criterion(), player, Instant.now(), lastScore, score);
} else {
event = SpongeEventFactory.createCriterionEventScoreChange(cause, advancement, this.criterion(), player, lastScore, score);
}
if (SpongeCommon.post(event)) {
return this.get();
}
SpongeScoreCriterion.BYPASS_EVENT = true;
// This is the only case a instant will be returned
if (score == this.goal()) {
Instant instant = null;
for (final 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 (final 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 SpongeCriterionTrigger method bridge$trigger.
@Override
public void bridge$trigger(final ServerPlayer player) {
final PlayerAdvancements playerAdvancements = ((net.minecraft.server.level.ServerPlayer) player).getAdvancements();
final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
// correct type verified in builder
@SuppressWarnings("unchecked") final TypeToken<FilteredTriggerConfiguration> typeToken = (TypeToken<FilteredTriggerConfiguration>) TypeToken.get(this.triggerConfigurationType);
for (final Listener listener : new ArrayList<>(this.listeners.get(playerAdvancements))) {
final CriterionTrigger_ListenerAccessor mixinListener = (CriterionTrigger_ListenerAccessor) listener;
final Advancement advancement = (Advancement) mixinListener.accessor$advancement();
final AdvancementCriterion advancementCriterion = (AdvancementCriterion) ((net.minecraft.advancements.Advancement) advancement).getCriteria().get(mixinListener.accessor$criterion());
final CriterionEvent.Trigger event = SpongeEventFactory.createCriterionEventTrigger(cause, advancement, advancementCriterion, typeToken, player, (FilteredTrigger) listener.getTriggerInstance(), this.eventHandler == null);
if (this.eventHandler != null) {
this.eventHandler.accept(event);
if (!event.result()) {
continue;
}
}
SpongeCommon.post(event);
if (event.result()) {
listener.run(playerAdvancements);
}
}
}
use of org.spongepowered.api.advancement.criteria.AdvancementCriterion in project SpongeCommon by SpongePowered.
the class SpongeCriterionBuilder method build0.
@SuppressWarnings("ConstantConditions")
@Override
AdvancementCriterion build0() {
CriterionTriggerInstance trigger = (CriterionTriggerInstance) this.trigger;
if (this.trigger == null) {
trigger = SpongeDummyTrigger.Instance.dummy();
}
final Criterion criterion = new Criterion(trigger);
((CriterionBridge) criterion).bridge$setName(this.name);
return (AdvancementCriterion) criterion;
}
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 (final AdvancementCriterion criterion : this.criterion.criteria()) {
final Instant time1 = this.progress.get(criterion).get().grant();
if (time == null || time1.isAfter(time)) {
time = time1;
}
}
// Should be impossible
checkNotNull(time);
return time;
}
Aggregations