Преглед изворни кода

干掉了铁砧的文本框

LTCatt пре 6 година
родитељ
комит
a5a2755b32

+ 4 - 34
src/main/java/com/hexdragon/enchrebirth/block/AnvilContainerRe.java

@@ -1,5 +1,6 @@
 package com.hexdragon.enchrebirth.block;
 
+import com.hexdragon.enchrebirth.registry.RegMain;
 import net.minecraft.block.AnvilBlock;
 import net.minecraft.block.BlockState;
 import net.minecraft.enchantment.Enchantment;
@@ -8,19 +9,16 @@ import net.minecraft.entity.player.PlayerEntity;
 import net.minecraft.entity.player.PlayerInventory;
 import net.minecraft.inventory.IInventory;
 import net.minecraft.inventory.container.AbstractRepairContainer;
-import net.minecraft.inventory.container.ContainerType;
 import net.minecraft.item.EnchantedBookItem;
 import net.minecraft.item.ItemStack;
 import net.minecraft.item.Items;
 import net.minecraft.tags.BlockTags;
 import net.minecraft.util.IWorldPosCallable;
 import net.minecraft.util.IntReferenceHolder;
-import net.minecraft.util.text.StringTextComponent;
 import net.minecraftforge.api.distmarker.Dist;
 import net.minecraftforge.api.distmarker.OnlyIn;
 import net.minecraftforge.common.MinecraftForge;
 import net.minecraftforge.event.AnvilUpdateEvent;
-import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
@@ -30,7 +28,6 @@ import java.util.Map;
 public class AnvilContainerRe extends AbstractRepairContainer {
     private static final Logger LOGGER = LogManager.getLogger();
     public int materialCost;
-    private String repairedItemName;
     private final IntReferenceHolder maximumCost = IntReferenceHolder.single();
 
     public AnvilContainerRe(int id, PlayerInventory playerInventory) {
@@ -38,7 +35,7 @@ public class AnvilContainerRe extends AbstractRepairContainer {
     }
 
     public AnvilContainerRe(int id, PlayerInventory playerInventory, IWorldPosCallable worldPosCallable) {
-        super(ContainerType.ANVIL, id, playerInventory, worldPosCallable);
+        super(RegMain.containerAnvil.get(), id, playerInventory, worldPosCallable);
         this.trackInt(this.maximumCost);
     }
 
@@ -111,7 +108,8 @@ public class AnvilContainerRe extends AbstractRepairContainer {
             boolean flag = false;
 
             if (!itemstack2.isEmpty()) {
-                if (!onAnvilChange(this, itemstack, itemstack2, field_234642_c_, repairedItemName, j)) return;
+                if (!onAnvilChange(this, itemstack, itemstack2, field_234642_c_, itemstack.getDisplayName().getString(), j))
+                    return;
                 flag = itemstack2.getItem() == Items.ENCHANTED_BOOK && !EnchantedBookItem.getEnchantments(itemstack2).isEmpty();
                 if (itemstack1.isDamageable() && itemstack1.getItem().getIsRepairable(itemstack, itemstack2)) {
                     int l2 = Math.min(itemstack1.getDamage(), itemstack1.getMaxDamage() / 4);
@@ -218,17 +216,6 @@ public class AnvilContainerRe extends AbstractRepairContainer {
                 }
             }
 
-            if (StringUtils.isBlank(this.repairedItemName)) {
-                if (itemstack.hasDisplayName()) {
-                    k = 1;
-                    i += k;
-                    itemstack1.clearCustomName();
-                }
-            } else if (!this.repairedItemName.equals(itemstack.getDisplayName().getString())) {
-                k = 1;
-                i += k;
-                itemstack1.setDisplayName(new StringTextComponent(this.repairedItemName));
-            }
             if (flag && !itemstack1.isBookEnchantable(itemstack2)) itemstack1 = ItemStack.EMPTY;
 
             this.maximumCost.set(j + i);
@@ -279,23 +266,6 @@ public class AnvilContainerRe extends AbstractRepairContainer {
         return oldRepairCost * 2 + 1;
     }
 
-    /**
-     * used by the Anvil GUI to update the Item Name being typed by the player
-     */
-    public void updateItemName(String newName) {
-        this.repairedItemName = newName;
-        if (this.getSlot(2).getHasStack()) {
-            ItemStack itemstack = this.getSlot(2).getStack();
-            if (StringUtils.isBlank(newName)) {
-                itemstack.clearCustomName();
-            } else {
-                itemstack.setDisplayName(new StringTextComponent(this.repairedItemName));
-            }
-        }
-
-        this.updateRepairOutput();
-    }
-
     /**
      * Get's the maximum xp cost
      */

+ 23 - 65
src/main/java/com/hexdragon/enchrebirth/block/AnvilScreenRe.java

@@ -3,13 +3,8 @@ package com.hexdragon.enchrebirth.block;
 import com.mojang.blaze3d.matrix.MatrixStack;
 import com.mojang.blaze3d.systems.RenderSystem;
 import net.minecraft.client.Minecraft;
-import net.minecraft.client.gui.screen.inventory.AbstractRepairScreen;
-import net.minecraft.client.gui.widget.TextFieldWidget;
+import net.minecraft.client.gui.screen.inventory.ContainerScreen;
 import net.minecraft.entity.player.PlayerInventory;
-import net.minecraft.inventory.container.Container;
-import net.minecraft.inventory.container.Slot;
-import net.minecraft.item.ItemStack;
-import net.minecraft.network.play.client.CRenameItemPacket;
 import net.minecraft.util.ResourceLocation;
 import net.minecraft.util.text.ITextComponent;
 import net.minecraft.util.text.TranslationTextComponent;
@@ -17,61 +12,40 @@ import net.minecraftforge.api.distmarker.Dist;
 import net.minecraftforge.api.distmarker.OnlyIn;
 
 @OnlyIn(Dist.CLIENT)
-public class AnvilScreenRe extends AbstractRepairScreen<AnvilContainerRe> {
-    private static final ResourceLocation ANVIL_RESOURCE = new ResourceLocation("textures/gui/container/anvil.png");
-    private static final ITextComponent field_243333_B = new TranslationTextComponent("container.repair.expensive");
-    private TextFieldWidget nameField;
+public class AnvilScreenRe extends ContainerScreen<AnvilContainerRe> {
 
     public AnvilScreenRe(AnvilContainerRe container, PlayerInventory playerInventory, ITextComponent title) {
-        super(container, playerInventory, title, ANVIL_RESOURCE);
+        super(container, playerInventory, title);
+        this.playerInventoryTitleY = 1000000; // 关闭 “物品栏” 三个字的显示
         this.titleX = 60;
     }
 
-    protected void initFields() {
-        this.minecraft.keyboardListener.enableRepeatEvents(true);
-        int i = (this.width - this.xSize) / 2;
-        int j = (this.height - this.ySize) / 2;
-        this.nameField = new TextFieldWidget(this.font, i + 62, j + 24, 103, 12, new TranslationTextComponent("container.repair"));
-        this.nameField.setCanLoseFocus(false);
-        this.nameField.setTextColor(-1);
-        this.nameField.setDisabledTextColour(-1);
-        this.nameField.setEnableBackgroundDrawing(false);
-        this.nameField.setMaxStringLength(35);
-        this.nameField.setResponder(this::renameItem);
-        this.children.add(this.nameField);
-        this.setFocusedDefault(this.nameField);
-    }
-
-    public void resize(Minecraft minecraft, int width, int height) {
-        String s = this.nameField.getText();
-        this.init(minecraft, width, height);
-        this.nameField.setText(s);
-    }
-
-    public void onClose() {
-        super.onClose();
-        this.minecraft.keyboardListener.enableRepeatEvents(false);
+    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
+        this.renderBackground(matrixStack);
+        super.render(matrixStack, mouseX, mouseY, partialTicks);
+        RenderSystem.disableBlend();
+        this.renderHoveredTooltip(matrixStack, mouseX, mouseY);
     }
 
-    public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
-        if (keyCode == 256) {
-            this.minecraft.player.closeScreen();
+    protected void drawGuiContainerBackgroundLayer(MatrixStack matrixStack, float partialTicks, int x, int y) {
+        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
+        this.minecraft.getTextureManager().bindTexture(this.ANVIL_RESOURCE);
+        int i = (this.width - this.xSize) / 2;
+        int j = (this.height - this.ySize) / 2;
+        this.blit(matrixStack, i, j, 0, 0, this.xSize, this.ySize);
+        // this.blit(matrixStack, i + 59, j + 20, 0, this.ySize + (this.container.getSlot(0).getHasStack() ? 0 : 16), 110, 16);
+        // 显示无法合成的红叉:输入不为空但输出为空
+        if ((this.container.getSlot(0).getHasStack() || this.container.getSlot(1).getHasStack()) && !this.container.getSlot(2).getHasStack()) {
+            this.blit(matrixStack, i + 99, j + 45, this.xSize, 0, 28, 21);
         }
 
-        return !this.nameField.keyPressed(keyCode, scanCode, modifiers) && !this.nameField.canWrite() ? super.keyPressed(keyCode, scanCode, modifiers) : true;
     }
 
-    private void renameItem(String name) {
-        if (!name.isEmpty()) {
-            String s = name;
-            Slot slot = this.container.getSlot(0);
-            if (slot != null && slot.getHasStack() && !slot.getStack().hasDisplayName() && name.equals(slot.getStack().getDisplayName().getString())) {
-                s = "";
-            }
+    private static final ResourceLocation ANVIL_RESOURCE = new ResourceLocation("textures/gui/container/anvil.png");
+    private static final ITextComponent field_243333_B = new TranslationTextComponent("container.repair.expensive");
 
-            this.container.updateItemName(s);
-            this.minecraft.player.connection.sendPacket(new CRenameItemPacket(s));
-        }
+    public void resize(Minecraft minecraft, int width, int height) {
+        this.init(minecraft, width, height);
     }
 
     protected void drawGuiContainerForegroundLayer(MatrixStack matrixStack, int x, int y) {
@@ -103,20 +77,4 @@ public class AnvilScreenRe extends AbstractRepairScreen<AnvilContainerRe> {
 
     }
 
-    public void renderNameField(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
-        this.nameField.render(matrixStack, mouseX, mouseY, partialTicks);
-    }
-
-    /**
-     * Sends the contents of an inventory slot to the client-side Container. This doesn't have to match the actual
-     * contents of that slot.
-     */
-    public void sendSlotContents(Container containerToSend, int slotInd, ItemStack stack) {
-        if (slotInd == 0) {
-            this.nameField.setText(stack.isEmpty() ? "" : stack.getDisplayName().getString());
-            this.nameField.setEnabled(!stack.isEmpty());
-            this.setListener(this.nameField);
-        }
-
-    }
 }