Skip to content

Commit

Permalink
Fix #608, advancements based on amount of media spent no longer trigg…
Browse files Browse the repository at this point in the history
…ering.
  • Loading branch information
Talia-12 committed Feb 7, 2024
1 parent 0843add commit ce18829
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package at.petrak.hexcasting.api.advancements;

import com.google.gson.JsonElement;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.BuiltInExceptionProvider;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.advancements.critereon.MinMaxBounds;
import net.minecraft.util.GsonHelper;

import javax.annotation.Nullable;
import java.util.Objects;
import java.util.function.Function;

public class MinMaxLongs extends MinMaxBounds<Long> {
public static final MinMaxLongs ANY = new MinMaxLongs(null, null);
@Nullable
private final Long minSq;
@Nullable
private final Long maxSq;

private static MinMaxLongs create(StringReader reader, @Nullable Long min, @Nullable Long max) throws CommandSyntaxException {
if (min != null && max != null && min > max) {
throw ERROR_SWAPPED.createWithContext(reader);
} else {
return new MinMaxLongs(min, max);
}
}

@Nullable
private static Long squareOpt(@Nullable Long l) {
return l == null ? null : l * l;
}

private MinMaxLongs(@Nullable Long min, @Nullable Long max) {
super(min, max);
this.minSq = squareOpt(min);
this.maxSq = squareOpt(max);
}

public static MinMaxLongs exactly(long l) {
return new MinMaxLongs(l, l);
}

public static MinMaxLongs between(long min, long max) {
return new MinMaxLongs(min, max);
}

public static MinMaxLongs atLeast(long min) {
return new MinMaxLongs(min, null);
}

public static MinMaxLongs atMost(long max) {
return new MinMaxLongs(null, max);
}

public boolean matches(long l) {
if (this.min != null && this.min > l) {
return false;
} else {
return this.max == null || this.max >= l;
}
}

public boolean matchesSqr(long l) {
if (this.minSq != null && this.minSq > l) {
return false;
} else {
return this.maxSq == null || this.maxSq >= l;
}
}

public static MinMaxLongs fromJson(@Nullable JsonElement json) {
return fromJson(json, ANY, GsonHelper::convertToLong, MinMaxLongs::new);
}

public static MinMaxLongs fromReader(StringReader reader) throws CommandSyntaxException {
return fromReader(reader, (l) -> l);
}

public static MinMaxLongs fromReader(StringReader reader, Function<Long, Long> map) throws CommandSyntaxException {
BuiltInExceptionProvider builtInExceptions = CommandSyntaxException.BUILT_IN_EXCEPTIONS;
Objects.requireNonNull(builtInExceptions);
return fromReader(reader, MinMaxLongs::create, Long::parseLong, builtInExceptions::readerInvalidInt, map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ public ResourceLocation getId() {
protected Instance createInstance(JsonObject json, ContextAwarePredicate predicate,
DeserializationContext context) {
return new Instance(predicate,
MinMaxBounds.Ints.fromJson(json.get(TAG_MEDIA_SPENT)),
MinMaxBounds.Ints.fromJson(json.get(TAG_MEDIA_WASTED)));
MinMaxLongs.fromJson(json.get(TAG_MEDIA_SPENT)),
MinMaxLongs.fromJson(json.get(TAG_MEDIA_WASTED)));
}

public void trigger(ServerPlayer player, int mediaSpent, int mediaWasted) {
public void trigger(ServerPlayer player, long mediaSpent, long mediaWasted) {
super.trigger(player, inst -> inst.test(mediaSpent, mediaWasted));
}

public static class Instance extends AbstractCriterionTriggerInstance {
protected final MinMaxBounds.Ints mediaSpent;
protected final MinMaxBounds.Ints mediaWasted;
protected final MinMaxLongs mediaSpent;
protected final MinMaxLongs mediaWasted;

public Instance(ContextAwarePredicate predicate, MinMaxBounds.Ints mediaSpent,
MinMaxBounds.Ints mediaWasted) {
public Instance(ContextAwarePredicate predicate, MinMaxLongs mediaSpent,
MinMaxLongs mediaWasted) {
super(SpendMediaTrigger.ID, predicate);
this.mediaSpent = mediaSpent;
this.mediaWasted = mediaWasted;
Expand All @@ -56,7 +56,7 @@ public JsonObject serializeToJson(SerializationContext ctx) {
return json;
}

private boolean test(int mediaSpentIn, int mediaWastedIn) {
private boolean test(long mediaSpentIn, long mediaWastedIn) {
return this.mediaSpent.matches(mediaSpentIn) && this.mediaWasted.matches(mediaWastedIn);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ protected long extractMediaFromInventory(long costLeft, boolean allowOvercast) {
}

this.caster.awardStat(HexStatistics.MEDIA_USED, (int) (startCost - costLeft));
HexAdvancementTriggers.SPEND_MEDIA_TRIGGER.trigger(
this.caster,
startCost - costLeft,
costLeft < 0 ? -costLeft : 0
);

return costLeft;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import at.petrak.hexcasting.api.HexAPI;
import at.petrak.hexcasting.api.advancements.FailToCastGreatSpellTrigger;
import at.petrak.hexcasting.api.advancements.MinMaxLongs;
import at.petrak.hexcasting.api.advancements.OvercastTrigger;
import at.petrak.hexcasting.api.advancements.SpendMediaTrigger;
import at.petrak.hexcasting.api.misc.MediaConstants;
Expand Down Expand Up @@ -57,15 +58,15 @@ public void generate(HolderLookup.Provider provider, Consumer<Advancement> consu
.display(simpleDisplay(Items.GLISTERING_MELON_SLICE, "wasteful_cast", FrameType.TASK))
.parent(root)
.addCriterion("waste_amt", new SpendMediaTrigger.Instance(ContextAwarePredicate.ANY,
MinMaxBounds.Ints.ANY,
MinMaxBounds.Ints.atLeast((int) (89 * MediaConstants.DUST_UNIT / 10))))
MinMaxLongs.ANY,
MinMaxLongs.atLeast(89 * MediaConstants.DUST_UNIT / 10)))
.save(consumer, prefix("aaa_wasteful_cast"));
Advancement.Builder.advancement()
.display(simpleDisplay(HexItems.CHARGED_AMETHYST, "big_cast", FrameType.TASK))
.parent(root)
.addCriterion("cast_amt", new SpendMediaTrigger.Instance(ContextAwarePredicate.ANY,
MinMaxBounds.Ints.atLeast((int) (64 * MediaConstants.CRYSTAL_UNIT)),
MinMaxBounds.Ints.ANY))
MinMaxLongs.atLeast(64 * MediaConstants.CRYSTAL_UNIT),
MinMaxLongs.ANY))
.save(consumer, prefix("aab_big_cast"));

var impotence = Advancement.Builder.advancement()
Expand Down

0 comments on commit ce18829

Please sign in to comment.