Skip to content

Commit

Permalink
effect stuff, related #6 #11, not tested well and i need to improve d…
Browse files Browse the repository at this point in the history
…ocs and stuff
  • Loading branch information
quat1024 committed May 30, 2023
1 parent c73c908 commit 8b82cbe
Show file tree
Hide file tree
Showing 15 changed files with 414 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Partial, PartialSpecEffect> {
public PartialSpecEffect(Set<MobEffect> mobEffects, Who who) {
this.mobEffects = mobEffects;
this.who = who;
}

private final Set<MobEffect> mobEffects;
private final Who who;

@Override
public Spec<Partial, ?> 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<PartialSpecEffect> getSerializer() {
return Serializer.INSTANCE;
}

public static class Serializer implements JsonSerializer<PartialSpecEffect> {
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<MobEffect> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Partial, PartialSpecLocation> {
public PartialSpecLocation(LocationPredicate pred, LocationGetter who, String uniqueId, int offsetX, int offsetY, int offsetZ) {
this.pred = pred;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Partial, PartialSpecEffect> {
public PartialSpecEffect(Set<MobEffect> mobEffects, Who who) {
this.mobEffects = mobEffects;
this.who = who;
}

private final Set<MobEffect> mobEffects;
private final Who who;

@Override
public Spec<Partial, ?> 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<PartialSpecEffect> getSerializer() {
return Serializer.INSTANCE;
}

public static class Serializer implements JsonSerializer<PartialSpecEffect> {
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<MobEffect> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Partial, PartialSpecEffect> {
public PartialSpecEffect(Set<MobEffect> mobEffects, Who who) {
this.mobEffects = mobEffects;
this.who = who;
}

private final Set<MobEffect> mobEffects;
private final Who who;

@Override
public Spec<Partial, ?> 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<PartialSpecEffect> getSerializer() {
return Serializer.INSTANCE;
}

public static class Serializer implements JsonSerializer<PartialSpecEffect> {
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<MobEffect> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 8b82cbe

Please sign in to comment.