| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.GameNarrator;
|
| 6 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 7 | import net.minecraft.client.gui.components.AbstractSliderButton;
|
| 8 | import net.minecraft.client.gui.components.Button;
|
| 9 | import net.minecraft.client.gui.components.CycleButton;
|
| 10 | import net.minecraft.client.gui.components.EditBox;
|
| 11 | import net.minecraft.client.gui.components.Tooltip;
|
| 12 | import net.minecraft.client.gui.screens.Screen;
|
| 13 | import net.minecraft.client.input.KeyEvent;
|
| 14 | import net.minecraft.network.chat.CommonComponents;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.network.protocol.game.ServerboundJigsawGeneratePacket;
|
| 17 | import net.minecraft.network.protocol.game.ServerboundSetJigsawBlockPacket;
|
| 18 | import net.minecraft.resources.Identifier;
|
| 19 | import net.minecraft.util.Mth;
|
| 20 | import net.minecraft.world.level.block.JigsawBlock;
|
| 21 | import net.minecraft.world.level.block.entity.JigsawBlockEntity;
|
| 22 | import net.minecraft.world.level.block.entity.JigsawBlockEntity.JointType;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public class JigsawBlockEditScreen extends Screen {
|
| 26 | private static final Component JOINT_LABEL = Component.translatable("jigsaw_block.joint_label");
|
| 27 | private static final Component POOL_LABEL = Component.translatable("jigsaw_block.pool");
|
| 28 | private static final Component NAME_LABEL = Component.translatable("jigsaw_block.name");
|
| 29 | private static final Component TARGET_LABEL = Component.translatable("jigsaw_block.target");
|
| 30 | private static final Component FINAL_STATE_LABEL = Component.translatable("jigsaw_block.final_state");
|
| 31 | private static final Component PLACEMENT_PRIORITY_LABEL = Component.translatable("jigsaw_block.placement_priority");
|
| 32 | private static final Component PLACEMENT_PRIORITY_TOOLTIP = Component.translatable("jigsaw_block.placement_priority.tooltip");
|
| 33 | private static final Component SELECTION_PRIORITY_LABEL = Component.translatable("jigsaw_block.selection_priority");
|
| 34 | private static final Component SELECTION_PRIORITY_TOOLTIP = Component.translatable("jigsaw_block.selection_priority.tooltip");
|
| 35 | private final JigsawBlockEntity jigsawEntity;
|
| 36 | private EditBox nameEdit;
|
| 37 | private EditBox targetEdit;
|
| 38 | private EditBox poolEdit;
|
| 39 | private EditBox finalStateEdit;
|
| 40 | private EditBox selectionPriorityEdit;
|
| 41 | private EditBox placementPriorityEdit;
|
| 42 | private int levels;
|
| 43 | private boolean keepJigsaws = true;
|
| 44 | private CycleButton<JointType> jointButton;
|
| 45 | private Button doneButton;
|
| 46 | private Button generateButton;
|
| 47 | private JointType joint;
|
| 48 |
|
| 49 | public JigsawBlockEditScreen(final JigsawBlockEntity jigsawEntity) {
|
| 50 | super(GameNarrator.NO_TITLE);
|
| 51 | this.jigsawEntity = jigsawEntity;
|
| 52 | }
|
| 53 |
|
| 54 | private void onDone() {
|
| 55 | this.sendToServer();
|
| 56 | this.minecraft.gui.setScreen(null);
|
| 57 | }
|
| 58 |
|
| 59 | private void onCancel() {
|
| 60 | this.minecraft.gui.setScreen(null);
|
| 61 | }
|
| 62 |
|
| 63 | private void sendToServer() {
|
| 64 | this.minecraft
|
| 65 | .getConnection()
|
| 66 | .send(
|
| 67 | new ServerboundSetJigsawBlockPacket(
|
| 68 | this.jigsawEntity.getBlockPos(),
|
| 69 | Identifier.parse(this.nameEdit.getValue()),
|
| 70 | Identifier.parse(this.targetEdit.getValue()),
|
| 71 | Identifier.parse(this.poolEdit.getValue()),
|
| 72 | this.finalStateEdit.getValue(),
|
| 73 | this.joint,
|
| 74 | this.parseAsInt(this.selectionPriorityEdit.getValue()),
|
| 75 | this.parseAsInt(this.placementPriorityEdit.getValue())
|
| 76 | )
|
| 77 | );
|
| 78 | }
|
| 79 |
|
| 80 | private int parseAsInt(final String value) {
|
| 81 | try {
|
| 82 | return Integer.parseInt(value);
|
| 83 | } catch (NumberFormatException ignored) {
|
| 84 | return 0;
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | private void sendGenerate() {
|
| 89 | this.minecraft.getConnection().send(new ServerboundJigsawGeneratePacket(this.jigsawEntity.getBlockPos(), this.levels, this.keepJigsaws));
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public void onClose() {
|
| 94 | this.onCancel();
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | protected void init() {
|
| 99 | this.poolEdit = new EditBox(this.font, this.width / 2 - 153, 20, 300, 20, POOL_LABEL);
|
| 100 | this.poolEdit.setMaxLength(128);
|
| 101 | this.poolEdit.setValue(this.jigsawEntity.getPool().identifier().toString());
|
| 102 | this.poolEdit.setResponder(value -> this.updateValidity());
|
| 103 | this.addWidget(this.poolEdit);
|
| 104 | this.nameEdit = new EditBox(this.font, this.width / 2 - 153, 55, 300, 20, NAME_LABEL);
|
| 105 | this.nameEdit.setMaxLength(128);
|
| 106 | this.nameEdit.setValue(this.jigsawEntity.getName().toString());
|
| 107 | this.nameEdit.setResponder(value -> this.updateValidity());
|
| 108 | this.addWidget(this.nameEdit);
|
| 109 | this.targetEdit = new EditBox(this.font, this.width / 2 - 153, 90, 300, 20, TARGET_LABEL);
|
| 110 | this.targetEdit.setMaxLength(128);
|
| 111 | this.targetEdit.setValue(this.jigsawEntity.getTarget().toString());
|
| 112 | this.targetEdit.setResponder(value -> this.updateValidity());
|
| 113 | this.addWidget(this.targetEdit);
|
| 114 | this.finalStateEdit = new EditBox(this.font, this.width / 2 - 153, 125, 300, 20, FINAL_STATE_LABEL);
|
| 115 | this.finalStateEdit.setMaxLength(256);
|
| 116 | this.finalStateEdit.setValue(this.jigsawEntity.getFinalState());
|
| 117 | this.addWidget(this.finalStateEdit);
|
| 118 | this.selectionPriorityEdit = new EditBox(this.font, this.width / 2 - 153, 160, 98, 20, SELECTION_PRIORITY_LABEL);
|
| 119 | this.selectionPriorityEdit.setMaxLength(3);
|
| 120 | this.selectionPriorityEdit.setValue(Integer.toString(this.jigsawEntity.getSelectionPriority()));
|
| 121 | this.selectionPriorityEdit.setTooltip(Tooltip.create(SELECTION_PRIORITY_TOOLTIP));
|
| 122 | this.addWidget(this.selectionPriorityEdit);
|
| 123 | this.placementPriorityEdit = new EditBox(this.font, this.width / 2 - 50, 160, 98, 20, PLACEMENT_PRIORITY_LABEL);
|
| 124 | this.placementPriorityEdit.setMaxLength(3);
|
| 125 | this.placementPriorityEdit.setValue(Integer.toString(this.jigsawEntity.getPlacementPriority()));
|
| 126 | this.placementPriorityEdit.setTooltip(Tooltip.create(PLACEMENT_PRIORITY_TOOLTIP));
|
| 127 | this.addWidget(this.placementPriorityEdit);
|
| 128 | this.joint = this.jigsawEntity.getJoint();
|
| 129 | this.jointButton = this.addRenderableWidget(
|
| 130 | CycleButton.builder(JointType::getTranslatedName, this.joint)
|
| 131 | .withValues(JointType.values())
|
| 132 | .displayOnlyValue()
|
| 133 | .create(this.width / 2 + 54, 160, 100, 20, JOINT_LABEL, (button, value) -> this.joint = value)
|
| 134 | );
|
| 135 | boolean vertical = JigsawBlock.getFrontFacing(this.jigsawEntity.getBlockState()).getAxis().isVertical();
|
| 136 | this.jointButton.active = vertical;
|
| 137 | this.jointButton.visible = vertical;
|
| 138 | this.addRenderableWidget(new AbstractSliderButton(this.width / 2 - 154, 185, 100, 20, CommonComponents.EMPTY, 0.0) {
|
| 139 | {
|
| 140 | this.updateMessage();
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | protected void updateMessage() {
|
| 145 | this.setMessage(Component.translatable("jigsaw_block.levels", JigsawBlockEditScreen.this.levels));
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | protected void applyValue() {
|
| 150 | JigsawBlockEditScreen.this.levels = Mth.floor(Mth.clampedLerp(this.value, 0.0, 20.0));
|
| 151 | }
|
| 152 | });
|
| 153 | this.addRenderableWidget(
|
| 154 | CycleButton.onOffBuilder(this.keepJigsaws)
|
| 155 | .create(this.width / 2 - 50, 185, 100, 20, Component.translatable("jigsaw_block.keep_jigsaws"), (button, value) -> this.keepJigsaws = value)
|
| 156 | );
|
| 157 | this.generateButton = this.addRenderableWidget(Button.builder(Component.translatable("jigsaw_block.generate"), button -> {
|
| 158 | this.onDone();
|
| 159 | this.sendGenerate();
|
| 160 | }).bounds(this.width / 2 + 54, 185, 100, 20).build());
|
| 161 | this.doneButton = this.addRenderableWidget(
|
| 162 | Button.builder(CommonComponents.GUI_DONE, button -> this.onDone()).bounds(this.width / 2 - 4 - 150, 210, 150, 20).build()
|
| 163 | );
|
| 164 | this.addRenderableWidget(Button.builder(CommonComponents.GUI_CANCEL, button -> this.onCancel()).bounds(this.width / 2 + 4, 210, 150, 20).build());
|
| 165 | this.updateValidity();
|
| 166 | }
|
| 167 |
|
| 168 | @Override
|
| 169 | protected void setInitialFocus() {
|
| 170 | this.setInitialFocus(this.poolEdit);
|
| 171 | }
|
| 172 |
|
| 173 | public static boolean isValidIdentifier(final String location) {
|
| 174 | return Identifier.tryParse(location) != null;
|
| 175 | }
|
| 176 |
|
| 177 | private void updateValidity() {
|
| 178 | boolean isValid = isValidIdentifier(this.nameEdit.getValue()) && isValidIdentifier(this.targetEdit.getValue()) && isValidIdentifier(this.poolEdit.getValue());
|
| 179 | this.doneButton.active = isValid;
|
| 180 | this.generateButton.active = isValid;
|
| 181 | }
|
| 182 |
|
| 183 | @Override
|
| 184 | public boolean isInGameUi() {
|
| 185 | return true;
|
| 186 | }
|
| 187 |
|
| 188 | @Override
|
| 189 | public void resize(final int width, final int height) {
|
| 190 | String oldNameEdit = this.nameEdit.getValue();
|
| 191 | String oldTargetEdit = this.targetEdit.getValue();
|
| 192 | String oldPoolEdit = this.poolEdit.getValue();
|
| 193 | String oldFinalStateEdit = this.finalStateEdit.getValue();
|
| 194 | String oldSelectionPriorityEdit = this.selectionPriorityEdit.getValue();
|
| 195 | String oldPlacementPriorityEdit = this.placementPriorityEdit.getValue();
|
| 196 | int oldLevels = this.levels;
|
| 197 | JointType oldJointType = this.joint;
|
| 198 | this.init(width, height);
|
| 199 | this.nameEdit.setValue(oldNameEdit);
|
| 200 | this.targetEdit.setValue(oldTargetEdit);
|
| 201 | this.poolEdit.setValue(oldPoolEdit);
|
| 202 | this.finalStateEdit.setValue(oldFinalStateEdit);
|
| 203 | this.levels = oldLevels;
|
| 204 | this.joint = oldJointType;
|
| 205 | this.jointButton.setValue(oldJointType);
|
| 206 | this.selectionPriorityEdit.setValue(oldSelectionPriorityEdit);
|
| 207 | this.placementPriorityEdit.setValue(oldPlacementPriorityEdit);
|
| 208 | }
|
| 209 |
|
| 210 | @Override
|
| 211 | public boolean keyPressed(final KeyEvent event) {
|
| 212 | if (super.keyPressed(event)) {
|
| 213 | return true;
|
| 214 | } else if (this.doneButton.active && event.isConfirmation()) {
|
| 215 | this.onDone();
|
| 216 | return true;
|
| 217 | } else {
|
| 218 | return false;
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | @Override
|
| 223 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 224 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 225 | graphics.text(this.font, POOL_LABEL, this.width / 2 - 153, 10, -6250336);
|
| 226 | this.poolEdit.extractRenderState(graphics, mouseX, mouseY, a);
|
| 227 | graphics.text(this.font, NAME_LABEL, this.width / 2 - 153, 45, -6250336);
|
| 228 | this.nameEdit.extractRenderState(graphics, mouseX, mouseY, a);
|
| 229 | graphics.text(this.font, TARGET_LABEL, this.width / 2 - 153, 80, -6250336);
|
| 230 | this.targetEdit.extractRenderState(graphics, mouseX, mouseY, a);
|
| 231 | graphics.text(this.font, FINAL_STATE_LABEL, this.width / 2 - 153, 115, -6250336);
|
| 232 | this.finalStateEdit.extractRenderState(graphics, mouseX, mouseY, a);
|
| 233 | graphics.text(this.font, SELECTION_PRIORITY_LABEL, this.width / 2 - 153, 150, -6250336);
|
| 234 | this.placementPriorityEdit.extractRenderState(graphics, mouseX, mouseY, a);
|
| 235 | graphics.text(this.font, PLACEMENT_PRIORITY_LABEL, this.width / 2 - 50, 150, -6250336);
|
| 236 | this.selectionPriorityEdit.extractRenderState(graphics, mouseX, mouseY, a);
|
| 237 | if (JigsawBlock.getFrontFacing(this.jigsawEntity.getBlockState()).getAxis().isVertical()) {
|
| 238 | graphics.text(this.font, JOINT_LABEL, this.width / 2 + 53, 150, -6250336);
|
| 239 | }
|
| 240 | }
|
| 241 | }
|
| 242 |
|