use of org.spongepowered.api.advancement.criteria.AdvancementCriterion 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;
}
use of org.spongepowered.api.advancement.criteria.AdvancementCriterion 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.api.advancement.criteria.AdvancementCriterion 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;
}
use of org.spongepowered.api.advancement.criteria.AdvancementCriterion in project SpongeCommon by SpongePowered.
the class SpongeCriterionUtil method build.
public static AdvancementCriterion build(final Class<? extends OperatorCriterion> type, final Function<Set<AdvancementCriterion>, AdvancementCriterion> function, final AdvancementCriterion criterion, final Iterable<AdvancementCriterion> criteria) {
checkNotNull(criteria, "criteria");
final List<AdvancementCriterion> builder = new ArrayList<>();
SpongeCriterionUtil.build(type, criterion, builder);
for (final AdvancementCriterion criterion1 : criteria) {
SpongeCriterionUtil.build(type, criterion1, builder);
}
return builder.isEmpty() ? new SpongeEmptyCriterion() : 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 AdvancementTest method onAdvancementRegistry.
@Listener
public void onAdvancementRegistry(final RegisterDataPackValueEvent<Advancement> event) {
if (!this.enabled) {
return;
}
this.rootAdvancement = Advancement.builder().criterion(AdvancementCriterion.dummy()).displayInfo(DisplayInfo.builder().icon(ItemTypes.COMMAND_BLOCK).title(Component.text("Advancement Tests")).description(Component.text("Dummy trigger. Granted manually after testplugin is enabled")).build()).root().background(ResourceKey.minecraft("textures/gui/advancements/backgrounds/stone.png")).key(ResourceKey.of(this.plugin, "root")).build();
event.register(this.rootAdvancement);
final AdvancementCriterion someDirtCriterion = AdvancementCriterion.builder().trigger(FilteredTrigger.builder().type(this.inventoryChangeTrigger).config(new InventoryChangeTriggerConfig(ItemStack.of(ItemTypes.DIRT))).build()).name("some_dirt").build();
final Advancement someDirt = Advancement.builder().criterion(someDirtCriterion).displayInfo(DisplayInfo.builder().icon(ItemTypes.DIRT).title(Component.text("Got dirt!")).type(AdvancementTypes.TASK).build()).parent(this.rootAdvancement).key(ResourceKey.of(this.plugin, "some_dirt")).build();
event.register(someDirt);
final AdvancementCriterion lotsOfDirtCriterion = AdvancementCriterion.builder().trigger(FilteredTrigger.builder().type(this.inventoryChangeTrigger).config(new InventoryChangeTriggerConfig(ItemStack.of(ItemTypes.DIRT, 64))).build()).name("lots_of_dirt").build();
final Advancement lotsOfDirt = Advancement.builder().criterion(lotsOfDirtCriterion).displayInfo(DisplayInfo.builder().icon(ItemTypes.DIRT).title(Component.text("Got more dirt!")).type(AdvancementTypes.GOAL).build()).parent(someDirt).key(ResourceKey.of(this.plugin, "lots_of_dirt")).build();
event.register(lotsOfDirt);
final AdvancementCriterion tonsOfDirtCriterion = AdvancementCriterion.builder().trigger(FilteredTrigger.builder().type(this.inventoryChangeTrigger).config(new InventoryChangeTriggerConfig(ItemStack.of(ItemTypes.DIRT, 64 * 9))).build()).name("tons_of_dirt").build();
final Advancement tonsOfDirt = Advancement.builder().criterion(tonsOfDirtCriterion).displayInfo(DisplayInfo.builder().icon(ItemTypes.DIRT).title(Component.text("Got tons of dirt!")).type(AdvancementTypes.CHALLENGE).hidden(true).build()).parent(lotsOfDirt).key(ResourceKey.of(this.plugin, "tons_of_dirt")).build();
event.register(tonsOfDirt);
this.counter1 = ScoreAdvancementCriterion.builder().goal(10).name("counter").build();
this.counter1Bypass = AdvancementCriterion.dummy();
this.counterAdvancement1 = Advancement.builder().criterion(OrCriterion.of(this.counter1, this.counter1Bypass)).displayInfo(DisplayInfo.builder().icon(ItemTypes.CHEST).title(Component.text("Open some chests.")).type(AdvancementTypes.GOAL).build()).parent(this.rootAdvancement).key(ResourceKey.of(this.plugin, "counting")).build();
event.register(this.counterAdvancement1);
this.counter2 = ScoreAdvancementCriterion.builder().goal(20).name("counter").build();
this.counterAdvancement2 = Advancement.builder().criterion(this.counter2).displayInfo(DisplayInfo.builder().icon(ItemTypes.CHEST).title(Component.text("Open more chests")).type(AdvancementTypes.CHALLENGE).build()).parent(this.counterAdvancement1).key(ResourceKey.of(this.plugin, "counting_more")).build();
event.register(this.counterAdvancement2);
final AdvancementCriterion a = AdvancementCriterion.builder().name("A").build();
final AdvancementCriterion b = AdvancementCriterion.builder().name("B").build();
final AdvancementCriterion c = AdvancementCriterion.builder().name("C").build();
final AdvancementCriterion d = AdvancementCriterion.builder().name("D").build();
final AdvancementCriterion e = AdvancementCriterion.builder().name("E").build();
final AdvancementCriterion f = AdvancementCriterion.builder().name("F").build();
final AdvancementCriterion combinationCriterion = OrCriterion.of(a, AndCriterion.of(b, OrCriterion.of(c, d)), AndCriterion.of(e, f));
final Advancement combinationAdvancement = Advancement.builder().criterion(combinationCriterion).displayInfo(DisplayInfo.builder().icon(ItemTypes.CHEST).title(Component.text("A || (B & (C || D)) || (E & F)")).description(Component.text("ABE ABF ACDE ACDF")).type(AdvancementTypes.CHALLENGE).build()).parent(this.counterAdvancement1).key(ResourceKey.of(this.plugin, "combination")).build();
event.register(combinationAdvancement);
}
Aggregations