| 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.gui.GuiGraphicsExtractor;
|
| 6 | import net.minecraft.client.gui.components.EditBox;
|
| 7 | import net.minecraft.client.input.KeyEvent;
|
| 8 | import net.minecraft.client.renderer.RenderPipelines;
|
| 9 | import net.minecraft.core.component.DataComponents;
|
| 10 | import net.minecraft.network.chat.Component;
|
| 11 | import net.minecraft.network.protocol.game.ServerboundRenameItemPacket;
|
| 12 | import net.minecraft.resources.Identifier;
|
| 13 | import net.minecraft.world.entity.player.Inventory;
|
| 14 | import net.minecraft.world.entity.player.Player;
|
| 15 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 16 | import net.minecraft.world.inventory.AnvilMenu;
|
| 17 | import net.minecraft.world.inventory.Slot;
|
| 18 | import net.minecraft.world.item.ItemStack;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class AnvilScreen extends ItemCombinerScreen<AnvilMenu> {
|
| 22 | private static final Identifier TEXT_FIELD_SPRITE = Identifier.withDefaultNamespace("container/anvil/text_field");
|
| 23 | private static final Identifier TEXT_FIELD_DISABLED_SPRITE = Identifier.withDefaultNamespace("container/anvil/text_field_disabled");
|
| 24 | private static final Identifier ERROR_SPRITE = Identifier.withDefaultNamespace("container/anvil/error");
|
| 25 | private static final Identifier ANVIL_LOCATION = Identifier.withDefaultNamespace("textures/gui/container/anvil.png");
|
| 26 | private static final Component TOO_EXPENSIVE_TEXT = Component.translatable("container.repair.expensive");
|
| 27 | private EditBox name;
|
| 28 | private final Player player;
|
| 29 |
|
| 30 | public AnvilScreen(final AnvilMenu menu, final Inventory inventory, final Component title) {
|
| 31 | super(menu, inventory, title, ANVIL_LOCATION);
|
| 32 | this.player = inventory.player;
|
| 33 | this.titleLabelX = 60;
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | protected void subInit() {
|
| 38 | int xo = (this.width - this.imageWidth) / 2;
|
| 39 | int yo = (this.height - this.imageHeight) / 2;
|
| 40 | this.name = new EditBox(this.font, xo + 62, yo + 24, 103, 12, Component.translatable("container.repair"));
|
| 41 | this.name.setCanLoseFocus(false);
|
| 42 | this.name.setTextColor(-1);
|
| 43 | this.name.setTextColorUneditable(-1);
|
| 44 | this.name.setInvertHighlightedTextColor(false);
|
| 45 | this.name.setBordered(false);
|
| 46 | this.name.setMaxLength(50);
|
| 47 | this.name.setResponder(this::onNameChanged);
|
| 48 | this.name.setValue("");
|
| 49 | this.addRenderableWidget(this.name);
|
| 50 | this.name.setEditable(this.menu.getSlot(0).hasItem());
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | protected void containerTick() {
|
| 55 | super.containerTick();
|
| 56 | this.minecraft.player.experienceDisplayStartTick = this.minecraft.player.tickCount;
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected void setInitialFocus() {
|
| 61 | this.setInitialFocus(this.name);
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public void resize(final int width, final int height) {
|
| 66 | String oldEdit = this.name.getValue();
|
| 67 | this.init(width, height);
|
| 68 | this.name.setValue(oldEdit);
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public boolean keyPressed(final KeyEvent event) {
|
| 73 | if (event.isEscape()) {
|
| 74 | this.minecraft.player.closeContainer();
|
| 75 | return true;
|
| 76 | } else {
|
| 77 | return !this.name.keyPressed(event) && !this.name.canConsumeInput() ? super.keyPressed(event) : true;
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | private void onNameChanged(final String name) {
|
| 82 | Slot slot = this.menu.getSlot(0);
|
| 83 | if (slot.hasItem()) {
|
| 84 | String newName = name;
|
| 85 | if (!slot.getItem().has(DataComponents.CUSTOM_NAME) && newName.equals(slot.getItem().getHoverName().getString())) {
|
| 86 | newName = "";
|
| 87 | }
|
| 88 |
|
| 89 | if (this.menu.setItemName(newName)) {
|
| 90 | this.minecraft.player.connection.send(new ServerboundRenameItemPacket(newName));
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | protected void extractLabels(final GuiGraphicsExtractor graphics, final int xm, final int ym) {
|
| 97 | super.extractLabels(graphics, xm, ym);
|
| 98 | int cost = this.menu.getCost();
|
| 99 | if (cost > 0) {
|
| 100 | int color = -8323296;
|
| 101 | Component line;
|
| 102 | if (cost >= 40 && !this.minecraft.player.hasInfiniteMaterials()) {
|
| 103 | line = TOO_EXPENSIVE_TEXT;
|
| 104 | color = -40864;
|
| 105 | } else if (!this.menu.getSlot(2).hasItem()) {
|
| 106 | line = null;
|
| 107 | } else {
|
| 108 | line = Component.translatable("container.repair.cost", cost);
|
| 109 | if (!this.menu.getSlot(2).mayPickup(this.player)) {
|
| 110 | color = -40864;
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | if (line != null) {
|
| 115 | int tx = this.imageWidth - 8 - this.font.width(line) - 2;
|
| 116 | int ty = 69;
|
| 117 | graphics.fill(tx - 2, 67, this.imageWidth - 8, 79, 1325400064);
|
| 118 | graphics.text(this.font, line, tx, 69, color);
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 125 | super.extractBackground(graphics, mouseX, mouseY, a);
|
| 126 | graphics.blitSprite(
|
| 127 | RenderPipelines.GUI_TEXTURED, this.menu.getSlot(0).hasItem() ? TEXT_FIELD_SPRITE : TEXT_FIELD_DISABLED_SPRITE, this.leftPos + 59, this.topPos + 20, 110, 16
|
| 128 | );
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | protected void extractErrorIcon(final GuiGraphicsExtractor graphics, final int xo, final int yo) {
|
| 133 | if ((this.menu.getSlot(0).hasItem() || this.menu.getSlot(1).hasItem()) && !this.menu.getSlot(this.menu.getResultSlot()).hasItem()) {
|
| 134 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ERROR_SPRITE, xo + 99, yo + 45, 28, 21);
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | public void slotChanged(final AbstractContainerMenu container, final int slotIndex, final ItemStack itemStack) {
|
| 140 | if (slotIndex == 0) {
|
| 141 | this.name.setValue(itemStack.isEmpty() ? "" : itemStack.getHoverName().getString());
|
| 142 | this.name.setEditable(!itemStack.isEmpty());
|
| 143 | this.setFocused(this.name);
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|