OSDN Git Service

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