TestInstanceBlockEditScreen.java

net.minecraft.client.gui.screens.inventory.TestInstanceBlockEditScreen

信息

  • 全限定名:net.minecraft.client.gui.screens.inventory.TestInstanceBlockEditScreen
  • 类型:public class
  • 包:net.minecraft.client.gui.screens.inventory
  • 源码路径:src/main/java/net/minecraft/client/gui/screens/inventory/TestInstanceBlockEditScreen.java
  • 起始行号:L29
  • 继承:Screen
  • 职责:

    TODO

字段/常量

  • ID_LABEL

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

      TODO

  • SIZE_LABEL

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

      TODO

  • INCLUDE_ENTITIES_LABEL

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

      TODO

  • ROTATION_LABEL

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

      TODO

  • BUTTON_PADDING

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

      TODO

  • WIDTH

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

      TODO

  • blockEntity

    • 类型: TestInstanceBlockEntity
    • 修饰符: private final
    • 源码定位: L36
    • 说明:

      TODO

  • idEdit

    • 类型: EditBox
    • 修饰符: private
    • 源码定位: L37
    • 说明:

      TODO

  • sizeXEdit

    • 类型: EditBox
    • 修饰符: private
    • 源码定位: L38
    • 说明:

      TODO

  • sizeYEdit

    • 类型: EditBox
    • 修饰符: private
    • 源码定位: L39
    • 说明:

      TODO

  • sizeZEdit

    • 类型: EditBox
    • 修饰符: private
    • 源码定位: L40
    • 说明:

      TODO

  • infoWidget

    • 类型: FittingMultiLineTextWidget
    • 修饰符: private
    • 源码定位: L41
    • 说明:

      TODO

  • saveButton

    • 类型: Button
    • 修饰符: private
    • 源码定位: L42
    • 说明:

      TODO

  • exportButton

    • 类型: Button
    • 修饰符: private
    • 源码定位: L43
    • 说明:

      TODO

  • includeEntitiesButton

    • 类型: CycleButton<Boolean>
    • 修饰符: private
    • 源码定位: L44
    • 说明:

      TODO

  • rotationButton

    • 类型: CycleButton<Rotation>
    • 修饰符: private
    • 源码定位: L45
    • 说明:

      TODO

内部类/嵌套类型

构造器

public TestInstanceBlockEditScreen(TestInstanceBlockEntity blockEntity) @ L47

  • 构造器名:TestInstanceBlockEditScreen
  • 源码定位:L47
  • 修饰符:public

参数:

  • blockEntity: TestInstanceBlockEntity

说明:

TODO

方法

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

protected void init() @ L52

  • 方法名:init
  • 源码定位:L52
  • 返回类型:void
  • 修饰符:protected

参数:

说明:

TODO

private void updateSaveState() @ L119

  • 方法名:updateSaveState
  • 源码定位:L119
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

private static Component rotationDisplay(Rotation rotation) @ L127

  • 方法名:rotationDisplay
  • 源码定位:L127
  • 返回类型:Component
  • 修饰符:private static

参数:

  • rotation: Rotation

说明:

TODO

private void setSize(Vec3i size) @ L136

  • 方法名:setSize
  • 源码定位:L136
  • 返回类型:void
  • 修饰符:private

参数:

  • size: Vec3i

说明:

TODO

private int widgetX(int index, int count) @ L142

  • 方法名:widgetX
  • 源码定位:L142
  • 返回类型:int
  • 修饰符:private

参数:

  • index: int
  • count: int

说明:

TODO

private static int widgetSize(int count) @ L148

  • 方法名:widgetSize
  • 源码定位:L148
  • 返回类型:int
  • 修饰符:private static

参数:

  • count: int

说明:

TODO

private static float exactWidgetSize(int count) @ L152

  • 方法名:exactWidgetSize
  • 源码定位:L152
  • 返回类型:float
  • 修饰符:private static

参数:

  • count: int

说明:

TODO

public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) @ L156

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

参数:

  • graphics: GuiGraphicsExtractor
  • mouseX: int
  • mouseY: int
  • a: float

说明:

TODO

private void updateTestInfo(boolean isInit) @ L167

  • 方法名:updateTestInfo
  • 源码定位:L167
  • 返回类型:void
  • 修饰符:private

参数:

  • isInit: boolean

说明:

TODO

private void onDone() @ L176

  • 方法名:onDone
  • 源码定位:L176
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

private boolean sendToServer(ServerboundTestInstanceBlockActionPacket.Action action) @ L181

  • 方法名:sendToServer
  • 源码定位:L181
  • 返回类型:boolean
  • 修饰符:private

参数:

  • action: ServerboundTestInstanceBlockActionPacket.Action

说明:

TODO

public void setStatus(Component status, Optional<Vec3i> size) @ L194

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

参数:

  • status: Component
  • size: Optional

说明:

TODO

private void onCancel() @ L209

  • 方法名:onCancel
  • 源码定位:L209
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

private static int parseSize(String value) @ L213

  • 方法名:parseSize
  • 源码定位:L213
  • 返回类型:int
  • 修饰符:private static

参数:

  • value: String

说明:

TODO

public boolean isInGameUi() @ L221

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

参数:

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class TestInstanceBlockEditScreen extends Screen {
    private static final Component ID_LABEL = Component.translatable("test_instance_block.test_id");
    private static final Component SIZE_LABEL = Component.translatable("test_instance_block.size");
    private static final Component INCLUDE_ENTITIES_LABEL = Component.translatable("test_instance_block.entities");
    private static final Component ROTATION_LABEL = Component.translatable("test_instance_block.rotation");
    private static final int BUTTON_PADDING = 8;
    private static final int WIDTH = 316;
    private final TestInstanceBlockEntity blockEntity;
    private @Nullable EditBox idEdit;
    private @Nullable EditBox sizeXEdit;
    private @Nullable EditBox sizeYEdit;
    private @Nullable EditBox sizeZEdit;
    private @Nullable FittingMultiLineTextWidget infoWidget;
    private @Nullable Button saveButton;
    private @Nullable Button exportButton;
    private @Nullable CycleButton<Boolean> includeEntitiesButton;
    private @Nullable CycleButton<Rotation> rotationButton;
 
    public TestInstanceBlockEditScreen(TestInstanceBlockEntity blockEntity) {
        super(blockEntity.getBlockState().getBlock().getName());
        this.blockEntity = blockEntity;
    }
 
    @Override
    protected void init() {
        int leftEdge = this.width / 2 - 158;
        boolean includeExport = SharedConstants.IS_RUNNING_IN_IDE;
        int actionButtonCount = includeExport ? 3 : 2;
        int buttonSize = widgetSize(actionButtonCount);
        this.idEdit = new EditBox(this.font, leftEdge, 40, 316, 20, Component.translatable("test_instance_block.test_id"));
        this.idEdit.setMaxLength(128);
        Optional<ResourceKey<GameTestInstance>> test = this.blockEntity.test();
        if (test.isPresent()) {
            this.idEdit.setValue(test.get().identifier().toString());
        }
 
        this.idEdit.setResponder(s -> this.updateTestInfo(false));
        this.addRenderableWidget(this.idEdit);
        this.infoWidget = new FittingMultiLineTextWidget(leftEdge, 70, 316, 8 * 9, Component.literal(""), this.font);
        this.addRenderableWidget(this.infoWidget);
        Vec3i size = this.blockEntity.getSize();
        int index = 0;
        this.sizeXEdit = new EditBox(this.font, this.widgetX(index++, 5), 160, widgetSize(5), 20, Component.translatable("structure_block.size.x"));
        this.sizeXEdit.setMaxLength(15);
        this.addRenderableWidget(this.sizeXEdit);
        this.sizeYEdit = new EditBox(this.font, this.widgetX(index++, 5), 160, widgetSize(5), 20, Component.translatable("structure_block.size.y"));
        this.sizeYEdit.setMaxLength(15);
        this.addRenderableWidget(this.sizeYEdit);
        this.sizeZEdit = new EditBox(this.font, this.widgetX(index++, 5), 160, widgetSize(5), 20, Component.translatable("structure_block.size.z"));
        this.sizeZEdit.setMaxLength(15);
        this.addRenderableWidget(this.sizeZEdit);
        this.setSize(size);
        this.rotationButton = this.addRenderableWidget(
            CycleButton.builder(TestInstanceBlockEditScreen::rotationDisplay, this.blockEntity.getRotation())
                .withValues(Rotation.values())
                .displayOnlyValue()
                .create(this.widgetX(index++, 5), 160, widgetSize(5), 20, ROTATION_LABEL, (button, value) -> this.updateSaveState())
        );
        this.includeEntitiesButton = this.addRenderableWidget(
            CycleButton.onOffBuilder(!this.blockEntity.ignoreEntities())
                .displayOnlyValue()
                .create(this.widgetX(index++, 5), 160, widgetSize(5), 20, INCLUDE_ENTITIES_LABEL)
        );
        index = 0;
        this.addRenderableWidget(Button.builder(Component.translatable("test_instance.action.reset"), button -> {
            this.sendToServer(ServerboundTestInstanceBlockActionPacket.Action.RESET);
            this.minecraft.setScreen(null);
        }).bounds(this.widgetX(index++, actionButtonCount), 185, buttonSize, 20).build());
        this.saveButton = this.addRenderableWidget(Button.builder(Component.translatable("test_instance.action.save"), button -> {
            this.sendToServer(ServerboundTestInstanceBlockActionPacket.Action.SAVE);
            this.minecraft.setScreen(null);
        }).bounds(this.widgetX(index++, actionButtonCount), 185, buttonSize, 20).build());
        if (includeExport) {
            this.exportButton = this.addRenderableWidget(Button.builder(Component.literal("Export Structure"), button -> {
                this.sendToServer(ServerboundTestInstanceBlockActionPacket.Action.EXPORT);
                this.minecraft.setScreen(null);
            }).bounds(this.widgetX(index++, actionButtonCount), 185, buttonSize, 20).build());
        }
 
        this.addRenderableWidget(Button.builder(Component.translatable("test_instance.action.run"), button -> {
            this.sendToServer(ServerboundTestInstanceBlockActionPacket.Action.RUN);
            this.minecraft.setScreen(null);
        }).bounds(this.widgetX(0, 3), 210, widgetSize(3), 20).build());
        this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, button -> this.onDone()).bounds(this.widgetX(1, 3), 210, widgetSize(3), 20).build());
        this.addRenderableWidget(
            Button.builder(CommonComponents.GUI_CANCEL, button -> this.onCancel()).bounds(this.widgetX(2, 3), 210, widgetSize(3), 20).build()
        );
        this.updateTestInfo(true);
    }
 
    private void updateSaveState() {
        boolean allowSaving = this.rotationButton.getValue() == Rotation.NONE && Identifier.tryParse(this.idEdit.getValue()) != null;
        this.saveButton.active = allowSaving;
        if (this.exportButton != null) {
            this.exportButton.active = allowSaving;
        }
    }
 
    private static Component rotationDisplay(Rotation rotation) {
        return Component.literal(switch (rotation) {
            case NONE -> "0";
            case CLOCKWISE_90 -> "90";
            case CLOCKWISE_180 -> "180";
            case COUNTERCLOCKWISE_90 -> "270";
        });
    }
 
    private void setSize(Vec3i size) {
        this.sizeXEdit.setValue(Integer.toString(size.getX()));
        this.sizeYEdit.setValue(Integer.toString(size.getY()));
        this.sizeZEdit.setValue(Integer.toString(size.getZ()));
    }
 
    private int widgetX(int index, int count) {
        int leftEdge = this.width / 2 - 158;
        float buttonSize = exactWidgetSize(count);
        return (int)(leftEdge + index * (8.0F + buttonSize));
    }
 
    private static int widgetSize(int count) {
        return (int)exactWidgetSize(count);
    }
 
    private static float exactWidgetSize(int count) {
        return (float)(316 - (count - 1) * 8) / count;
    }
 
    @Override
    public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
        super.extractRenderState(graphics, mouseX, mouseY, a);
        int leftEdge = this.width / 2 - 158;
        graphics.centeredText(this.font, this.title, this.width / 2, 10, -1);
        graphics.text(this.font, ID_LABEL, leftEdge, 30, -6250336);
        graphics.text(this.font, SIZE_LABEL, leftEdge, 150, -6250336);
        graphics.text(this.font, ROTATION_LABEL, this.rotationButton.getX(), 150, -6250336);
        graphics.text(this.font, INCLUDE_ENTITIES_LABEL, this.includeEntitiesButton.getX(), 150, -6250336);
    }
 
    private void updateTestInfo(boolean isInit) {
        boolean valid = this.sendToServer(isInit ? ServerboundTestInstanceBlockActionPacket.Action.INIT : ServerboundTestInstanceBlockActionPacket.Action.QUERY);
        if (!valid) {
            this.infoWidget.setMessage(Component.translatable("test_instance.description.invalid_id").withStyle(ChatFormatting.RED));
        }
 
        this.updateSaveState();
    }
 
    private void onDone() {
        this.sendToServer(ServerboundTestInstanceBlockActionPacket.Action.SET);
        this.onClose();
    }
 
    private boolean sendToServer(ServerboundTestInstanceBlockActionPacket.Action action) {
        Optional<Identifier> id = Optional.ofNullable(Identifier.tryParse(this.idEdit.getValue()));
        Optional<ResourceKey<GameTestInstance>> key = id.map(i -> ResourceKey.create(Registries.TEST_INSTANCE, i));
        Vec3i size = new Vec3i(parseSize(this.sizeXEdit.getValue()), parseSize(this.sizeYEdit.getValue()), parseSize(this.sizeZEdit.getValue()));
        boolean ignoreEntities = !this.includeEntitiesButton.getValue();
        this.minecraft
            .getConnection()
            .send(
                new ServerboundTestInstanceBlockActionPacket(this.blockEntity.getBlockPos(), action, key, size, this.rotationButton.getValue(), ignoreEntities)
            );
        return id.isPresent();
    }
 
    public void setStatus(Component status, Optional<Vec3i> size) {
        MutableComponent description = Component.empty();
        this.blockEntity
            .errorMessage()
            .ifPresent(
                error -> description.append(
                        Component.translatable("test_instance.description.failed", Component.empty().withStyle(ChatFormatting.RED).append(error))
                    )
                    .append("\n\n")
            );
        description.append(status);
        this.infoWidget.setMessage(description);
        size.ifPresent(this::setSize);
    }
 
    private void onCancel() {
        this.onClose();
    }
 
    private static int parseSize(String value) {
        try {
            return Mth.clamp(Integer.parseInt(value), 1, 48);
        } catch (NumberFormatException var2) {
            return 1;
        }
    }
 
    @Override
    public boolean isInGameUi() {
        return true;
    }
}

引用的其他类

  • GuiGraphicsExtractor

    • 引用位置: 参数
  • Button

    • 引用位置: 字段/方法调用
    • 关联成员: Button.builder()
  • CycleButton

    • 引用位置: 字段/方法调用
    • 关联成员: CycleButton.builder(), CycleButton.onOffBuilder()
  • EditBox

    • 引用位置: 字段/构造调用
    • 关联成员: EditBox()
  • FittingMultiLineTextWidget

    • 引用位置: 字段/构造调用
    • 关联成员: FittingMultiLineTextWidget()
  • Screen

    • 引用位置: 继承
  • Vec3i

    • 引用位置: 参数/构造调用
    • 关联成员: Vec3i()
  • Component

    • 引用位置: 参数/字段/方法调用/返回值
    • 关联成员: Component.empty(), Component.literal(), Component.translatable()
  • ServerboundTestInstanceBlockActionPacket

    • 引用位置: 参数/构造调用
    • 关联成员: ServerboundTestInstanceBlockActionPacket()
  • Identifier

    • 引用位置: 方法调用
    • 关联成员: Identifier.tryParse()
  • ResourceKey

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

    • 引用位置: 方法调用
    • 关联成员: Mth.clamp()
  • Rotation

    • 引用位置: 参数/字段/方法调用
    • 关联成员: Rotation.values()
  • TestInstanceBlockEntity

    • 引用位置: 参数/字段