Răsfoiți Sursa

实际可用的铁砧物品栏

LTCatt 6 ani în urmă
părinte
comite
65b9d93fa7

+ 1 - 2
src/main/java/com/hexdragon/enchrebirth/Main.java

@@ -6,5 +6,4 @@ import org.apache.logging.log4j.Logger;
 public class Main {
 public class Main {
     public static final String MODID = "enchrebirth";
     public static final String MODID = "enchrebirth";
     public static final Logger LOGGER = LogManager.getLogger();
     public static final Logger LOGGER = LogManager.getLogger();
-
-}
+}

+ 2 - 8
src/main/java/com/hexdragon/enchrebirth/block/AnvilContainerRe.java

@@ -3,7 +3,6 @@ package com.hexdragon.enchrebirth.block;
 import com.hexdragon.core.item.CraftInputInventory;
 import com.hexdragon.core.item.CraftInputInventory;
 import com.hexdragon.core.item.EnchantmentHelperRe;
 import com.hexdragon.core.item.EnchantmentHelperRe;
 import com.hexdragon.core.item.ItemHelperRe;
 import com.hexdragon.core.item.ItemHelperRe;
-import com.hexdragon.enchrebirth.Main;
 import com.hexdragon.enchrebirth.registry.RegMain;
 import com.hexdragon.enchrebirth.registry.RegMain;
 import net.minecraft.block.AnvilBlock;
 import net.minecraft.block.AnvilBlock;
 import net.minecraft.block.BlockState;
 import net.minecraft.block.BlockState;
@@ -31,26 +30,21 @@ public class AnvilContainerRe extends Container {
     private final PlayerEntity player;
     private final PlayerEntity player;
     public AnvilContainerRe(int id, PlayerInventory playerInventory) {
     public AnvilContainerRe(int id, PlayerInventory playerInventory) {
         this(id, playerInventory, IWorldPosCallable.DUMMY);
         this(id, playerInventory, IWorldPosCallable.DUMMY);
-        this.inputInventory.container = this;
     }
     }
     public AnvilContainerRe(int id, PlayerInventory playerInventory, IWorldPosCallable worldPosCallable) {
     public AnvilContainerRe(int id, PlayerInventory playerInventory, IWorldPosCallable worldPosCallable) {
         super(RegMain.containerAnvil.get(), id);
         super(RegMain.containerAnvil.get(), id);
         this.worldPosCallable = worldPosCallable;
         this.worldPosCallable = worldPosCallable;
         this.player = playerInventory.player;
         this.player = playerInventory.player;
-        worldPosCallable.consume((world, blockPos) -> {
-            this.inputInventory = ((AnvilTileEntity) world.getTileEntity(blockPos)).getInventory();
-            if (this.inputInventory == null) this.inputInventory = new CraftInputInventory(2);
-        });
+        worldPosCallable.consume((world, blockPos) -> this.inputInventory = ((AnvilTileEntity) world.getTileEntity(blockPos)).inventory); // 与 NBT 中的物品栏同步
         Constuct(playerInventory);
         Constuct(playerInventory);
     }
     }
 
 
     // 输入与输出物品槽
     // 输入与输出物品槽
-    public CraftInputInventory inputInventory;
+    public CraftInputInventory inputInventory = new CraftInputInventory(2);
     public final CraftResultInventory outputInventory = new CraftResultInventory();
     public final CraftResultInventory outputInventory = new CraftResultInventory();
 
 
     // 构造页面槽位
     // 构造页面槽位
     private void Constuct(PlayerInventory playerInventory) {
     private void Constuct(PlayerInventory playerInventory) {
-        Main.LOGGER.warn("CONSTUCT");
         this.addSlot(new Slot(this.inputInventory, 0, 62, 28));
         this.addSlot(new Slot(this.inputInventory, 0, 62, 28));
         this.addSlot(new Slot(this.inputInventory, 1, 80, 28));
         this.addSlot(new Slot(this.inputInventory, 1, 80, 28));
         this.addSlot(new Slot(this.outputInventory, 2, 138, 28) {
         this.addSlot(new Slot(this.outputInventory, 2, 138, 28) {

+ 10 - 18
src/main/java/com/hexdragon/enchrebirth/block/AnvilTileEntity.java

@@ -1,7 +1,6 @@
 package com.hexdragon.enchrebirth.block;
 package com.hexdragon.enchrebirth.block;
 
 
 import com.hexdragon.core.item.CraftInputInventory;
 import com.hexdragon.core.item.CraftInputInventory;
-import com.hexdragon.enchrebirth.Main;
 import com.hexdragon.enchrebirth.registry.RegMain;
 import com.hexdragon.enchrebirth.registry.RegMain;
 import com.sun.istack.internal.Nullable;
 import com.sun.istack.internal.Nullable;
 import net.minecraft.block.BlockState;
 import net.minecraft.block.BlockState;
@@ -16,6 +15,7 @@ import net.minecraft.util.IWorldPosCallable;
 import net.minecraft.util.text.ITextComponent;
 import net.minecraft.util.text.ITextComponent;
 import net.minecraft.util.text.TranslationTextComponent;
 import net.minecraft.util.text.TranslationTextComponent;
 
 
+// 铁砧 TileEntity,用于引导自定义模型与存储物品 NBT
 public class AnvilTileEntity extends TileEntity implements INamedContainerProvider {
 public class AnvilTileEntity extends TileEntity implements INamedContainerProvider {
     public AnvilTileEntity() {
     public AnvilTileEntity() {
         super(RegMain.tileEntityAnvil.get());
         super(RegMain.tileEntityAnvil.get());
@@ -24,33 +24,25 @@ public class AnvilTileEntity extends TileEntity implements INamedContainerProvid
         return new TranslationTextComponent("block.minecraft.anvil");
         return new TranslationTextComponent("block.minecraft.anvil");
     }
     }
 
 
-    public final CraftInputInventory inputInventory = new CraftInputInventory(2);
-
+    // 内建物品栏
+    public final CraftInputInventory inventory = new CraftInputInventory(2);
     @Nullable @Override public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
     @Nullable @Override public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
-        Main.LOGGER.warn("CREATE MENU");
+        // 与 Container 同步物品栏
         AnvilContainerRe container = new AnvilContainerRe(sycID, inventory, IWorldPosCallable.of(this.world, this.pos));
         AnvilContainerRe container = new AnvilContainerRe(sycID, inventory, IWorldPosCallable.of(this.world, this.pos));
-        inputInventory.container = container;
+        this.inventory.container = container;
         return container;
         return container;
     }
     }
 
 
+    // 将物品栏 NBT 化
     @Override public void read(BlockState state, CompoundNBT compound) {
     @Override public void read(BlockState state, CompoundNBT compound) {
-        Main.LOGGER.warn("READ");
-        inputInventory.addItem(ItemStack.read(compound.getCompound("item1")));
-        inputInventory.addItem(ItemStack.read(compound.getCompound("item2")));
+        inventory.setInventorySlotContents(0, ItemStack.read(compound.getCompound("ItemLeft")));
+        inventory.setInventorySlotContents(1, ItemStack.read(compound.getCompound("ItemRight")));
         super.read(null, compound);
         super.read(null, compound);
     }
     }
-
     @Override public CompoundNBT write(CompoundNBT compound) {
     @Override public CompoundNBT write(CompoundNBT compound) {
-        Main.LOGGER.warn("WRITE");
-        ItemStack itemStack1 = inputInventory.getStackInSlot(0).copy();
-        compound.put("item1", itemStack1.serializeNBT());
-        ItemStack itemStack2 = inputInventory.getStackInSlot(1).copy();
-        compound.put("item2", itemStack2.serializeNBT());
+        compound.put("ItemLeft", inventory.getStackInSlot(0).serializeNBT());
+        compound.put("ItemRight", inventory.getStackInSlot(1).serializeNBT());
         return super.write(compound);
         return super.write(compound);
     }
     }
 
 
-    public CraftInputInventory getInventory() {
-        return inputInventory;
-    }
-
 }
 }