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

59 lines
2.1 KiB
Java
Raw Normal View History

2021-08-19 14:43:15 +02:00
package com.minelittlepony.unicopia.advancement;
import com.google.gson.JsonObject;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.Unicopia;
2021-08-19 14:43:15 +02:00
import com.minelittlepony.unicopia.entity.player.Pony;
import net.minecraft.advancement.criterion.AbstractCriterion;
import net.minecraft.advancement.criterion.AbstractCriterionConditions;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.predicate.entity.AdvancementEntityPredicateDeserializer;
import net.minecraft.predicate.entity.AdvancementEntityPredicateSerializer;
2023-06-03 13:40:54 +02:00
import net.minecraft.predicate.entity.LootContextPredicate;
2021-08-19 14:43:15 +02:00
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
public class RaceChangeCriterion extends AbstractCriterion<RaceChangeCriterion.Conditions> {
private static final Identifier ID = Unicopia.id("player_change_race");
2021-08-19 14:43:15 +02:00
@Override
public Identifier getId() {
return ID;
}
@Override
2023-06-03 13:40:54 +02:00
protected Conditions conditionsFromJson(JsonObject json, LootContextPredicate playerPredicate, AdvancementEntityPredicateDeserializer deserializer) {
2022-08-27 15:07:29 +02:00
return new Conditions(playerPredicate, Race.fromName(JsonHelper.getString(json, "race"), Race.EARTH));
2021-08-19 14:43:15 +02:00
}
public void trigger(PlayerEntity player) {
if (player instanceof ServerPlayerEntity) {
2021-12-22 10:15:09 +01:00
trigger((ServerPlayerEntity)player, c -> c.test((ServerPlayerEntity)player));
2021-08-19 14:43:15 +02:00
}
}
public static class Conditions extends AbstractCriterionConditions {
private final Race race;
2023-06-03 13:40:54 +02:00
public Conditions(LootContextPredicate playerPredicate, Race race) {
2021-08-19 14:43:15 +02:00
super(ID, playerPredicate);
this.race = race;
}
public boolean test(ServerPlayerEntity player) {
return Pony.of(player).getSpecies() == race;
2021-08-19 14:43:15 +02:00
}
@Override
public JsonObject toJson(AdvancementEntityPredicateSerializer serializer) {
JsonObject json = super.toJson(serializer);
2022-08-27 15:07:29 +02:00
json.addProperty("race", Race.REGISTRY.getId(race).toString());
2021-08-19 14:43:15 +02:00
return json;
}
}
}