From ce18829d2a0336a9edd1fc62b227f58f713a3077 Mon Sep 17 00:00:00 2001 From: Talia-12 Date: Tue, 6 Feb 2024 21:24:41 +1000 Subject: [PATCH] Fix #608, advancements based on amount of media spent no longer triggering. --- .../api/advancements/MinMaxLongs.java | 85 +++++++++++++++++++ .../api/advancements/SpendMediaTrigger.java | 16 ++-- .../casting/eval/env/PlayerBasedCastEnv.java | 5 ++ .../hexcasting/datagen/HexAdvancements.java | 9 +- 4 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 Common/src/main/java/at/petrak/hexcasting/api/advancements/MinMaxLongs.java diff --git a/Common/src/main/java/at/petrak/hexcasting/api/advancements/MinMaxLongs.java b/Common/src/main/java/at/petrak/hexcasting/api/advancements/MinMaxLongs.java new file mode 100644 index 000000000..0be502c86 --- /dev/null +++ b/Common/src/main/java/at/petrak/hexcasting/api/advancements/MinMaxLongs.java @@ -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 { + 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 map) throws CommandSyntaxException { + BuiltInExceptionProvider builtInExceptions = CommandSyntaxException.BUILT_IN_EXCEPTIONS; + Objects.requireNonNull(builtInExceptions); + return fromReader(reader, MinMaxLongs::create, Long::parseLong, builtInExceptions::readerInvalidInt, map); + } +} diff --git a/Common/src/main/java/at/petrak/hexcasting/api/advancements/SpendMediaTrigger.java b/Common/src/main/java/at/petrak/hexcasting/api/advancements/SpendMediaTrigger.java index aa12d472c..d610807f5 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/advancements/SpendMediaTrigger.java +++ b/Common/src/main/java/at/petrak/hexcasting/api/advancements/SpendMediaTrigger.java @@ -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; @@ -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); } } diff --git a/Common/src/main/java/at/petrak/hexcasting/api/casting/eval/env/PlayerBasedCastEnv.java b/Common/src/main/java/at/petrak/hexcasting/api/casting/eval/env/PlayerBasedCastEnv.java index 9932ba7b3..c13793007 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/casting/eval/env/PlayerBasedCastEnv.java +++ b/Common/src/main/java/at/petrak/hexcasting/api/casting/eval/env/PlayerBasedCastEnv.java @@ -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; } diff --git a/Common/src/main/java/at/petrak/hexcasting/datagen/HexAdvancements.java b/Common/src/main/java/at/petrak/hexcasting/datagen/HexAdvancements.java index 19956a7db..28012e0da 100644 --- a/Common/src/main/java/at/petrak/hexcasting/datagen/HexAdvancements.java +++ b/Common/src/main/java/at/petrak/hexcasting/datagen/HexAdvancements.java @@ -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; @@ -57,15 +58,15 @@ public void generate(HolderLookup.Provider provider, Consumer 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()