ItemUsedOnLocationTrigger.java

net.minecraft.advancements.criterion.ItemUsedOnLocationTrigger

信息

  • 全限定名:net.minecraft.advancements.criterion.ItemUsedOnLocationTrigger
  • 类型:public class
  • 包:net.minecraft.advancements.criterion
  • 源码路径:src/main/java/net/minecraft/advancements/criterion/ItemUsedOnLocationTrigger.java
  • 起始行号:L28
  • 继承:SimpleCriterionTrigger<ItemUsedOnLocationTrigger.TriggerInstance>
  • 职责:

    TODO

字段/常量

内部类/嵌套类型

  • net.minecraft.advancements.criterion.ItemUsedOnLocationTrigger.TriggerInstance
    • 类型: record
    • 修饰符: public
    • 源码定位: L47
    • 说明:

      TODO

构造器

方法

下面的方法块按源码顺序生成。

public Codec<ItemUsedOnLocationTrigger.TriggerInstance> codec() @ L29

  • 方法名:codec
  • 源码定位:L29
  • 返回类型:Codec<ItemUsedOnLocationTrigger.TriggerInstance>
  • 修饰符:public

参数:

说明:

TODO

public void trigger(ServerPlayer player, BlockPos pos, ItemInstance tool) @ L34

  • 方法名:trigger
  • 源码定位:L34
  • 返回类型:void
  • 修饰符:public

参数:

  • player: ServerPlayer
  • pos: BlockPos
  • tool: ItemInstance

说明:

TODO

代码

public class ItemUsedOnLocationTrigger extends SimpleCriterionTrigger<ItemUsedOnLocationTrigger.TriggerInstance> {
    @Override
    public Codec<ItemUsedOnLocationTrigger.TriggerInstance> codec() {
        return ItemUsedOnLocationTrigger.TriggerInstance.CODEC;
    }
 
    public void trigger(ServerPlayer player, BlockPos pos, ItemInstance tool) {
        ServerLevel level = player.level();
        BlockState state = level.getBlockState(pos);
        LootParams params = new LootParams.Builder(level)
            .withParameter(LootContextParams.ORIGIN, pos.getCenter())
            .withParameter(LootContextParams.THIS_ENTITY, player)
            .withParameter(LootContextParams.BLOCK_STATE, state)
            .withParameter(LootContextParams.TOOL, tool)
            .create(LootContextParamSets.ADVANCEMENT_LOCATION);
        LootContext context = new LootContext.Builder(params).create(Optional.empty());
        this.trigger(player, t -> t.matches(context));
    }
 
    public record TriggerInstance(Optional<ContextAwarePredicate> player, Optional<ContextAwarePredicate> location)
        implements SimpleCriterionTrigger.SimpleInstance {
        public static final Codec<ItemUsedOnLocationTrigger.TriggerInstance> CODEC = RecordCodecBuilder.create(
            i -> i.group(
                    EntityPredicate.ADVANCEMENT_CODEC.optionalFieldOf("player").forGetter(ItemUsedOnLocationTrigger.TriggerInstance::player),
                    ContextAwarePredicate.CODEC.optionalFieldOf("location").forGetter(ItemUsedOnLocationTrigger.TriggerInstance::location)
                )
                .apply(i, ItemUsedOnLocationTrigger.TriggerInstance::new)
        );
 
        public static Criterion<ItemUsedOnLocationTrigger.TriggerInstance> placedBlock(Block block) {
            ContextAwarePredicate location = ContextAwarePredicate.create(LootItemBlockStatePropertyCondition.hasBlockStateProperties(block).build());
            return CriteriaTriggers.PLACED_BLOCK.createCriterion(new ItemUsedOnLocationTrigger.TriggerInstance(Optional.empty(), Optional.of(location)));
        }
 
        public static Criterion<ItemUsedOnLocationTrigger.TriggerInstance> placedBlock(LootItemCondition.Builder... conditions) {
            ContextAwarePredicate location = ContextAwarePredicate.create(
                Arrays.stream(conditions).map(LootItemCondition.Builder::build).toArray(LootItemCondition[]::new)
            );
            return CriteriaTriggers.PLACED_BLOCK.createCriterion(new ItemUsedOnLocationTrigger.TriggerInstance(Optional.empty(), Optional.of(location)));
        }
 
        public static <T extends Comparable<T>> Criterion<ItemUsedOnLocationTrigger.TriggerInstance> placedBlockWithProperties(
            Block block, Property<T> property, String propertyValue
        ) {
            StatePropertiesPredicate.Builder predicateBuilder = StatePropertiesPredicate.Builder.properties().hasProperty(property, propertyValue);
            ContextAwarePredicate location = ContextAwarePredicate.create(
                LootItemBlockStatePropertyCondition.hasBlockStateProperties(block).setProperties(predicateBuilder).build()
            );
            return CriteriaTriggers.PLACED_BLOCK.createCriterion(new ItemUsedOnLocationTrigger.TriggerInstance(Optional.empty(), Optional.of(location)));
        }
 
        public static Criterion<ItemUsedOnLocationTrigger.TriggerInstance> placedBlockWithProperties(
            Block block, Property<Boolean> property, boolean propertyValue
        ) {
            return placedBlockWithProperties(block, property, String.valueOf(propertyValue));
        }
 
        public static Criterion<ItemUsedOnLocationTrigger.TriggerInstance> placedBlockWithProperties(Block block, Property<Integer> property, int propertyValue) {
            return placedBlockWithProperties(block, property, String.valueOf(propertyValue));
        }
 
        public static <T extends Comparable<T> & StringRepresentable> Criterion<ItemUsedOnLocationTrigger.TriggerInstance> placedBlockWithProperties(
            Block block, Property<T> properties, T propertyValue
        ) {
            return placedBlockWithProperties(block, properties, propertyValue.getSerializedName());
        }
 
        private static ItemUsedOnLocationTrigger.TriggerInstance itemUsedOnLocation(LocationPredicate.Builder location, ItemPredicate.Builder item) {
            ContextAwarePredicate predicate = ContextAwarePredicate.create(LocationCheck.checkLocation(location).build(), MatchTool.toolMatches(item).build());
            return new ItemUsedOnLocationTrigger.TriggerInstance(Optional.empty(), Optional.of(predicate));
        }
 
        public static Criterion<ItemUsedOnLocationTrigger.TriggerInstance> itemUsedOnBlock(LocationPredicate.Builder location, ItemPredicate.Builder item) {
            return CriteriaTriggers.ITEM_USED_ON_BLOCK.createCriterion(itemUsedOnLocation(location, item));
        }
 
        public static Criterion<ItemUsedOnLocationTrigger.TriggerInstance> allayDropItemOnBlock(LocationPredicate.Builder location, ItemPredicate.Builder item) {
            return CriteriaTriggers.ALLAY_DROP_ITEM_ON_BLOCK.createCriterion(itemUsedOnLocation(location, item));
        }
 
        public boolean matches(LootContext locationContext) {
            return this.location.isEmpty() || this.location.get().matches(locationContext);
        }
 
        @Override
        public void validate(ValidationContextSource validator) {
            SimpleCriterionTrigger.SimpleInstance.super.validate(validator);
            Validatable.validate(validator.context(LootContextParamSets.ADVANCEMENT_LOCATION), "location", this.location);
        }
    }
}

引用的其他类