use of org.spongepowered.common.advancement.criterion.DefaultedAdvancementCriterion 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.advancement.criterion.DefaultedAdvancementCriterion in project SpongeCommon by SpongePowered.
the class SpongeCriterionUtil method collectCriteria.
private static List<List<String>> collectCriteria(final AdvancementCriterion criterion, final Map<String, Criterion> criteria) {
List<List<String>> requirements = new ArrayList<>();
if (criterion instanceof SpongeAndCriterion) {
((SpongeOperatorCriterion) criterion).criteria().forEach(c -> requirements.addAll(SpongeCriterionUtil.collectCriteria(c, criteria)));
} else if (criterion instanceof SpongeOrCriterion) {
// OR List of AND Criteria of OR Criteria
final List<List<List<String>>> andRequirementsList = ((SpongeOperatorCriterion) criterion).criteria().stream().map(c -> SpongeCriterionUtil.collectCriteria(c, criteria)).collect(Collectors.toList());
List<List<String>> finalList = new ArrayList<>();
// For every AND Criteria
for (List<List<String>> andRequirements : andRequirementsList) {
if (finalList.isEmpty()) {
// Just take the first one in as is
finalList.addAll(andRequirements);
} else {
List<List<String>> workingList = new ArrayList<>();
for (List<String> andRequirement : andRequirements) {
for (List<String> prevAndRequirement : finalList) {
// For every AND requirement and FOR every previous AND requirement
// combine their OR requirements
// !! this can get very big very quickly !!
// TODO limit requirement count?
final List<String> newAndRequirement = new ArrayList<>(prevAndRequirement);
newAndRequirement.addAll(andRequirement);
workingList.add(newAndRequirement);
}
}
finalList = workingList;
}
}
requirements.addAll(finalList);
} else if (criterion instanceof SpongeScoreCriterion) {
final SpongeScoreCriterion scoreCriterion = (SpongeScoreCriterion) criterion;
for (int i = 0; i < scoreCriterion.goal(); i++) {
final DefaultedAdvancementCriterion internalCriterion = scoreCriterion.internalCriteria.get(i);
criteria.put(internalCriterion.name(), ((Criterion) internalCriterion));
requirements.add(Collections.singletonList(internalCriterion.name()));
}
} else {
criteria.put(criterion.name(), (Criterion) criterion);
requirements.add(Collections.singletonList(criterion.name()));
}
return requirements;
}
Aggregations