BlockPredicateArgument.java

net.minecraft.commands.arguments.blocks.BlockPredicateArgument

信息

  • 全限定名:net.minecraft.commands.arguments.blocks.BlockPredicateArgument
  • 类型:public class
  • 包:net.minecraft.commands.arguments.blocks
  • 源码路径:src/main/java/net/minecraft/commands/arguments/blocks/BlockPredicateArgument.java
  • 起始行号:L30
  • 实现:ArgumentType<BlockPredicateArgument.Result>
  • 职责:

    TODO

字段/常量

  • EXAMPLES

    • 类型: Collection<String>
    • 修饰符: private static final
    • 源码定位: L31
    • 说明:

      TODO

  • blocks

    • 类型: HolderLookup<Block>
    • 修饰符: private final
    • 源码定位: L32
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.commands.arguments.blocks.BlockPredicateArgument.BlockPredicate

    • 类型: class
    • 修饰符: private static
    • 源码定位: L68
    • 说明:

      TODO

  • net.minecraft.commands.arguments.blocks.BlockPredicateArgument.Result

    • 类型: interface
    • 修饰符: public
    • 源码定位: L105
    • 说明:

      TODO

  • net.minecraft.commands.arguments.blocks.BlockPredicateArgument.TagPredicate

    • 类型: class
    • 修饰符: private static
    • 源码定位: L109
    • 说明:

      TODO

构造器

public BlockPredicateArgument(CommandBuildContext context) @ L34

  • 构造器名:BlockPredicateArgument
  • 源码定位:L34
  • 修饰符:public

参数:

  • context: CommandBuildContext

说明:

TODO

方法

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

public static BlockPredicateArgument blockPredicate(CommandBuildContext context) @ L38

  • 方法名:blockPredicate
  • 源码定位:L38
  • 返回类型:BlockPredicateArgument
  • 修饰符:public static

参数:

  • context: CommandBuildContext

说明:

TODO

public BlockPredicateArgument.Result parse(StringReader reader) @ L42

  • 方法名:parse
  • 源码定位:L42
  • 返回类型:BlockPredicateArgument.Result
  • 修饰符:public

参数:

  • reader: StringReader

说明:

TODO

public static BlockPredicateArgument.Result parse(HolderLookup<Block> blocks, StringReader reader) @ L46

  • 方法名:parse
  • 源码定位:L46
  • 返回类型:BlockPredicateArgument.Result
  • 修饰符:public static

参数:

  • blocks: HolderLookup
  • reader: StringReader

说明:

TODO

public static Predicate<BlockInWorld> getBlockPredicate(CommandContext<CommandSourceStack> context, String name) @ L54

  • 方法名:getBlockPredicate
  • 源码定位:L54
  • 返回类型:Predicate
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) @ L58

  • 方法名:listSuggestions
  • 源码定位:L58
  • 返回类型: CompletableFuture
  • 修饰符:public

参数:

  • context: CommandContext
  • builder: SuggestionsBuilder

说明:

TODO

public Collection<String> getExamples() @ L63

  • 方法名:getExamples
  • 源码定位:L63
  • 返回类型:Collection
  • 修饰符:public

参数:

说明:

TODO

代码

public class BlockPredicateArgument implements ArgumentType<BlockPredicateArgument.Result> {
    private static final Collection<String> EXAMPLES = Arrays.asList("stone", "minecraft:stone", "stone[foo=bar]", "#stone", "#stone[foo=bar]{baz=nbt}");
    private final HolderLookup<Block> blocks;
 
    public BlockPredicateArgument(CommandBuildContext context) {
        this.blocks = context.lookupOrThrow(Registries.BLOCK);
    }
 
    public static BlockPredicateArgument blockPredicate(CommandBuildContext context) {
        return new BlockPredicateArgument(context);
    }
 
    public BlockPredicateArgument.Result parse(StringReader reader) throws CommandSyntaxException {
        return parse(this.blocks, reader);
    }
 
    public static BlockPredicateArgument.Result parse(HolderLookup<Block> blocks, StringReader reader) throws CommandSyntaxException {
        return BlockStateParser.parseForTesting(blocks, reader, true)
            .map(
                block -> new BlockPredicateArgument.BlockPredicate(block.blockState(), block.properties().keySet(), block.nbt()),
                tag -> new BlockPredicateArgument.TagPredicate(tag.tag(), tag.vagueProperties(), tag.nbt())
            );
    }
 
    public static Predicate<BlockInWorld> getBlockPredicate(CommandContext<CommandSourceStack> context, String name) throws CommandSyntaxException {
        return context.getArgument(name, BlockPredicateArgument.Result.class);
    }
 
    @Override
    public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
        return BlockStateParser.fillSuggestions(this.blocks, builder, true, true);
    }
 
    @Override
    public Collection<String> getExamples() {
        return EXAMPLES;
    }
 
    private static class BlockPredicate implements BlockPredicateArgument.Result {
        private final BlockState state;
        private final Set<Property<?>> properties;
        private final @Nullable CompoundTag nbt;
 
        public BlockPredicate(BlockState state, Set<Property<?>> properties, @Nullable CompoundTag nbt) {
            this.state = state;
            this.properties = properties;
            this.nbt = nbt;
        }
 
        public boolean test(BlockInWorld blockInWorld) {
            BlockState state = blockInWorld.getState();
            if (!state.is(this.state.getBlock())) {
                return false;
            } else {
                for (Property<?> property : this.properties) {
                    if (state.getValue(property) != this.state.getValue(property)) {
                        return false;
                    }
                }
 
                if (this.nbt == null) {
                    return true;
                } else {
                    BlockEntity entity = blockInWorld.getEntity();
                    return entity != null && NbtUtils.compareNbt(this.nbt, entity.saveWithFullMetadata(blockInWorld.getLevel().registryAccess()), true);
                }
            }
        }
 
        @Override
        public boolean requiresNbt() {
            return this.nbt != null;
        }
    }
 
    public interface Result extends Predicate<BlockInWorld> {
        boolean requiresNbt();
    }
 
    private static class TagPredicate implements BlockPredicateArgument.Result {
        private final HolderSet<Block> tag;
        private final @Nullable CompoundTag nbt;
        private final Map<String, String> vagueProperties;
 
        private TagPredicate(HolderSet<Block> tag, Map<String, String> vagueProperties, @Nullable CompoundTag nbt) {
            this.tag = tag;
            this.vagueProperties = vagueProperties;
            this.nbt = nbt;
        }
 
        public boolean test(BlockInWorld blockInWorld) {
            BlockState state = blockInWorld.getState();
            if (!state.is(this.tag)) {
                return false;
            } else {
                for (Entry<String, String> entry : this.vagueProperties.entrySet()) {
                    Property<?> property = state.getBlock().getStateDefinition().getProperty(entry.getKey());
                    if (property == null) {
                        return false;
                    }
 
                    Comparable<?> value = (Comparable<?>)property.getValue(entry.getValue()).orElse(null);
                    if (value == null) {
                        return false;
                    }
 
                    if (state.getValue(property) != value) {
                        return false;
                    }
                }
 
                if (this.nbt == null) {
                    return true;
                } else {
                    BlockEntity entity = blockInWorld.getEntity();
                    return entity != null && NbtUtils.compareNbt(this.nbt, entity.saveWithFullMetadata(blockInWorld.getLevel().registryAccess()), true);
                }
            }
        }
 
        @Override
        public boolean requiresNbt() {
            return this.nbt != null;
        }
    }
}

引用的其他类