BlockInput.java

net.minecraft.commands.arguments.blocks.BlockInput

信息

  • 全限定名:net.minecraft.commands.arguments.blocks.BlockInput
  • 类型:public class
  • 包:net.minecraft.commands.arguments.blocks
  • 源码路径:src/main/java/net/minecraft/commands/arguments/blocks/BlockInput.java
  • 起始行号:L22
  • 实现:Predicate
  • 职责:

    TODO

字段/常量

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L23
    • 说明:

      TODO

  • state

    • 类型: BlockState
    • 修饰符: private final
    • 源码定位: L24
    • 说明:

      TODO

  • properties

    • 类型: Set<Property<?>>
    • 修饰符: private final
    • 源码定位: L25
    • 说明:

      TODO

  • tag

    • 类型: CompoundTag
    • 修饰符: private final
    • 源码定位: L26
    • 说明:

      TODO

内部类/嵌套类型

构造器

public BlockInput(BlockState state, Set<Property<?>> properties, CompoundTag tag) @ L28

  • 构造器名:BlockInput
  • 源码定位:L28
  • 修饰符:public

参数:

  • state: BlockState
  • properties: Set<Property<?>>
  • tag: CompoundTag

说明:

TODO

方法

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

public BlockState getState() @ L34

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

参数:

说明:

TODO

public Set<Property<?>> getDefinedProperties() @ L38

  • 方法名:getDefinedProperties
  • 源码定位:L38
  • 返回类型:Set<Property<?>>
  • 修饰符:public

参数:

说明:

TODO

public boolean test(BlockInWorld blockInWorld) @ L42

  • 方法名:test
  • 源码定位:L42
  • 返回类型:boolean
  • 修饰符:public

参数:

  • blockInWorld: BlockInWorld

说明:

TODO

public boolean test(ServerLevel level, BlockPos pos) @ L62

  • 方法名:test
  • 源码定位:L62
  • 返回类型:boolean
  • 修饰符:public

参数:

  • level: ServerLevel
  • pos: BlockPos

说明:

TODO

public boolean place(ServerLevel level, BlockPos pos, int update) @ L66

  • 方法名:place
  • 源码定位:L66
  • 返回类型:boolean
  • 修饰符:public

参数:

  • level: ServerLevel
  • pos: BlockPos
  • update: int

说明:

TODO

private BlockState overwriteWithDefinedProperties(BlockState state) @ L103

  • 方法名:overwriteWithDefinedProperties
  • 源码定位:L103
  • 返回类型:BlockState
  • 修饰符:private

参数:

  • state: BlockState

说明:

TODO

private static <T extends Comparable<T>> BlockState copyProperty(BlockState target, BlockState source, Property<T> property) @ L115

  • 方法名:copyProperty
  • 源码定位:L115
  • 返回类型:<T extends Comparable> BlockState
  • 修饰符:private static

参数:

  • target: BlockState
  • source: BlockState
  • property: Property

说明:

TODO

代码

public class BlockInput implements Predicate<BlockInWorld> {
    private static final Logger LOGGER = LogUtils.getLogger();
    private final BlockState state;
    private final Set<Property<?>> properties;
    private final @Nullable CompoundTag tag;
 
    public BlockInput(BlockState state, Set<Property<?>> properties, @Nullable CompoundTag tag) {
        this.state = state;
        this.properties = properties;
        this.tag = tag;
    }
 
    public BlockState getState() {
        return this.state;
    }
 
    public Set<Property<?>> getDefinedProperties() {
        return this.properties;
    }
 
    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.tag == null) {
                return true;
            } else {
                BlockEntity entity = blockInWorld.getEntity();
                return entity != null && NbtUtils.compareNbt(this.tag, entity.saveWithFullMetadata(blockInWorld.getLevel().registryAccess()), true);
            }
        }
    }
 
    public boolean test(ServerLevel level, BlockPos pos) {
        return this.test(new BlockInWorld(level, pos, false));
    }
 
    public boolean place(ServerLevel level, BlockPos pos, @Block.UpdateFlags int update) {
        BlockState state = (update & 16) != 0 ? this.state : Block.updateFromNeighbourShapes(this.state, level, pos);
        if (state.isAir()) {
            state = this.state;
        }
 
        state = this.overwriteWithDefinedProperties(state);
        boolean affected = false;
        if (level.setBlock(pos, state, update)) {
            affected = true;
        }
 
        if (this.tag != null) {
            BlockEntity entity = level.getBlockEntity(pos);
            if (entity != null) {
                try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(LOGGER)) {
                    HolderLookup.Provider registries = level.registryAccess();
                    ProblemReporter blockEntityReporter = reporter.forChild(entity.problemPath());
                    TagValueOutput initialOutput = TagValueOutput.createWithContext(blockEntityReporter.forChild(() -> "(before)"), registries);
                    entity.saveWithoutMetadata(initialOutput);
                    CompoundTag before = initialOutput.buildResult();
                    entity.loadWithComponents(TagValueInput.create(reporter, registries, this.tag));
                    TagValueOutput updatedOutput = TagValueOutput.createWithContext(blockEntityReporter.forChild(() -> "(after)"), registries);
                    entity.saveWithoutMetadata(updatedOutput);
                    CompoundTag after = updatedOutput.buildResult();
                    if (!after.equals(before)) {
                        affected = true;
                        entity.setChanged();
                        level.getChunkSource().blockChanged(pos);
                    }
                }
            }
        }
 
        return affected;
    }
 
    private BlockState overwriteWithDefinedProperties(BlockState state) {
        if (state == this.state) {
            return state;
        } else {
            for (Property<?> property : this.properties) {
                state = copyProperty(state, this.state, property);
            }
 
            return state;
        }
    }
 
    private static <T extends Comparable<T>> BlockState copyProperty(BlockState target, BlockState source, Property<T> property) {
        return target.trySetValue(property, source.getValue(property));
    }
}

引用的其他类

  • BlockPos

    • 引用位置: 参数
  • CompoundTag

    • 引用位置: 参数/字段
  • NbtUtils

    • 引用位置: 方法调用
    • 关联成员: NbtUtils.compareNbt()
  • ServerLevel

    • 引用位置: 参数
  • ProblemReporter

    • 引用位置: 方法调用/构造调用
    • 关联成员: ProblemReporter.ScopedCollector(), ScopedCollector()
  • Block

    • 引用位置: 方法调用
    • 关联成员: Block.updateFromNeighbourShapes()
  • BlockState

    • 引用位置: 参数/字段/返回值
  • BlockInWorld

    • 引用位置: 参数/实现/构造调用
    • 关联成员: BlockInWorld()
  • Property

    • 引用位置: 参数/字段/返回值
  • TagValueInput

    • 引用位置: 方法调用
    • 关联成员: TagValueInput.create()
  • TagValueOutput

    • 引用位置: 方法调用
    • 关联成员: TagValueOutput.createWithContext()