diff --git a/common-1.16.5/src/main/java/agency/highlysuspect/apathy/Apathy116.java b/common-1.16.5/src/main/java/agency/highlysuspect/apathy/Apathy116.java index 30420f7..bc74738 100644 --- a/common-1.16.5/src/main/java/agency/highlysuspect/apathy/Apathy116.java +++ b/common-1.16.5/src/main/java/agency/highlysuspect/apathy/Apathy116.java @@ -23,6 +23,7 @@ import agency.highlysuspect.apathy.core.wrapper.DragonDuck; import agency.highlysuspect.apathy.rule.PartialSpecDefenderHasAdvancement; import agency.highlysuspect.apathy.rule.PartialSpecDefenderInPlayerSet; +import agency.highlysuspect.apathy.rule.PartialSpecEffect; import agency.highlysuspect.apathy.rule.PartialSpecLocation; import agency.highlysuspect.apathy.rule.PartialSpecScore; import agency.highlysuspect.apathy.rule.PartialSpecScoreboardTeam; @@ -172,6 +173,7 @@ public void addRules() { super.addRules(); partialSerializers.register("advancements", PartialSpecDefenderHasAdvancement.Serializer.INSTANCE); + partialSerializers.register("effect", PartialSpecEffect.Serializer.INSTANCE); partialSerializers.register("in_player_set", PartialSpecDefenderInPlayerSet.Serializer.INSTANCE); partialSerializers.register("location", PartialSpecLocation.Serializer.INSTANCE); partialSerializers.register("score", PartialSpecScore.Serializer.INSTANCE); diff --git a/common-1.16.5/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java b/common-1.16.5/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java index a407cea..be99337 100644 --- a/common-1.16.5/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java +++ b/common-1.16.5/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java @@ -64,7 +64,7 @@ public void write(PartialSpecDefenderHasAdvancement thing, JsonObject json) { @Override public PartialSpecDefenderHasAdvancement read(JsonObject json) { - return new PartialSpecDefenderHasAdvancement(StreamSupport.stream(json.getAsJsonArray("advancements").spliterator(), false) + return new PartialSpecDefenderHasAdvancement(CoolGsonHelper.streamArray(json.getAsJsonArray("advancements")) .map(JsonElement::getAsString) .map(ResourceLocation::new) .collect(Collectors.toSet())); diff --git a/common-1.16.5/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecEffect.java b/common-1.16.5/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecEffect.java new file mode 100644 index 0000000..a012a76 --- /dev/null +++ b/common-1.16.5/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecEffect.java @@ -0,0 +1,93 @@ +package agency.highlysuspect.apathy.rule; + +import agency.highlysuspect.apathy.VerConv; +import agency.highlysuspect.apathy.core.Apathy; +import agency.highlysuspect.apathy.core.rule.CoolGsonHelper; +import agency.highlysuspect.apathy.core.rule.JsonSerializer; +import agency.highlysuspect.apathy.core.rule.Partial; +import agency.highlysuspect.apathy.core.rule.PartialSpecAlways; +import agency.highlysuspect.apathy.core.rule.Spec; +import agency.highlysuspect.apathy.core.rule.Who; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.entity.LivingEntity; + +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class PartialSpecEffect implements Spec { + public PartialSpecEffect(Set mobEffects, Who who) { + this.mobEffects = mobEffects; + this.who = who; + } + + private final Set mobEffects; + private final Who who; + + @Override + public Spec optimize() { + if(mobEffects.isEmpty()) return PartialSpecAlways.FALSE; + else return this; + } + + @Override + public Partial build() { + //i love useless microoptimizations + if(mobEffects.size() == 1) { + MobEffect theEffect = mobEffects.iterator().next(); + return (attacker, defender) -> { + LivingEntity which = who.choose(VerConv.mob(attacker), VerConv.player(defender)); + return which.hasEffect(theEffect); + }; + } else return (attacker, defender) -> { + LivingEntity which = who.choose(VerConv.mob(attacker), VerConv.player(defender)); + for(MobEffect effect : mobEffects) { + if(which.hasEffect(effect)) return true; + } + return false; + }; + } + + @Override + public JsonSerializer getSerializer() { + return Serializer.INSTANCE; + } + + public static class Serializer implements JsonSerializer { + public static final Serializer INSTANCE = new Serializer(); + + @Override + public void write(PartialSpecEffect thing, JsonObject json) { + json.add("effects", thing.mobEffects.stream() + .map(Registry.MOB_EFFECT::getKey) + .filter(Objects::nonNull) + .map(ResourceLocation::toString) + .map(JsonPrimitive::new) + .collect(CoolGsonHelper.toJsonArray())); + json.addProperty("who", thing.who.toString()); + } + + @Override + public PartialSpecEffect read(JsonObject json) { + Set mobEffects = CoolGsonHelper.streamArray(json.getAsJsonArray("effects")) + .map(JsonElement::getAsString) + .map(ResourceLocation::new) + .flatMap(rl -> { + MobEffect effect = Registry.MOB_EFFECT.get(rl); + if(effect == null) { + Apathy.instance.log.error("unknown mob effect: " + rl); + return Stream.of(); + } else return Stream.of(effect); + }) + .collect(Collectors.toSet()); + Who who = Who.fromString(json.get("who").getAsString()); + return new PartialSpecEffect(mobEffects, who); + } + } +} diff --git a/common-1.16.5/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecLocation.java b/common-1.16.5/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecLocation.java index 9a6774f..c38ab2e 100644 --- a/common-1.16.5/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecLocation.java +++ b/common-1.16.5/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecLocation.java @@ -11,13 +11,12 @@ import com.google.gson.JsonObject; import net.minecraft.advancements.critereon.LocationPredicate; import net.minecraft.server.level.ServerLevel; -import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import java.util.Locale; import java.util.Map; -@SuppressWarnings("ClassCanBeRecord") +//@SuppressWarnings("ClassCanBeRecord") public class PartialSpecLocation implements Spec { public PartialSpecLocation(LocationPredicate pred, LocationGetter who, String uniqueId, int offsetX, int offsetY, int offsetZ) { this.pred = pred; diff --git a/common-1.18.2/src/main/java/agency/highlysuspect/apathy/Apathy118.java b/common-1.18.2/src/main/java/agency/highlysuspect/apathy/Apathy118.java index 5645d3a..b86d31b 100644 --- a/common-1.18.2/src/main/java/agency/highlysuspect/apathy/Apathy118.java +++ b/common-1.18.2/src/main/java/agency/highlysuspect/apathy/Apathy118.java @@ -24,6 +24,7 @@ import agency.highlysuspect.apathy.core.wrapper.DragonDuck; import agency.highlysuspect.apathy.rule.PartialSpecDefenderHasAdvancement; import agency.highlysuspect.apathy.rule.PartialSpecDefenderInPlayerSet; +import agency.highlysuspect.apathy.rule.PartialSpecEffect; import agency.highlysuspect.apathy.rule.PartialSpecLocation; import agency.highlysuspect.apathy.rule.PartialSpecScore; import agency.highlysuspect.apathy.rule.PartialSpecScoreboardTeam; @@ -157,6 +158,7 @@ public void addRules() { super.addRules(); partialSerializers.register("advancements", PartialSpecDefenderHasAdvancement.Serializer.INSTANCE); + partialSerializers.register("effect", PartialSpecEffect.Serializer.INSTANCE); partialSerializers.register("in_player_set", PartialSpecDefenderInPlayerSet.Serializer.INSTANCE); partialSerializers.register("location", PartialSpecLocation.Serializer.INSTANCE); partialSerializers.register("score", PartialSpecScore.Serializer.INSTANCE); diff --git a/common-1.18.2/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java b/common-1.18.2/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java index a407cea..4e1d797 100644 --- a/common-1.18.2/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java +++ b/common-1.18.2/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java @@ -4,7 +4,6 @@ import agency.highlysuspect.apathy.core.rule.CoolGsonHelper; import agency.highlysuspect.apathy.core.rule.JsonSerializer; import agency.highlysuspect.apathy.core.rule.Partial; -import agency.highlysuspect.apathy.core.rule.PartialSpecAlways; import agency.highlysuspect.apathy.core.rule.Spec; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -64,7 +63,7 @@ public void write(PartialSpecDefenderHasAdvancement thing, JsonObject json) { @Override public PartialSpecDefenderHasAdvancement read(JsonObject json) { - return new PartialSpecDefenderHasAdvancement(StreamSupport.stream(json.getAsJsonArray("advancements").spliterator(), false) + return new PartialSpecDefenderHasAdvancement(CoolGsonHelper.streamArray(json.getAsJsonArray("advancements")) .map(JsonElement::getAsString) .map(ResourceLocation::new) .collect(Collectors.toSet())); diff --git a/common-1.18.2/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecEffect.java b/common-1.18.2/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecEffect.java new file mode 100644 index 0000000..a012a76 --- /dev/null +++ b/common-1.18.2/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecEffect.java @@ -0,0 +1,93 @@ +package agency.highlysuspect.apathy.rule; + +import agency.highlysuspect.apathy.VerConv; +import agency.highlysuspect.apathy.core.Apathy; +import agency.highlysuspect.apathy.core.rule.CoolGsonHelper; +import agency.highlysuspect.apathy.core.rule.JsonSerializer; +import agency.highlysuspect.apathy.core.rule.Partial; +import agency.highlysuspect.apathy.core.rule.PartialSpecAlways; +import agency.highlysuspect.apathy.core.rule.Spec; +import agency.highlysuspect.apathy.core.rule.Who; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.entity.LivingEntity; + +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class PartialSpecEffect implements Spec { + public PartialSpecEffect(Set mobEffects, Who who) { + this.mobEffects = mobEffects; + this.who = who; + } + + private final Set mobEffects; + private final Who who; + + @Override + public Spec optimize() { + if(mobEffects.isEmpty()) return PartialSpecAlways.FALSE; + else return this; + } + + @Override + public Partial build() { + //i love useless microoptimizations + if(mobEffects.size() == 1) { + MobEffect theEffect = mobEffects.iterator().next(); + return (attacker, defender) -> { + LivingEntity which = who.choose(VerConv.mob(attacker), VerConv.player(defender)); + return which.hasEffect(theEffect); + }; + } else return (attacker, defender) -> { + LivingEntity which = who.choose(VerConv.mob(attacker), VerConv.player(defender)); + for(MobEffect effect : mobEffects) { + if(which.hasEffect(effect)) return true; + } + return false; + }; + } + + @Override + public JsonSerializer getSerializer() { + return Serializer.INSTANCE; + } + + public static class Serializer implements JsonSerializer { + public static final Serializer INSTANCE = new Serializer(); + + @Override + public void write(PartialSpecEffect thing, JsonObject json) { + json.add("effects", thing.mobEffects.stream() + .map(Registry.MOB_EFFECT::getKey) + .filter(Objects::nonNull) + .map(ResourceLocation::toString) + .map(JsonPrimitive::new) + .collect(CoolGsonHelper.toJsonArray())); + json.addProperty("who", thing.who.toString()); + } + + @Override + public PartialSpecEffect read(JsonObject json) { + Set mobEffects = CoolGsonHelper.streamArray(json.getAsJsonArray("effects")) + .map(JsonElement::getAsString) + .map(ResourceLocation::new) + .flatMap(rl -> { + MobEffect effect = Registry.MOB_EFFECT.get(rl); + if(effect == null) { + Apathy.instance.log.error("unknown mob effect: " + rl); + return Stream.of(); + } else return Stream.of(effect); + }) + .collect(Collectors.toSet()); + Who who = Who.fromString(json.get("who").getAsString()); + return new PartialSpecEffect(mobEffects, who); + } + } +} diff --git a/common-1.19.2-begrudgingly/src/main/java/agency/highlysuspect/apathy/Apathy119.java b/common-1.19.2-begrudgingly/src/main/java/agency/highlysuspect/apathy/Apathy119.java index 1578a3e..8994452 100644 --- a/common-1.19.2-begrudgingly/src/main/java/agency/highlysuspect/apathy/Apathy119.java +++ b/common-1.19.2-begrudgingly/src/main/java/agency/highlysuspect/apathy/Apathy119.java @@ -24,6 +24,7 @@ import agency.highlysuspect.apathy.core.wrapper.DragonDuck; import agency.highlysuspect.apathy.rule.PartialSpecDefenderHasAdvancement; import agency.highlysuspect.apathy.rule.PartialSpecDefenderInPlayerSet; +import agency.highlysuspect.apathy.rule.PartialSpecEffect; import agency.highlysuspect.apathy.rule.PartialSpecLocation; import agency.highlysuspect.apathy.rule.PartialSpecScore; import agency.highlysuspect.apathy.rule.PartialSpecScoreboardTeam; @@ -173,6 +174,7 @@ public void addRules() { super.addRules(); partialSerializers.register("advancements", PartialSpecDefenderHasAdvancement.Serializer.INSTANCE); + partialSerializers.register("effect", PartialSpecEffect.Serializer.INSTANCE); partialSerializers.register("in_player_set", PartialSpecDefenderInPlayerSet.Serializer.INSTANCE); partialSerializers.register("location", PartialSpecLocation.Serializer.INSTANCE); partialSerializers.register("score", PartialSpecScore.Serializer.INSTANCE); diff --git a/common-1.19.2-begrudgingly/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java b/common-1.19.2-begrudgingly/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java index c9be4ca..2e09091 100644 --- a/common-1.19.2-begrudgingly/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java +++ b/common-1.19.2-begrudgingly/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java @@ -70,7 +70,7 @@ public void write(PartialSpecDefenderHasAdvancement thing, JsonObject json) { @Override public PartialSpecDefenderHasAdvancement read(JsonObject json) { - return new PartialSpecDefenderHasAdvancement(StreamSupport.stream(json.getAsJsonArray("advancements").spliterator(), false) + return new PartialSpecDefenderHasAdvancement(CoolGsonHelper.streamArray(json.getAsJsonArray("advancements")) .map(JsonElement::getAsString) .map(ResourceLocation::new) .collect(Collectors.toSet())); diff --git a/common-1.19.2-begrudgingly/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecEffect.java b/common-1.19.2-begrudgingly/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecEffect.java new file mode 100644 index 0000000..a012a76 --- /dev/null +++ b/common-1.19.2-begrudgingly/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecEffect.java @@ -0,0 +1,93 @@ +package agency.highlysuspect.apathy.rule; + +import agency.highlysuspect.apathy.VerConv; +import agency.highlysuspect.apathy.core.Apathy; +import agency.highlysuspect.apathy.core.rule.CoolGsonHelper; +import agency.highlysuspect.apathy.core.rule.JsonSerializer; +import agency.highlysuspect.apathy.core.rule.Partial; +import agency.highlysuspect.apathy.core.rule.PartialSpecAlways; +import agency.highlysuspect.apathy.core.rule.Spec; +import agency.highlysuspect.apathy.core.rule.Who; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.entity.LivingEntity; + +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class PartialSpecEffect implements Spec { + public PartialSpecEffect(Set mobEffects, Who who) { + this.mobEffects = mobEffects; + this.who = who; + } + + private final Set mobEffects; + private final Who who; + + @Override + public Spec optimize() { + if(mobEffects.isEmpty()) return PartialSpecAlways.FALSE; + else return this; + } + + @Override + public Partial build() { + //i love useless microoptimizations + if(mobEffects.size() == 1) { + MobEffect theEffect = mobEffects.iterator().next(); + return (attacker, defender) -> { + LivingEntity which = who.choose(VerConv.mob(attacker), VerConv.player(defender)); + return which.hasEffect(theEffect); + }; + } else return (attacker, defender) -> { + LivingEntity which = who.choose(VerConv.mob(attacker), VerConv.player(defender)); + for(MobEffect effect : mobEffects) { + if(which.hasEffect(effect)) return true; + } + return false; + }; + } + + @Override + public JsonSerializer getSerializer() { + return Serializer.INSTANCE; + } + + public static class Serializer implements JsonSerializer { + public static final Serializer INSTANCE = new Serializer(); + + @Override + public void write(PartialSpecEffect thing, JsonObject json) { + json.add("effects", thing.mobEffects.stream() + .map(Registry.MOB_EFFECT::getKey) + .filter(Objects::nonNull) + .map(ResourceLocation::toString) + .map(JsonPrimitive::new) + .collect(CoolGsonHelper.toJsonArray())); + json.addProperty("who", thing.who.toString()); + } + + @Override + public PartialSpecEffect read(JsonObject json) { + Set mobEffects = CoolGsonHelper.streamArray(json.getAsJsonArray("effects")) + .map(JsonElement::getAsString) + .map(ResourceLocation::new) + .flatMap(rl -> { + MobEffect effect = Registry.MOB_EFFECT.get(rl); + if(effect == null) { + Apathy.instance.log.error("unknown mob effect: " + rl); + return Stream.of(); + } else return Stream.of(effect); + }) + .collect(Collectors.toSet()); + Who who = Who.fromString(json.get("who").getAsString()); + return new PartialSpecEffect(mobEffects, who); + } + } +} diff --git a/common-1.19.4/src/main/java/agency/highlysuspect/apathy/Apathy119.java b/common-1.19.4/src/main/java/agency/highlysuspect/apathy/Apathy119.java index 11bab92..ca43867 100644 --- a/common-1.19.4/src/main/java/agency/highlysuspect/apathy/Apathy119.java +++ b/common-1.19.4/src/main/java/agency/highlysuspect/apathy/Apathy119.java @@ -24,6 +24,7 @@ import agency.highlysuspect.apathy.core.wrapper.DragonDuck; import agency.highlysuspect.apathy.rule.PartialSpecDefenderHasAdvancement; import agency.highlysuspect.apathy.rule.PartialSpecDefenderInPlayerSet; +import agency.highlysuspect.apathy.rule.PartialSpecEffect; import agency.highlysuspect.apathy.rule.PartialSpecLocation; import agency.highlysuspect.apathy.rule.PartialSpecScore; import agency.highlysuspect.apathy.rule.PartialSpecScoreboardTeam; @@ -173,6 +174,7 @@ public void addRules() { super.addRules(); partialSerializers.register("advancements", PartialSpecDefenderHasAdvancement.Serializer.INSTANCE); + partialSerializers.register("effect", PartialSpecEffect.Serializer.INSTANCE); partialSerializers.register("in_player_set", PartialSpecDefenderInPlayerSet.Serializer.INSTANCE); partialSerializers.register("location", PartialSpecLocation.Serializer.INSTANCE); partialSerializers.register("score", PartialSpecScore.Serializer.INSTANCE); diff --git a/common-1.19.4/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java b/common-1.19.4/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java index c9be4ca..2e09091 100644 --- a/common-1.19.4/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java +++ b/common-1.19.4/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecDefenderHasAdvancement.java @@ -70,7 +70,7 @@ public void write(PartialSpecDefenderHasAdvancement thing, JsonObject json) { @Override public PartialSpecDefenderHasAdvancement read(JsonObject json) { - return new PartialSpecDefenderHasAdvancement(StreamSupport.stream(json.getAsJsonArray("advancements").spliterator(), false) + return new PartialSpecDefenderHasAdvancement(CoolGsonHelper.streamArray(json.getAsJsonArray("advancements")) .map(JsonElement::getAsString) .map(ResourceLocation::new) .collect(Collectors.toSet())); diff --git a/common-1.19.4/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecEffect.java b/common-1.19.4/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecEffect.java new file mode 100644 index 0000000..f6c58d2 --- /dev/null +++ b/common-1.19.4/src/main/java/agency/highlysuspect/apathy/rule/PartialSpecEffect.java @@ -0,0 +1,93 @@ +package agency.highlysuspect.apathy.rule; + +import agency.highlysuspect.apathy.VerConv; +import agency.highlysuspect.apathy.core.Apathy; +import agency.highlysuspect.apathy.core.rule.CoolGsonHelper; +import agency.highlysuspect.apathy.core.rule.JsonSerializer; +import agency.highlysuspect.apathy.core.rule.Partial; +import agency.highlysuspect.apathy.core.rule.PartialSpecAlways; +import agency.highlysuspect.apathy.core.rule.Spec; +import agency.highlysuspect.apathy.core.rule.Who; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.entity.LivingEntity; + +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class PartialSpecEffect implements Spec { + public PartialSpecEffect(Set mobEffects, Who who) { + this.mobEffects = mobEffects; + this.who = who; + } + + private final Set mobEffects; + private final Who who; + + @Override + public Spec optimize() { + if(mobEffects.isEmpty()) return PartialSpecAlways.FALSE; + else return this; + } + + @Override + public Partial build() { + //i love useless microoptimizations + if(mobEffects.size() == 1) { + MobEffect theEffect = mobEffects.iterator().next(); + return (attacker, defender) -> { + LivingEntity which = who.choose(VerConv.mob(attacker), VerConv.player(defender)); + return which.hasEffect(theEffect); + }; + } else return (attacker, defender) -> { + LivingEntity which = who.choose(VerConv.mob(attacker), VerConv.player(defender)); + for(MobEffect effect : mobEffects) { + if(which.hasEffect(effect)) return true; + } + return false; + }; + } + + @Override + public JsonSerializer getSerializer() { + return Serializer.INSTANCE; + } + + public static class Serializer implements JsonSerializer { + public static final Serializer INSTANCE = new Serializer(); + + @Override + public void write(PartialSpecEffect thing, JsonObject json) { + json.add("effects", thing.mobEffects.stream() + .map(BuiltInRegistries.MOB_EFFECT::getKey) + .filter(Objects::nonNull) + .map(ResourceLocation::toString) + .map(JsonPrimitive::new) + .collect(CoolGsonHelper.toJsonArray())); + json.addProperty("who", thing.who.toString()); + } + + @Override + public PartialSpecEffect read(JsonObject json) { + Set mobEffects = CoolGsonHelper.streamArray(json.getAsJsonArray("effects")) + .map(JsonElement::getAsString) + .map(ResourceLocation::new) + .flatMap(rl -> { + MobEffect effect = BuiltInRegistries.MOB_EFFECT.get(rl); + if(effect == null) { + Apathy.instance.log.error("unknown mob effect: " + rl); + return Stream.of(); + } else return Stream.of(effect); + }) + .collect(Collectors.toSet()); + Who who = Who.fromString(json.get("who").getAsString()); + return new PartialSpecEffect(mobEffects, who); + } + } +} diff --git a/docs/2.x/JSON.md b/docs/2.x/JSON.md index 77daa25..3a6540a 100644 --- a/docs/2.x/JSON.md +++ b/docs/2.x/JSON.md @@ -389,13 +389,13 @@ This rule acts a litlte bit like `difficulty_case`. Note that this example makes ## `score` Arguments: * `objective`, any string -* `who`, either `"attacker"` (the attacking mob) or `"defender"` (the defending player) (if not specified, defaults to `"defender"` in 1.18.2) +* `who`, either `"attacker"` (the attacking mob) or `"defender"` (the defending player) * `thresholdMode`, either `"at_least"`, `"at_most"`, or `"equal"` * `threshold`, any integer The predicate tests a scoreboard value of either the attacker or the defending player (choose with `who`). It returns `true` if the test passes, and `false` if it does not. A `"thresholdMode"` of `"at_least"` performs a "greater than or equal to" test. `"at_most"` performs a "less than or equal to" test. -If the scoreboard objective does not exist, this predicate will always return `false`. +If the scoreboard objective does not exist, this predicate will always return `false`. If `who` is not specified, the default value is `"defender"`, but don't rely on this for new scripts. ### Example @@ -520,6 +520,32 @@ This rule will make mobs from dungeon spawners always hostile. } ``` +## `effect` **(NEW in 2.7)** +Arguments: +* `who`, either `"attacker"` or `"defender"` +* `effects`, array of potion effect IDs + +The predicate returns `true` if the attacking mob or the defending player (select with `who`) has at least one of the specified effects. + +### Example + +This rule will make mobs ignore players with the Invisibility effect. + +```json +{ + "type": "predicated", + "if_true": "deny", + "if_false": "pass", + "predicate": { + "type": "effect", + "who": "defender", + "effects": [ + "minecraft:invisibility" + ] + } +} +``` + ## `advancements` Arguments: * `advancements`, an array of strings corresponding to advancement IDs, like `["minecraft:story/enter_the_end", "minecraft:story/enter_the_nether"]` diff --git a/gradle.properties b/gradle.properties index d085441..40425b9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # Mod stuff -version = 2.6 +version = 2.7 group = agency.highlysuspect modId = apathy