| 1 | package net.minecraft.advancements.triggers;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.server.level.ServerPlayer;
|
| 10 | import net.minecraft.world.item.ItemInstance;
|
| 11 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 12 | import net.minecraft.world.level.block.state.BlockState;
|
| 13 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 14 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 15 | import net.minecraft.world.level.storage.loot.Validatable;
|
| 16 | import net.minecraft.world.level.storage.loot.ValidationContextSource;
|
| 17 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 18 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 19 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 20 | import net.minecraft.world.phys.Vec3;
|
| 21 |
|
| 22 | public class AnyBlockInteractionTrigger extends SimpleCriterionTrigger<AnyBlockInteractionTrigger.TriggerInstance> {
|
| 23 | @Override
|
| 24 | public Codec<AnyBlockInteractionTrigger.TriggerInstance> codec() {
|
| 25 | return AnyBlockInteractionTrigger.TriggerInstance.CODEC;
|
| 26 | }
|
| 27 |
|
| 28 | public void trigger(final ServerPlayer player, final BlockPos pos, final ItemInstance tool) {
|
| 29 | ServerLevel level = player.level();
|
| 30 | BlockState state = level.getBlockState(pos);
|
| 31 | BlockEntity blockEntity = level.getBlockEntity(pos);
|
| 32 | LootParams params = new LootParams.Builder(level)
|
| 33 | .withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(pos))
|
| 34 | .withParameter(LootContextParams.THIS_ENTITY, player)
|
| 35 | .withParameter(LootContextParams.BLOCK_STATE, state)
|
| 36 | .withOptionalParameter(LootContextParams.BLOCK_ENTITY, blockEntity)
|
| 37 | .withParameter(LootContextParams.TOOL, tool)
|
| 38 | .create(LootContextParamSets.ADVANCEMENT_LOCATION);
|
| 39 | LootContext context = new LootContext.Builder(params).create(Optional.empty());
|
| 40 | this.trigger(player, t -> t.matches(context));
|
| 41 | }
|
| 42 |
|
| 43 | public record TriggerInstance(Optional<Holder<LootItemCondition>> player, Optional<Holder<LootItemCondition>> location)
|
| 44 | implements SimpleCriterionTrigger.SimpleInstance {
|
| 45 | public static final Codec<AnyBlockInteractionTrigger.TriggerInstance> CODEC = RecordCodecBuilder.create(
|
| 46 | i -> i.group(
|
| 47 | LootItemCondition.CODEC.optionalFieldOf("player").forGetter(AnyBlockInteractionTrigger.TriggerInstance::player),
|
| 48 | LootItemCondition.CODEC.optionalFieldOf("location").forGetter(AnyBlockInteractionTrigger.TriggerInstance::location)
|
| 49 | )
|
| 50 | .apply(i, AnyBlockInteractionTrigger.TriggerInstance::new)
|
| 51 | );
|
| 52 |
|
| 53 | public boolean matches(final LootContext locationContext) {
|
| 54 | return this.location.isEmpty() || this.location.get().value().test(locationContext);
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public void validate(final ValidationContextSource validator) {
|
| 59 | SimpleCriterionTrigger.SimpleInstance.super.validate(validator);
|
| 60 | Validatable.validateHolder(validator.context(LootContextParamSets.ADVANCEMENT_LOCATION), "location", this.location);
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 |
|