| 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.level.block.state.BlockState;
|
| 11 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 12 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 13 | import net.minecraft.world.level.storage.loot.Validatable;
|
| 14 | import net.minecraft.world.level.storage.loot.ValidationContextSource;
|
| 15 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 16 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 17 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 18 | import net.minecraft.world.phys.Vec3;
|
| 19 |
|
| 20 | public class DefaultBlockInteractionTrigger extends SimpleCriterionTrigger<DefaultBlockInteractionTrigger.TriggerInstance> {
|
| 21 | @Override
|
| 22 | public Codec<DefaultBlockInteractionTrigger.TriggerInstance> codec() {
|
| 23 | return DefaultBlockInteractionTrigger.TriggerInstance.CODEC;
|
| 24 | }
|
| 25 |
|
| 26 | public void trigger(final ServerPlayer player, final BlockPos pos) {
|
| 27 | ServerLevel level = player.level();
|
| 28 | BlockState state = level.getBlockState(pos);
|
| 29 | LootParams params = new LootParams.Builder(level)
|
| 30 | .withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(pos))
|
| 31 | .withParameter(LootContextParams.THIS_ENTITY, player)
|
| 32 | .withParameter(LootContextParams.BLOCK_STATE, state)
|
| 33 | .create(LootContextParamSets.BLOCK_USE);
|
| 34 | LootContext context = new LootContext.Builder(params).create(Optional.empty());
|
| 35 | this.trigger(player, t -> t.matches(context));
|
| 36 | }
|
| 37 |
|
| 38 | public record TriggerInstance(Optional<Holder<LootItemCondition>> player, Optional<Holder<LootItemCondition>> location)
|
| 39 | implements SimpleCriterionTrigger.SimpleInstance {
|
| 40 | public static final Codec<DefaultBlockInteractionTrigger.TriggerInstance> CODEC = RecordCodecBuilder.create(
|
| 41 | i -> i.group(
|
| 42 | LootItemCondition.CODEC.optionalFieldOf("player").forGetter(DefaultBlockInteractionTrigger.TriggerInstance::player),
|
| 43 | LootItemCondition.CODEC.optionalFieldOf("location").forGetter(DefaultBlockInteractionTrigger.TriggerInstance::location)
|
| 44 | )
|
| 45 | .apply(i, DefaultBlockInteractionTrigger.TriggerInstance::new)
|
| 46 | );
|
| 47 |
|
| 48 | public boolean matches(final LootContext locationContext) {
|
| 49 | return this.location.isEmpty() || this.location.get().value().test(locationContext);
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public void validate(final ValidationContextSource validator) {
|
| 54 | SimpleCriterionTrigger.SimpleInstance.super.validate(validator);
|
| 55 | Validatable.validateHolder(validator.context(LootContextParamSets.BLOCK_USE), "location", this.location);
|
| 56 | }
|
| 57 | }
|
| 58 | }
|
| 59 |
|