Răsfoiți Sursa

换用支持多格子的 CraftResultInventoryRe

LTCatt 6 ani în urmă
părinte
comite
dd80d2232f

+ 64 - 0
src/main/java/com/hexdragon/core/item/CraftResultInventoryRe.java

@@ -0,0 +1,64 @@
+package com.hexdragon.core.item;
+
+import net.minecraft.entity.player.PlayerEntity;
+import net.minecraft.inventory.IInventory;
+import net.minecraft.inventory.IRecipeHolder;
+import net.minecraft.inventory.ItemStackHelper;
+import net.minecraft.item.ItemStack;
+import net.minecraft.item.crafting.IRecipe;
+import net.minecraft.util.NonNullList;
+
+import javax.annotation.Nullable;
+
+// 支持多个物品栏的 CraftResultInventory
+public class CraftResultInventoryRe implements IInventory, IRecipeHolder {
+
+    // 构造函数
+    private final int slotsCount;
+    private final NonNullList<ItemStack> inventoryContents;
+
+    public CraftResultInventoryRe(int numSlots) {
+        this.slotsCount = numSlots;
+        this.inventoryContents = NonNullList.withSize(numSlots, ItemStack.EMPTY);
+    }
+
+    public CraftResultInventoryRe(ItemStack... stacksIn) {
+        this.slotsCount = stacksIn.length;
+        this.inventoryContents = NonNullList.from(ItemStack.EMPTY, stacksIn);
+    }
+
+    // 各种简单的方法
+    public int getSizeInventory() {return slotsCount;}
+
+    public boolean isEmpty() {
+        for (ItemStack itemstack : this.inventoryContents) {
+            if (!itemstack.isEmpty()) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    public void clear() {this.inventoryContents.clear();}
+
+    public void markDirty() {}
+
+    public boolean isUsableByPlayer(PlayerEntity player) {return true;}
+
+    // 对物品格子的输入输出
+    public ItemStack getStackInSlot(int index) {return this.inventoryContents.get(index);}
+
+    public void setInventorySlotContents(int index, ItemStack stack) {this.inventoryContents.set(index, stack);}
+
+    public ItemStack removeStackFromSlot(int index) {return ItemStackHelper.getAndRemove(this.inventoryContents, index);}
+
+    public ItemStack decrStackSize(int index, int count) {return ItemStackHelper.getAndRemove(this.inventoryContents, index);}
+
+    // 不知道有啥用的 Recipe 支持
+    @Nullable private IRecipe<?> recipeUsed;
+
+    public void setRecipeUsed(@Nullable IRecipe<?> recipe) {this.recipeUsed = recipe;}
+
+    @Nullable public IRecipe<?> getRecipeUsed() {return this.recipeUsed;}
+
+}

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

@@ -1,5 +1,6 @@
 package com.hexdragon.enchrebirth.block;
 
+import com.hexdragon.core.item.CraftResultInventoryRe;
 import com.hexdragon.core.item.EnchantmentHelperRe;
 import com.hexdragon.core.item.ItemHelperRe;
 import com.hexdragon.enchrebirth.reg.Registry;
@@ -27,7 +28,7 @@ public class GrindstoneContainerRe extends Container {
             GrindstoneContainerRe.this.onCraftMatrixChanged(this);
         }
     };
-    private final IInventory outputInventory = new Inventory(2);
+    private final IInventory outputInventory = new CraftResultInventoryRe(2);
 
     // 构造页面槽位
     private void Constuct(int windowIdIn, PlayerInventory playerInventoryIn, final IWorldPosCallable worldPosCallableIn) {