143 lines
5.2 KiB
Java
143 lines
5.2 KiB
Java
package net.minecraft.client.gui.screens.inventory;
|
|
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.minecraft.client.Minecraft;
|
|
import net.minecraft.client.gui.GuiGraphics;
|
|
import net.minecraft.client.gui.components.EditBox;
|
|
import net.minecraft.client.renderer.RenderType;
|
|
import net.minecraft.core.component.DataComponents;
|
|
import net.minecraft.network.chat.Component;
|
|
import net.minecraft.network.protocol.game.ServerboundRenameItemPacket;
|
|
import net.minecraft.resources.ResourceLocation;
|
|
import net.minecraft.world.entity.player.Inventory;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.inventory.AbstractContainerMenu;
|
|
import net.minecraft.world.inventory.AnvilMenu;
|
|
import net.minecraft.world.inventory.Slot;
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public class AnvilScreen extends ItemCombinerScreen<AnvilMenu> {
|
|
private static final ResourceLocation TEXT_FIELD_SPRITE = ResourceLocation.withDefaultNamespace("container/anvil/text_field");
|
|
private static final ResourceLocation TEXT_FIELD_DISABLED_SPRITE = ResourceLocation.withDefaultNamespace("container/anvil/text_field_disabled");
|
|
private static final ResourceLocation ERROR_SPRITE = ResourceLocation.withDefaultNamespace("container/anvil/error");
|
|
private static final ResourceLocation ANVIL_LOCATION = ResourceLocation.withDefaultNamespace("textures/gui/container/anvil.png");
|
|
private static final Component TOO_EXPENSIVE_TEXT = Component.translatable("container.repair.expensive");
|
|
private EditBox name;
|
|
private final Player player;
|
|
|
|
public AnvilScreen(AnvilMenu menu, Inventory playerInventory, Component title) {
|
|
super(menu, playerInventory, title, ANVIL_LOCATION);
|
|
this.player = playerInventory.player;
|
|
this.titleLabelX = 60;
|
|
}
|
|
|
|
@Override
|
|
protected void subInit() {
|
|
int i = (this.width - this.imageWidth) / 2;
|
|
int j = (this.height - this.imageHeight) / 2;
|
|
this.name = new EditBox(this.font, i + 62, j + 24, 103, 12, Component.translatable("container.repair"));
|
|
this.name.setCanLoseFocus(false);
|
|
this.name.setTextColor(-1);
|
|
this.name.setTextColorUneditable(-1);
|
|
this.name.setBordered(false);
|
|
this.name.setMaxLength(50);
|
|
this.name.setResponder(this::onNameChanged);
|
|
this.name.setValue("");
|
|
this.addWidget(this.name);
|
|
this.name.setEditable(this.menu.getSlot(0).hasItem());
|
|
}
|
|
|
|
@Override
|
|
protected void setInitialFocus() {
|
|
this.setInitialFocus(this.name);
|
|
}
|
|
|
|
@Override
|
|
public void resize(Minecraft minecraft, int width, int height) {
|
|
String string = this.name.getValue();
|
|
this.init(minecraft, width, height);
|
|
this.name.setValue(string);
|
|
}
|
|
|
|
@Override
|
|
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
|
|
if (keyCode == 256) {
|
|
this.minecraft.player.closeContainer();
|
|
}
|
|
|
|
return !this.name.keyPressed(keyCode, scanCode, modifiers) && !this.name.canConsumeInput() ? super.keyPressed(keyCode, scanCode, modifiers) : true;
|
|
}
|
|
|
|
private void onNameChanged(String name) {
|
|
Slot slot = this.menu.getSlot(0);
|
|
if (slot.hasItem()) {
|
|
String string = name;
|
|
if (!slot.getItem().has(DataComponents.CUSTOM_NAME) && name.equals(slot.getItem().getHoverName().getString())) {
|
|
string = "";
|
|
}
|
|
|
|
if (this.menu.setItemName(string)) {
|
|
this.minecraft.player.connection.send(new ServerboundRenameItemPacket(string));
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void renderLabels(GuiGraphics guiGraphics, int mouseX, int mouseY) {
|
|
super.renderLabels(guiGraphics, mouseX, mouseY);
|
|
int i = this.menu.getCost();
|
|
if (i > 0) {
|
|
int j = 8453920;
|
|
Component component;
|
|
if (i >= 40 && !this.minecraft.player.hasInfiniteMaterials()) {
|
|
component = TOO_EXPENSIVE_TEXT;
|
|
j = 16736352;
|
|
} else if (!this.menu.getSlot(2).hasItem()) {
|
|
component = null;
|
|
} else {
|
|
component = Component.translatable("container.repair.cost", i);
|
|
if (!this.menu.getSlot(2).mayPickup(this.player)) {
|
|
j = 16736352;
|
|
}
|
|
}
|
|
|
|
if (component != null) {
|
|
int k = this.imageWidth - 8 - this.font.width(component) - 2;
|
|
int l = 69;
|
|
guiGraphics.fill(k - 2, 67, this.imageWidth - 8, 79, 1325400064);
|
|
guiGraphics.drawString(this.font, component, k, 69, j);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void renderBg(GuiGraphics guiGraphics, float partialTick, int mouseX, int mouseY) {
|
|
super.renderBg(guiGraphics, partialTick, mouseX, mouseY);
|
|
guiGraphics.blitSprite(
|
|
RenderType::guiTextured, this.menu.getSlot(0).hasItem() ? TEXT_FIELD_SPRITE : TEXT_FIELD_DISABLED_SPRITE, this.leftPos + 59, this.topPos + 20, 110, 16
|
|
);
|
|
}
|
|
|
|
@Override
|
|
public void renderFg(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
|
|
this.name.render(guiGraphics, mouseX, mouseY, partialTick);
|
|
}
|
|
|
|
@Override
|
|
protected void renderErrorIcon(GuiGraphics guiGraphics, int x, int y) {
|
|
if ((this.menu.getSlot(0).hasItem() || this.menu.getSlot(1).hasItem()) && !this.menu.getSlot(this.menu.getResultSlot()).hasItem()) {
|
|
guiGraphics.blitSprite(RenderType::guiTextured, ERROR_SPRITE, x + 99, y + 45, 28, 21);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void slotChanged(AbstractContainerMenu containerToSend, int dataSlotIndex, ItemStack stack) {
|
|
if (dataSlotIndex == 0) {
|
|
this.name.setValue(stack.isEmpty() ? "" : stack.getHoverName().getString());
|
|
this.name.setEditable(!stack.isEmpty());
|
|
this.setFocused(this.name);
|
|
}
|
|
}
|
|
}
|