OSDN Git Service

6b4fd7cfb58f91863c545725b40def116d169992
[chemicraft/chemicraft.git] / common / pcc / chemicraft / core / container / ContainerChemicalCraftingTable.java
1 package pcc.chemicraft.core.container;
2
3 import net.minecraft.entity.player.EntityPlayer;
4 import net.minecraft.inventory.Container;
5 import net.minecraft.inventory.IInventory;
6 import net.minecraft.inventory.Slot;
7 import net.minecraft.item.ItemStack;
8 import net.minecraft.world.World;
9 import pcc.chemicraft.core.ChemiCraftAPI;
10 import pcc.chemicraft.core.ChemiCraftCore;
11 import pcc.chemicraft.core.inventory.InventoryChemicalCraftingMaterial;
12 import pcc.chemicraft.core.inventory.InventoryChemicalCraftingNBT;
13 import pcc.chemicraft.core.inventory.InventoryChemicalCraftingResult;
14 import pcc.chemicraft.core.slot.SlotChemicalCraftingTableResult;
15 import pcc.chemicraft.core.tileentity.TileEntityChemicalCraftingTable;
16 import pcc.chemicraft.util.ChemicalNBTRecipe;
17
18 public class ContainerChemicalCraftingTable extends Container {
19
20         /**
21          * Worldのインスタンス
22          */
23         private World worldObj;
24
25
26         /**
27          * BlockのX, Y, Z座標
28          */
29         private int posX;
30         private int posY;
31         private int posZ;
32
33
34         /**
35          * the TileEntity.
36          */
37         private TileEntityChemicalCraftingTable tileEntity;
38
39
40         private InventoryChemicalCraftingMaterial invm;
41         private InventoryChemicalCraftingResult invr;
42         private InventoryChemicalCraftingNBT invn;
43
44         private ChemicalNBTRecipe useNBT;
45
46
47
48         public ContainerChemicalCraftingTable(EntityPlayer par1EntityPlayer, TileEntityChemicalCraftingTable par2)
49         {
50                 super();
51                 this.worldObj = par2.worldObj;
52                 this.posX = par2.xCoord;
53                 this.posY = par2.yCoord;
54                 this.posZ = par2.zCoord;
55                 this.tileEntity = par2;
56                 this.invm = (InventoryChemicalCraftingMaterial) this.tileEntity.chemicalCraftingInvMaterial;
57                 this.invr = (InventoryChemicalCraftingResult) this.tileEntity.chemicalCraftingInvResult;
58                 this.invn = (InventoryChemicalCraftingNBT) this.tileEntity.chemicalCraftingInvNBT;
59                 //GenerateInventory
60                 this.generateSlots(this.invm, this.invr, this.invn);
61                 //GeneratePlayerInventory
62                 int var3;
63
64                 for (var3 = 0; var3 < 3; ++var3)
65                 {
66                         for (int var4 = 0; var4 < 9; ++var4)
67                         {
68                                 this.addSlotToContainer(new Slot(par1EntityPlayer.inventory, var4 + var3 * 9 + 9, 8 + var4 * 18, 85 + var3 * 18));
69                         }
70                 }
71
72                 for (var3 = 0; var3 < 9; ++var3)
73                 {
74                         this.addSlotToContainer(new Slot(par1EntityPlayer.inventory, var3, 8 + var3 * 18, 143));
75                 }
76
77                 this.invm.setEventHandler(this);
78                 this.invr.setEventHandler(this);
79                 this.invn.setEventHandler(this);
80                 this.onCraftMatrixChanged(invm);
81         }
82
83
84
85         @Override
86         public void onCraftMatrixChanged(IInventory par1IInventory){
87                 this.useNBT = ChemiCraftAPI.instance().getCraftingManager().chemicalCrafting(this.invm, this.invr, this.invn);
88         }
89
90         @Override
91         public void onCraftGuiClosed(EntityPlayer par1EntityPlayer){
92                 super.onCraftGuiClosed(par1EntityPlayer);
93         }
94
95         @Override
96         public ItemStack slotClick(int par1, int par2, int par3, EntityPlayer par4EntityPlayer)
97         {
98                 if(par1 == 12){
99                         if(this.invr.getStackInSlot(0) == null) return super.slotClick(par1, par2, par3, par4EntityPlayer);
100                         ItemStack var1 = par4EntityPlayer.inventory.getItemStack();
101                         if(var1 != null){
102                                 if(var1.stackSize + this.invr.getStackInSlot(0).stackSize >= var1.getMaxStackSize()) return super.slotClick(par1, par2, par3, par4EntityPlayer);
103                         }
104                         for(int i = 0;i < this.invm.getSizeInventory();i++){
105                                 if(this.invm.getStackInSlot(i) != null){
106                                         if(this.invm.getStackInSlot(i).stackSize > 1){
107                                                 this.invm.getStackInSlot(i).stackSize--;
108                                         }else{
109                                                 this.invm.setInventorySlotContents(i, null);
110                                         }
111                                 }
112                         }
113                         ItemStack[] matchNBT = new ItemStack[this.invn.getSizeInventory()];
114                         for(int i = 0;i < this.invn.getSizeInventory();i++){
115                                 matchNBT[i] = this.invn.getStackInSlot(i);
116                         }
117                         if(this.useNBT == null) return super.slotClick(par1, par2, par3, par4EntityPlayer);
118                         ItemStack[] useItems = this.useNBT.getMatchItems(matchNBT);
119                         for(int i = 0;i < useItems.length;i++){
120                                 if(useItems[i] != null){
121                                         this.invn.setInventorySlotContents(i, null);
122                                 }
123                         }
124                 }
125                 return super.slotClick(par1, par2, par3, par4EntityPlayer);
126         }
127
128         @Override
129         public boolean canInteractWith(EntityPlayer par1EntityPlayer){
130                 return this.worldObj.getBlockId(this.posX, this.posY, this.posZ) != ChemiCraftCore.instance.chemicalCraftingTableID ? false : par1EntityPlayer.getDistanceSq((double)this.posX + 0.5D, (double)this.posY + 0.5D, (double)this.posZ + 0.5D) <= 64.0D;
131         }
132
133
134
135         @Override
136         public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2) {
137                 ItemStack var3 = null;
138                 Slot var4 = (Slot)this.inventorySlots.get(par2);
139                 Slot var6 = (Slot)this.inventorySlots.get(0);
140
141                 if (var4 != null && var4.getHasStack()) {
142                         ItemStack var5 = var4.getStack();
143                         var3 = var5.copy();
144                         if (par2 >= 0 && par2 < 13) {
145                                 if (!this.mergeItemStack(var5, 13, 13+36, false)) {
146                                         return null;
147                                 }
148                         } else if (par2 >= 16 && par2 < 53) {
149                                 if (var6.isItemValid(var3)) {
150                                         if (!this.mergeItemStack(var5, 3, 13, false)) {
151                                                 return null;
152                                         }
153                                 }
154                         }
155
156                         if (var5.stackSize == 0) {
157                                 var4.putStack((ItemStack)null);
158                         } else {
159                                 var4.onSlotChanged();
160                         }
161
162                         if (var5.stackSize == var3.stackSize) {
163                                 return null;
164                         }
165
166                         var4.onPickupFromSlot(par1EntityPlayer, var5);
167                 }
168
169                 return var3;
170         }
171
172
173
174         private void generateSlots(IInventory materialInv, IInventory resultInv, IInventory nbtInv){
175                 for(int i = 0;i < 3;i++){
176                         this.addSlotToContainer(new Slot(invn, i, 30 + 18 * i, 7));
177                 }
178                 for(int i = 0;i < 3;i++){
179                         for(int j = 0;j < 3;j++){
180                                 this.addSlotToContainer(new Slot(invm, i * 3 + j, 30 + 18 * j, 27 + 18 * i));
181                         }
182                 }
183                 this.addSlotToContainer(new SlotChemicalCraftingTableResult(invr, 0, 123, 33));
184         }
185 }