Unicopia/src/main/java/com/minelittlepony/unicopia/advancement/CustomEventCriterion.java

63 lines
2.6 KiB
Java
Raw Normal View History

2021-08-09 15:20:33 +02:00
package com.minelittlepony.unicopia.advancement;
2023-09-29 20:15:04 +02:00
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
2021-08-19 00:49:02 +02:00
import com.minelittlepony.unicopia.entity.player.Pony;
2023-12-30 11:55:26 +01:00
import com.minelittlepony.unicopia.util.CodecUtils;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
2021-08-09 15:20:33 +02:00
2023-12-30 11:55:26 +01:00
import net.fabricmc.fabric.api.util.TriState;
2021-08-09 15:20:33 +02:00
import net.minecraft.advancement.criterion.AbstractCriterion;
import net.minecraft.entity.Entity;
2023-12-30 11:55:26 +01:00
import net.minecraft.predicate.entity.EntityPredicate;
2023-06-03 13:40:54 +02:00
import net.minecraft.predicate.entity.LootContextPredicate;
2021-08-09 15:20:33 +02:00
import net.minecraft.server.network.ServerPlayerEntity;
2023-12-30 11:55:26 +01:00
import net.minecraft.util.dynamic.Codecs;
2021-08-09 15:20:33 +02:00
public class CustomEventCriterion extends AbstractCriterion<CustomEventCriterion.Conditions> {
@Override
2023-12-30 11:55:26 +01:00
public Codec<Conditions> getConditionsCodec() {
return Conditions.CODEC;
2021-08-09 15:20:33 +02:00
}
public CustomEventCriterion.Trigger createTrigger(String name) {
return player -> {
if (player instanceof ServerPlayerEntity p) {
int counter = Pony.of(p).getAdvancementProgress().compute(name, (key, i) -> i == null ? 1 : i + 1);
2022-01-14 10:09:01 +01:00
trigger(p, c -> c.test(name, counter, p));
2021-08-09 15:20:33 +02:00
}
};
}
public interface Trigger {
void trigger(@Nullable Entity player);
2021-08-09 15:20:33 +02:00
}
2023-12-30 11:55:26 +01:00
public record Conditions (
Optional<LootContextPredicate> player,
String event,
RacePredicate races,
TriState flying,
int repeatCount) implements AbstractCriterion.Conditions {
public static final Codec<Conditions> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codecs.createStrictOptionalFieldCodec(EntityPredicate.LOOT_CONTEXT_PREDICATE_CODEC, "player").forGetter(Conditions::player),
Codec.STRING.fieldOf("event").forGetter(Conditions::event),
2024-02-08 23:08:26 +01:00
RacePredicate.CODEC.optionalFieldOf("races", RacePredicate.EMPTY).forGetter(Conditions::races),
2023-12-30 11:55:26 +01:00
CodecUtils.tristateOf("flying").forGetter(Conditions::flying),
Codec.INT.optionalFieldOf("repeatCount", 0).forGetter(Conditions::repeatCount)
).apply(instance, Conditions::new));
2021-08-09 15:20:33 +02:00
2022-01-14 10:09:01 +01:00
public boolean test(String event, int count, ServerPlayerEntity player) {
2023-12-30 11:55:26 +01:00
boolean isFlying = Pony.of(player).getPhysics().isFlying();
2021-08-19 14:43:15 +02:00
return this.event.equalsIgnoreCase(event)
2023-09-09 19:46:41 +02:00
&& races.test(player)
2023-12-30 11:55:26 +01:00
&& flying.orElse(isFlying) == isFlying
2022-01-14 10:09:01 +01:00
&& (repeatCount <= 0 || (count > 0 && count % repeatCount == 0));
2021-08-09 15:20:33 +02:00
}
}
}