TestInstanceBlockEditScreen .java

net.minecraft.client.gui.screens.inventory.TestInstanceBlockEditScreen (class): members: ROTATION_LABEL, rotationButton, exportButton, infoWidget, idEdit.

net.minecraft.client.gui.screens.inventoryclass221 linesclientextends Screen9.5 KB

API Surface (4)

void init()
void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
void setStatus(final Component status, final Optional<Vec3i> size)
boolean isInGameUi()

Fields & Constants (16)

ID_LABEL
SIZE_LABEL
INCLUDE_ENTITIES_LABEL
ROTATION_LABEL
BUTTON_PADDING
WIDTH
blockEntity
idEdit
sizeXEdit
sizeYEdit
sizeZEdit
infoWidget
saveButton
exportButton
includeEntitiesButton
rotationButton

String literals (13)

"test_instance_block.test_id"
"test_instance_block.size"
"test_instance_block.entities"
"test_instance_block.rotation"
"structure_block.size.x"
"structure_block.size.y"
"structure_block.size.z"
"test_instance.action.reset"
"test_instance.action.save"
"Export Structure"
"test_instance.action.run"
"test_instance.description.invalid_id"
"test_instance.description.failed"

Full Source

TestInstanceBlockEditScreen.java221 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.inventory;
2
3
import java.util.Optional;
4
import net.fabricmc.api.EnvType;
5
import net.fabricmc.api.Environment;
6
import net.minecraft.ChatFormatting;
7
import net.minecraft.SharedConstants;
8
import net.minecraft.client.gui.GuiGraphicsExtractor;
9
import net.minecraft.client.gui.components.Button;
10
import net.minecraft.client.gui.components.CycleButton;
11
import net.minecraft.client.gui.components.EditBox;
12
import net.minecraft.client.gui.components.FittingMultiLineTextWidget;
13
import net.minecraft.client.gui.screens.Screen;
14
import net.minecraft.core.Vec3i;
15
import net.minecraft.core.registries.Registries;
16
import net.minecraft.gametest.framework.GameTestInstance;
17
import net.minecraft.network.chat.CommonComponents;
18
import net.minecraft.network.chat.Component;
19
import net.minecraft.network.chat.MutableComponent;
20
import net.minecraft.network.protocol.game.ServerboundTestInstanceBlockActionPacket;
21
import net.minecraft.network.protocol.game.ServerboundTestInstanceBlockActionPacket.Action;
22
import net.minecraft.resources.Identifier;
23
import net.minecraft.resources.ResourceKey;
24
import net.minecraft.util.Mth;
25
import net.minecraft.world.level.block.Rotation;
26
import net.minecraft.world.level.block.entity.TestInstanceBlockEntity;
27
import org.jspecify.annotations.Nullable;
28
29
@Environment(EnvType.CLIENT)
30
public class TestInstanceBlockEditScreen extends Screen {
31
	private static final Component ID_LABEL = Component.translatable("test_instance_block.test_id");
32
	private static final Component SIZE_LABEL = Component.translatable("test_instance_block.size");
33
	private static final Component INCLUDE_ENTITIES_LABEL = Component.translatable("test_instance_block.entities");
34
	private static final Component ROTATION_LABEL = Component.translatable("test_instance_block.rotation");
35
	private static final int BUTTON_PADDING = 8;
36
	private static final int WIDTH = 316;
37
	private final TestInstanceBlockEntity blockEntity;
38
	private @Nullable EditBox idEdit;
39
	private @Nullable EditBox sizeXEdit;
40
	private @Nullable EditBox sizeYEdit;
41
	private @Nullable EditBox sizeZEdit;
42
	private @Nullable FittingMultiLineTextWidget infoWidget;
43
	private @Nullable Button saveButton;
44
	private @Nullable Button exportButton;
45
	private @Nullable CycleButton<Boolean> includeEntitiesButton;
46
	private @Nullable CycleButton<Rotation> rotationButton;
47
48
	public TestInstanceBlockEditScreen(final TestInstanceBlockEntity blockEntity) {
49
		super(blockEntity.getBlockState().getBlock().getName());
50
		this.blockEntity = blockEntity;
51
	}
52
53
	@Override
54
	protected void init() {
55
		int leftEdge = this.width / 2 - 158;
56
		boolean includeExport = SharedConstants.IS_RUNNING_IN_IDE;
57
		int actionButtonCount = includeExport ? 3 : 2;
58
		int buttonSize = widgetSize(actionButtonCount);
59
		this.idEdit = new EditBox(this.font, leftEdge, 40, 316, 20, Component.translatable("test_instance_block.test_id"));
60
		this.idEdit.setMaxLength(128);
61
		Optional<ResourceKey<GameTestInstance>> test = this.blockEntity.test();
62
		if (test.isPresent()) {
63
			this.idEdit.setValue(test.get().identifier().toString());
64
		}
65
66
		this.idEdit.setResponder(s -> this.updateTestInfo(false));
67
		this.addRenderableWidget(this.idEdit);
68
		this.infoWidget = new FittingMultiLineTextWidget(leftEdge, 70, 316, 8 * 9, Component.literal(""), this.font);
69
		this.addRenderableWidget(this.infoWidget);
70
		Vec3i size = this.blockEntity.getSize();
71
		int index = 0;
72
		this.sizeXEdit = new EditBox(this.font, this.widgetX(index++, 5), 160, widgetSize(5), 20, Component.translatable("structure_block.size.x"));
73
		this.sizeXEdit.setMaxLength(15);
74
		this.addRenderableWidget(this.sizeXEdit);
75
		this.sizeYEdit = new EditBox(this.font, this.widgetX(index++, 5), 160, widgetSize(5), 20, Component.translatable("structure_block.size.y"));
76
		this.sizeYEdit.setMaxLength(15);
77
		this.addRenderableWidget(this.sizeYEdit);
78
		this.sizeZEdit = new EditBox(this.font, this.widgetX(index++, 5), 160, widgetSize(5), 20, Component.translatable("structure_block.size.z"));
79
		this.sizeZEdit.setMaxLength(15);
80
		this.addRenderableWidget(this.sizeZEdit);
81
		this.setSize(size);
82
		this.rotationButton = this.addRenderableWidget(
83
			CycleButton.builder(TestInstanceBlockEditScreen::rotationDisplay, this.blockEntity.getRotation())
84
				.withValues(Rotation.values())
85
				.displayOnlyValue()
86
				.create(this.widgetX(index++, 5), 160, widgetSize(5), 20, ROTATION_LABEL, (button, value) -> this.updateSaveState())
87
		);
88
		this.includeEntitiesButton = this.addRenderableWidget(
89
			CycleButton.onOffBuilder(!this.blockEntity.ignoreEntities())
90
				.displayOnlyValue()
91
				.create(this.widgetX(index++, 5), 160, widgetSize(5), 20, INCLUDE_ENTITIES_LABEL)
92
		);
93
		index = 0;
94
		this.addRenderableWidget(Button.builder(Component.translatable("test_instance.action.reset"), button -> {
95
			this.sendToServer(Action.RESET);
96
			this.minecraft.gui.setScreen(null);
97
		}).bounds(this.widgetX(index++, actionButtonCount), 185, buttonSize, 20).build());
98
		this.saveButton = this.addRenderableWidget(Button.builder(Component.translatable("test_instance.action.save"), button -> {
99
			this.sendToServer(Action.SAVE);
100
			this.minecraft.gui.setScreen(null);
101
		}).bounds(this.widgetX(index++, actionButtonCount), 185, buttonSize, 20).build());
102
		if (includeExport) {
103
			this.exportButton = this.addRenderableWidget(Button.builder(Component.literal("Export Structure"), button -> {
104
				this.sendToServer(Action.EXPORT);
105
				this.minecraft.gui.setScreen(null);
106
			}).bounds(this.widgetX(index++, actionButtonCount), 185, buttonSize, 20).build());
107
		}
108
109
		this.addRenderableWidget(Button.builder(Component.translatable("test_instance.action.run"), button -> {
110
			this.sendToServer(Action.RUN);
111
			this.minecraft.gui.setScreen(null);
112
		}).bounds(this.widgetX(0, 3), 210, widgetSize(3), 20).build());
113
		this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, button -> this.onDone()).bounds(this.widgetX(1, 3), 210, widgetSize(3), 20).build());
114
		this.addRenderableWidget(Button.builder(CommonComponents.GUI_CANCEL, button -> this.onCancel()).bounds(this.widgetX(2, 3), 210, widgetSize(3), 20).build());
115
		this.updateTestInfo(true);
116
	}
117
118
	private void updateSaveState() {
119
		boolean allowSaving = this.rotationButton.getValue() == Rotation.NONE && Identifier.tryParse(this.idEdit.getValue()) != null;
120
		this.saveButton.active = allowSaving;
121
		if (this.exportButton != null) {
122
			this.exportButton.active = allowSaving;
123
		}
124
	}
125
126
	private static Component rotationDisplay(final Rotation rotation) {
127
		return Component.literal(switch (rotation) {
128
			case NONE -> "0";
129
			case CLOCKWISE_90 -> "90";
130
			case CLOCKWISE_180 -> "180";
131
			case COUNTERCLOCKWISE_90 -> "270";
132
		});
133
	}
134
135
	private void setSize(final Vec3i size) {
136
		this.sizeXEdit.setValue(Integer.toString(size.getX()));
137
		this.sizeYEdit.setValue(Integer.toString(size.getY()));
138
		this.sizeZEdit.setValue(Integer.toString(size.getZ()));
139
	}
140
141
	private int widgetX(final int index, final int count) {
142
		int leftEdge = this.width / 2 - 158;
143
		float buttonSize = exactWidgetSize(count);
144
		return (int)(leftEdge + index * (8.0F + buttonSize));
145
	}
146
147
	private static int widgetSize(final int count) {
148
		return (int)exactWidgetSize(count);
149
	}
150
151
	private static float exactWidgetSize(final int count) {
152
		return (float)(316 - (count - 1) * 8) / count;
153
	}
154
155
	@Override
156
	public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
157
		super.extractRenderState(graphics, mouseX, mouseY, a);
158
		int leftEdge = this.width / 2 - 158;
159
		graphics.centeredText(this.font, this.title, this.width / 2, 10, -1);
160
		graphics.text(this.font, ID_LABEL, leftEdge, 30, -6250336);
161
		graphics.text(this.font, SIZE_LABEL, leftEdge, 150, -6250336);
162
		graphics.text(this.font, ROTATION_LABEL, this.rotationButton.getX(), 150, -6250336);
163
		graphics.text(this.font, INCLUDE_ENTITIES_LABEL, this.includeEntitiesButton.getX(), 150, -6250336);
164
	}
165
166
	private void updateTestInfo(final boolean isInit) {
167
		boolean valid = this.sendToServer(isInit ? Action.INIT : Action.QUERY);
168
		if (!valid) {
169
			this.infoWidget.setMessage(Component.translatable("test_instance.description.invalid_id").withStyle(ChatFormatting.RED));
170
		}
171
172
		this.updateSaveState();
173
	}
174
175
	private void onDone() {
176
		this.sendToServer(Action.SET);
177
		this.onClose();
178
	}
179
180
	private boolean sendToServer(final Action action) {
181
		Optional<Identifier> id = Optional.ofNullable(Identifier.tryParse(this.idEdit.getValue()));
182
		Optional<ResourceKey<GameTestInstance>> key = id.map(i -> ResourceKey.create(Registries.TEST_INSTANCE, i));
183
		Vec3i size = new Vec3i(parseSize(this.sizeXEdit.getValue()), parseSize(this.sizeYEdit.getValue()), parseSize(this.sizeZEdit.getValue()));
184
		boolean ignoreEntities = !this.includeEntitiesButton.getValue();
185
		this.minecraft
186
			.getConnection()
187
			.send(new ServerboundTestInstanceBlockActionPacket(this.blockEntity.getBlockPos(), action, key, size, this.rotationButton.getValue(), ignoreEntities));
188
		return id.isPresent();
189
	}
190
191
	public void setStatus(final Component status, final Optional<Vec3i> size) {
192
		MutableComponent description = Component.empty();
193
		this.blockEntity
194
			.errorMessage()
195
			.ifPresent(
196
				error -> description.append(Component.translatable("test_instance.description.failed", Component.empty().withStyle(ChatFormatting.RED).append(error)))
197
					.append("\n\n")
198
			);
199
		description.append(status);
200
		this.infoWidget.setMessage(description);
201
		size.ifPresent(this::setSize);
202
	}
203
204
	private void onCancel() {
205
		this.onClose();
206
	}
207
208
	private static int parseSize(final String value) {
209
		try {
210
			return Mth.clamp(Integer.parseInt(value), 1, 48);
211
		} catch (NumberFormatException ignored) {
212
			return 1;
213
		}
214
	}
215
216
	@Override
217
	public boolean isInGameUi() {
218
		return true;
219
	}
220
}
221

Decompiled Minecraft 26.3 source of TestInstanceBlockEditScreen (net/minecraft/client/gui/screens/inventory/TestInstanceBlockEditScreen.java) with Mojang official mappings, stored verbatim on the site.