| 1 | package net.minecraft.advancements.predicates.entity;
|
| 2 |
|
| 3 | import com.google.common.base.Suppliers;
|
| 4 | import com.google.common.collect.ImmutableList;
|
| 5 | import com.google.common.collect.Maps;
|
| 6 | import com.mojang.datafixers.kinds.App;
|
| 7 | import com.mojang.datafixers.util.Either;
|
| 8 | import com.mojang.serialization.Codec;
|
| 9 | import com.mojang.serialization.MapCodec;
|
| 10 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 11 | import com.mojang.serialization.codecs.RecordCodecBuilder.Mu;
|
| 12 | import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
|
| 13 | import it.unimi.dsi.fastutil.objects.Object2BooleanMaps;
|
| 14 | import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
|
| 15 | import it.unimi.dsi.fastutil.objects.Object2BooleanMap.Entry;
|
| 16 | import java.util.List;
|
| 17 | import java.util.Map;
|
| 18 | import java.util.Optional;
|
| 19 | import java.util.function.Predicate;
|
| 20 | import java.util.function.Supplier;
|
| 21 | import net.minecraft.advancements.AdvancementHolder;
|
| 22 | import net.minecraft.advancements.AdvancementProgress;
|
| 23 | import net.minecraft.advancements.CriterionProgress;
|
| 24 | import net.minecraft.advancements.predicates.FoodPredicate;
|
| 25 | import net.minecraft.advancements.predicates.GameTypePredicate;
|
| 26 | import net.minecraft.advancements.predicates.InputPredicate;
|
| 27 | import net.minecraft.advancements.predicates.MinMaxBounds;
|
| 28 | import net.minecraft.core.Holder;
|
| 29 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 30 | import net.minecraft.resources.Identifier;
|
| 31 | import net.minecraft.resources.ResourceKey;
|
| 32 | import net.minecraft.server.PlayerAdvancements;
|
| 33 | import net.minecraft.server.ServerAdvancementManager;
|
| 34 | import net.minecraft.server.level.ServerLevel;
|
| 35 | import net.minecraft.server.level.ServerPlayer;
|
| 36 | import net.minecraft.stats.ServerRecipeBook;
|
| 37 | import net.minecraft.stats.Stat;
|
| 38 | import net.minecraft.stats.StatType;
|
| 39 | import net.minecraft.stats.StatsCounter;
|
| 40 | import net.minecraft.util.ExtraCodecs;
|
| 41 | import net.minecraft.world.entity.Entity;
|
| 42 | import net.minecraft.world.entity.projectile.ProjectileUtil;
|
| 43 | import net.minecraft.world.item.crafting.Recipe;
|
| 44 | import net.minecraft.world.phys.AABB;
|
| 45 | import net.minecraft.world.phys.EntityHitResult;
|
| 46 | import net.minecraft.world.phys.HitResult;
|
| 47 | import net.minecraft.world.phys.Vec3;
|
| 48 | import org.jspecify.annotations.Nullable;
|
| 49 |
|
| 50 | public record PlayerPredicate(
|
| 51 | MinMaxBounds.Ints level,
|
| 52 | FoodPredicate food,
|
| 53 | GameTypePredicate gameType,
|
| 54 | List<PlayerPredicate.StatMatcher<?>> stats,
|
| 55 | Object2BooleanMap<ResourceKey<Recipe<?>>> recipes,
|
| 56 | Map<Identifier, PlayerPredicate.AdvancementPredicate> advancements,
|
| 57 | Optional<EntityPredicate> lookingAt,
|
| 58 | Optional<InputPredicate> input
|
| 59 | ) implements EntitySubPredicate {
|
| 60 | public static final int LOOKING_AT_RANGE = 100;
|
| 61 | public static final Codec<PlayerPredicate> CODEC = RecordCodecBuilder.create(
|
| 62 | i -> i.group(
|
| 63 | MinMaxBounds.Ints.CODEC.optionalFieldOf("level", MinMaxBounds.Ints.ANY).forGetter(PlayerPredicate::level),
|
| 64 | FoodPredicate.CODEC.optionalFieldOf("food", FoodPredicate.ANY).forGetter(PlayerPredicate::food),
|
| 65 | GameTypePredicate.CODEC.optionalFieldOf("gamemode", GameTypePredicate.ANY).forGetter(PlayerPredicate::gameType),
|
| 66 | PlayerPredicate.StatMatcher.CODEC.listOf().optionalFieldOf("stats", List.of()).forGetter(PlayerPredicate::stats),
|
| 67 | ExtraCodecs.object2BooleanMap(Recipe.KEY_CODEC).optionalFieldOf("recipes", Object2BooleanMaps.emptyMap()).forGetter(PlayerPredicate::recipes),
|
| 68 | Codec.unboundedMap(Identifier.CODEC, PlayerPredicate.AdvancementPredicate.CODEC)
|
| 69 | .optionalFieldOf("advancements", Map.of())
|
| 70 | .forGetter(PlayerPredicate::advancements),
|
| 71 | EntityPredicate.CODEC.optionalFieldOf("looking_at").forGetter(PlayerPredicate::lookingAt),
|
| 72 | InputPredicate.CODEC.optionalFieldOf("input").forGetter(PlayerPredicate::input)
|
| 73 | )
|
| 74 | .apply(i, PlayerPredicate::new)
|
| 75 | );
|
| 76 |
|
| 77 | @Override
|
| 78 | public boolean matches(final Entity entity, final ServerLevel level, final @Nullable Vec3 position) {
|
| 79 | if (!(entity instanceof ServerPlayer player)) {
|
| 80 | return false;
|
| 81 | } else {
|
| 82 | if (!this.level.matches(player.experienceLevel)) {
|
| 83 | return false;
|
| 84 | }
|
| 85 |
|
| 86 | if (!this.food.matches(player.getFoodData())) {
|
| 87 | return false;
|
| 88 | }
|
| 89 |
|
| 90 | if (!this.gameType.matches(player.gameMode())) {
|
| 91 | return false;
|
| 92 | }
|
| 93 |
|
| 94 | StatsCounter stats = player.getStats();
|
| 95 |
|
| 96 | for (PlayerPredicate.StatMatcher<?> stat : this.stats) {
|
| 97 | if (!stat.matches(stats)) {
|
| 98 | return false;
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | ServerRecipeBook recipes = player.getRecipeBook();
|
| 103 |
|
| 104 | for (Entry<ResourceKey<Recipe<?>>> e : this.recipes.object2BooleanEntrySet()) {
|
| 105 | if (recipes.contains(e.getKey()) != e.getBooleanValue()) {
|
| 106 | return false;
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | if (!this.advancements.isEmpty()) {
|
| 111 | PlayerAdvancements advancements = player.getAdvancements();
|
| 112 | ServerAdvancementManager serverAdvancements = player.level().getServer().getAdvancements();
|
| 113 |
|
| 114 | for (java.util.Map.Entry<Identifier, PlayerPredicate.AdvancementPredicate> entry : this.advancements.entrySet()) {
|
| 115 | AdvancementHolder advancement = serverAdvancements.get(entry.getKey());
|
| 116 | if (advancement == null || !entry.getValue().test(advancements.getOrStartProgress(advancement))) {
|
| 117 | return false;
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | if (this.lookingAt.isPresent()) {
|
| 123 | Vec3 from = player.getEyePosition();
|
| 124 | Vec3 viewVec = player.getViewVector(1.0F);
|
| 125 | Vec3 to = from.add(viewVec.x * 100.0, viewVec.y * 100.0, viewVec.z * 100.0);
|
| 126 | EntityHitResult lookingAtResult = ProjectileUtil.getEntityHitResult(
|
| 127 | player.level(), player, from, to, new AABB(from, to).inflate(1.0), e -> !e.isSpectator(), 0.0F
|
| 128 | );
|
| 129 | if (lookingAtResult == null || lookingAtResult.getType() != HitResult.Type.ENTITY) {
|
| 130 | return false;
|
| 131 | }
|
| 132 |
|
| 133 | Entity lookingAtEntity = lookingAtResult.getEntity();
|
| 134 | if (!this.lookingAt.get().matches(player, lookingAtEntity) || !player.hasLineOfSight(lookingAtEntity)) {
|
| 135 | return false;
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | return !this.input.isPresent() || this.input.get().matches(player.getLastClientInput());
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | private record AdvancementCriterionsPredicate(Object2BooleanMap<String> criterions) implements PlayerPredicate.AdvancementPredicate {
|
| 144 | public static final Codec<PlayerPredicate.AdvancementCriterionsPredicate> CODEC = ExtraCodecs.object2BooleanMap(Codec.STRING)
|
| 145 | .xmap(PlayerPredicate.AdvancementCriterionsPredicate::new, PlayerPredicate.AdvancementCriterionsPredicate::criterions);
|
| 146 |
|
| 147 | public boolean test(final AdvancementProgress progress) {
|
| 148 | for (Entry<String> e : this.criterions.object2BooleanEntrySet()) {
|
| 149 | CriterionProgress criterion = progress.getCriterion(e.getKey());
|
| 150 | if (criterion == null || criterion.isDone() != e.getBooleanValue()) {
|
| 151 | return false;
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | return true;
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | private record AdvancementDonePredicate(boolean state) implements PlayerPredicate.AdvancementPredicate {
|
| 160 | public static final Codec<PlayerPredicate.AdvancementDonePredicate> CODEC = Codec.BOOL
|
| 161 | .xmap(PlayerPredicate.AdvancementDonePredicate::new, PlayerPredicate.AdvancementDonePredicate::state);
|
| 162 |
|
| 163 | public boolean test(final AdvancementProgress progress) {
|
| 164 | return progress.isDone() == this.state;
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | private interface AdvancementPredicate extends Predicate<AdvancementProgress> {
|
| 169 | Codec<PlayerPredicate.AdvancementPredicate> CODEC = Codec.either(
|
| 170 | PlayerPredicate.AdvancementDonePredicate.CODEC, PlayerPredicate.AdvancementCriterionsPredicate.CODEC
|
| 171 | )
|
| 172 | .xmap(Either::unwrap, predicate -> {
|
| 173 | if (predicate instanceof PlayerPredicate.AdvancementDonePredicate done) {
|
| 174 | return Either.left(done);
|
| 175 | } else if (predicate instanceof PlayerPredicate.AdvancementCriterionsPredicate criterions) {
|
| 176 | return Either.right(criterions);
|
| 177 | } else {
|
| 178 | throw new UnsupportedOperationException();
|
| 179 | }
|
| 180 | });
|
| 181 | }
|
| 182 |
|
| 183 | public static class Builder {
|
| 184 | private MinMaxBounds.Ints level = MinMaxBounds.Ints.ANY;
|
| 185 | private FoodPredicate food = FoodPredicate.ANY;
|
| 186 | private GameTypePredicate gameType = GameTypePredicate.ANY;
|
| 187 | private final ImmutableList.Builder<PlayerPredicate.StatMatcher<?>> stats = ImmutableList.builder();
|
| 188 | private final Object2BooleanMap<ResourceKey<Recipe<?>>> recipes = new Object2BooleanOpenHashMap<>();
|
| 189 | private final Map<Identifier, PlayerPredicate.AdvancementPredicate> advancements = Maps.newHashMap();
|
| 190 | private Optional<EntityPredicate> lookingAt = Optional.empty();
|
| 191 | private Optional<InputPredicate> input = Optional.empty();
|
| 192 |
|
| 193 | public static PlayerPredicate.Builder player() {
|
| 194 | return new PlayerPredicate.Builder();
|
| 195 | }
|
| 196 |
|
| 197 | public PlayerPredicate.Builder setLevel(final MinMaxBounds.Ints level) {
|
| 198 | this.level = level;
|
| 199 | return this;
|
| 200 | }
|
| 201 |
|
| 202 | public PlayerPredicate.Builder setFood(final FoodPredicate food) {
|
| 203 | this.food = food;
|
| 204 | return this;
|
| 205 | }
|
| 206 |
|
| 207 | public <T> PlayerPredicate.Builder addStat(final StatType<T> type, final Holder.Reference<T> value, final MinMaxBounds.Ints range) {
|
| 208 | this.stats.add(new PlayerPredicate.StatMatcher<>(type, value, range));
|
| 209 | return this;
|
| 210 | }
|
| 211 |
|
| 212 | public PlayerPredicate.Builder addRecipe(final ResourceKey<Recipe<?>> recipe, final boolean present) {
|
| 213 | this.recipes.put(recipe, present);
|
| 214 | return this;
|
| 215 | }
|
| 216 |
|
| 217 | public PlayerPredicate.Builder setGameType(final GameTypePredicate gameType) {
|
| 218 | this.gameType = gameType;
|
| 219 | return this;
|
| 220 | }
|
| 221 |
|
| 222 | public PlayerPredicate.Builder setLookingAt(final EntityPredicate.Builder lookingAt) {
|
| 223 | this.lookingAt = Optional.of(lookingAt.build());
|
| 224 | return this;
|
| 225 | }
|
| 226 |
|
| 227 | public PlayerPredicate.Builder checkAdvancementDone(final Identifier advancement, final boolean isDone) {
|
| 228 | this.advancements.put(advancement, new PlayerPredicate.AdvancementDonePredicate(isDone));
|
| 229 | return this;
|
| 230 | }
|
| 231 |
|
| 232 | public PlayerPredicate.Builder checkAdvancementCriterions(final Identifier advancement, final Map<String, Boolean> criterions) {
|
| 233 | this.advancements.put(advancement, new PlayerPredicate.AdvancementCriterionsPredicate(new Object2BooleanOpenHashMap<>(criterions)));
|
| 234 | return this;
|
| 235 | }
|
| 236 |
|
| 237 | public PlayerPredicate.Builder hasInput(final InputPredicate input) {
|
| 238 | this.input = Optional.of(input);
|
| 239 | return this;
|
| 240 | }
|
| 241 |
|
| 242 | public PlayerPredicate build() {
|
| 243 | return new PlayerPredicate(this.level, this.food, this.gameType, this.stats.build(), this.recipes, this.advancements, this.lookingAt, this.input);
|
| 244 | }
|
| 245 | }
|
| 246 |
|
| 247 | private record StatMatcher<T>(StatType<T> type, Holder<T> value, MinMaxBounds.Ints range, Supplier<Stat<T>> stat) {
|
| 248 | public static final Codec<PlayerPredicate.StatMatcher<?>> CODEC = BuiltInRegistries.STAT_TYPE
|
| 249 | .byNameCodec()
|
| 250 | .dispatch(PlayerPredicate.StatMatcher::type, PlayerPredicate.StatMatcher::createTypedCodec);
|
| 251 |
|
| 252 | public StatMatcher(final StatType<T> type, final Holder<T> value, final MinMaxBounds.Ints range) {
|
| 253 | this(type, value, range, Suppliers.memoize(() -> type.get(value.value())));
|
| 254 | }
|
| 255 |
|
| 256 | private static <T> MapCodec<PlayerPredicate.StatMatcher<T>> createTypedCodec(final StatType<T> type) {
|
| 257 | return RecordCodecBuilder.mapCodec(
|
| 258 | i -> i.group(
|
| 259 | (App<Mu<? extends PlayerPredicate.StatMatcher<?>>, Holder<T>>)type.getRegistry()
|
| 260 | .holderByNameCodec()
|
| 261 | .fieldOf("stat")
|
| 262 | .forGetter(PlayerPredicate.StatMatcher::value),
|
| 263 | (App<Mu<? extends PlayerPredicate.StatMatcher<?>>, MinMaxBounds.Ints>)MinMaxBounds.Ints.CODEC
|
| 264 | .optionalFieldOf("value", MinMaxBounds.Ints.ANY)
|
| 265 | .forGetter(PlayerPredicate.StatMatcher::range)
|
| 266 | )
|
| 267 | .apply(i, (value, range) -> new PlayerPredicate.StatMatcher<>(type, value, range))
|
| 268 | );
|
| 269 | }
|
| 270 |
|
| 271 | public boolean matches(final StatsCounter counter) {
|
| 272 | return this.range.matches(counter.getValue(this.stat.get()));
|
| 273 | }
|
| 274 | }
|
| 275 | }
|
| 276 |
|