use of org.spongepowered.common.bridge.advancements.CriterionBridge in project SpongeCommon by SpongePowered.
the class AdvancementMixin method impl$setUpSpongeFields.
@SuppressWarnings({ "ConstantConditions" })
@Inject(method = "<init>", at = @At("RETURN"))
private void impl$setUpSpongeFields(ResourceLocation location, @Nullable Advancement parent, @Nullable DisplayInfo displayInfo, AdvancementRewards rewards, Map<String, Criterion> criteria, String[][] requirements, CallbackInfo ci) {
// Don't do anything on the client, unless we're performing registry initialization
if (!PlatformHooks.INSTANCE.getGeneralHooks().onServerThread()) {
return;
}
if (displayInfo != null) {
((DisplayInfoBridge) displayInfo).bridge$setAdvancement((org.spongepowered.api.advancement.Advancement) this);
}
this.impl$toastText = this.impl$generateToastText();
final Map<String, DefaultedAdvancementCriterion> criteriaMap = new LinkedHashMap<>();
final Map<String, List<DefaultedAdvancementCriterion>> scoreCriteria = new HashMap<>();
for (Map.Entry<String, Criterion> entry : criteria.entrySet()) {
final CriterionBridge mixinCriterion = (CriterionBridge) entry.getValue();
final String groupName = mixinCriterion.bridge$getScoreCriterionName();
if (groupName != null) {
scoreCriteria.computeIfAbsent(groupName, k -> new ArrayList<>()).add((DefaultedAdvancementCriterion) entry.getValue());
}
criteriaMap.put(entry.getKey(), (DefaultedAdvancementCriterion) mixinCriterion);
mixinCriterion.bridge$setName(entry.getKey());
}
for (Map.Entry<String, List<DefaultedAdvancementCriterion>> groupEntry : scoreCriteria.entrySet()) {
criteriaMap.put(groupEntry.getKey(), new SpongeScoreCriterion(groupEntry.getKey(), groupEntry.getValue()));
groupEntry.getValue().forEach(c -> criteriaMap.remove(c.name()));
}
final Set<AdvancementCriterion> andCriteria = new HashSet<>();
for (final String[] array : requirements) {
final Set<AdvancementCriterion> orCriteria = new HashSet<>();
for (final String name : array) {
DefaultedAdvancementCriterion criterion = criteriaMap.get(name);
if (criterion == null && criteria.get(name) != null) {
// internal removed by scoreCriterion
criterion = criteriaMap.get(((CriterionBridge) criteria.get(name)).bridge$getScoreCriterionName());
}
orCriteria.add(criterion);
}
andCriteria.add(OrCriterion.of(orCriteria));
}
this.impl$criterion = AndCriterion.of(andCriteria);
}
use of org.spongepowered.common.bridge.advancements.CriterionBridge in project SpongeCommon by SpongePowered.
the class AdvancementProgressMixin method impl$grantCriterion.
private boolean impl$grantCriterion(String rawCriterion) {
final net.minecraft.advancements.CriterionProgress criterionProgress = this.criteria.get(rawCriterion);
if (criterionProgress == null || criterionProgress.isDone()) {
return false;
}
if (SpongeScoreCriterion.BYPASS_EVENT) {
criterionProgress.grant();
return true;
}
final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
final ServerPlayer player = ((PlayerAdvancementsBridge) this.impl$playerAdvancements).bridge$getPlayer();
final CriterionProgress progress = (CriterionProgress) criterionProgress;
final AdvancementCriterion criterion = progress.criterion();
final CriterionBridge criterionBridge = (CriterionBridge) criterion;
// The score criterion needs special care
final SpongeScoreCriterion scoreCriterion = criterionBridge.bridge$getScoreCriterion();
final CriterionEvent event;
if (scoreCriterion != null) {
final SpongeScoreCriterionProgress scoreProgress = (SpongeScoreCriterionProgress) this.impl$progressMap.get(scoreCriterion.name());
final int lastScore = scoreProgress.score();
final int score = lastScore + 1;
if (lastScore == scoreCriterion.goal()) {
event = SpongeEventFactory.createCriterionEventScoreRevoke(cause, this.bridge$getAdvancement(), scoreCriterion, player, lastScore, score);
} else if (score == scoreCriterion.goal()) {
event = SpongeEventFactory.createCriterionEventScoreGrant(cause, this.bridge$getAdvancement(), scoreCriterion, player, Instant.now(), lastScore, score);
} else {
event = SpongeEventFactory.createCriterionEventScoreChange(cause, this.bridge$getAdvancement(), scoreCriterion, player, lastScore, score);
}
} else {
event = SpongeEventFactory.createCriterionEventGrant(cause, this.bridge$getAdvancement(), criterion, player, Instant.now());
}
if (SpongeCommon.post(event)) {
return false;
}
criterionProgress.grant();
return true;
}
Aggregations