OSDN Git Service

5e566ac926445a8d9266cad5ba1633c113f05974
[chemicraft/chemicraft.git] / common / pcc / chemicraft / core / container / ContainerChemicalCombinationTable.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.InventoryChemicalCombinationTableMaterial;
12 import pcc.chemicraft.core.inventory.InventoryChemicalCombinationTableResult;
13 import pcc.chemicraft.core.slot.SlotChemicalCombinationTableMaterial;
14 import pcc.chemicraft.core.slot.SlotChemicalCombinationTableResult;
15 import pcc.chemicraft.core.tileentity.TileEntityChemicalCombinationTable;
16 import cpw.mods.fml.common.network.PacketDispatcher;
17
18 public class ContainerChemicalCombinationTable extends Container {
19
20         /**
21          * the world.
22          */
23         private World worldObj;
24
25         /**
26          * the TileEntity of Position.
27          */
28         private int posX;
29         private int posY;
30         private int posZ;
31
32         /**
33          * the TileEntity.
34          */
35         private TileEntityChemicalCombinationTable tileentity;
36
37         /**
38          * the Inventory's.
39          */
40         private InventoryChemicalCombinationTableMaterial invm;
41         private InventoryChemicalCombinationTableResult invr;
42
43         /**
44          * the EntityPlayer.
45          */
46         private EntityPlayer entityplayer;
47
48         public ContainerChemicalCombinationTable(EntityPlayer par1EntityPlayer, TileEntityChemicalCombinationTable par2) {
49                 super();
50                 this.entityplayer = par1EntityPlayer;
51                 this.tileentity = par2;
52                 this.worldObj = tileentity.worldObj;
53                 this.posX = tileentity.xCoord;
54                 this.posY = tileentity.yCoord;
55                 this.posZ = tileentity.zCoord;
56                 this.invm = this.tileentity.getInvMaterial();
57                 this.invr = this.tileentity.getInvResult();
58
59                 for(int i = 0;i < 5;i++){
60                         for(int j = 0;j < 3;j++){
61                                 this.addSlotToContainer(new SlotChemicalCombinationTableMaterial(this.invm, i * 3 + j, i * 18 + 16, j * 18 + 15));
62                         }
63                 }
64                 this.addSlotToContainer(new SlotChemicalCombinationTableResult(invr, 0, 123, 34));
65
66
67                 int var3;
68                 for (var3 = 0; var3 < 3; ++var3)
69                 {
70                         for (int var4 = 0; var4 < 9; ++var4)
71                         {
72                                 this.addSlotToContainer(new Slot(par1EntityPlayer.inventory, var4 + var3 * 9 + 9, 8 + var4 * 18, 121 + var3 * 18));
73                         }
74                 }
75
76                 for (var3 = 0; var3 < 9; ++var3)
77                 {
78                         this.addSlotToContainer(new Slot(par1EntityPlayer.inventory, var3, 8 + var3 * 18, 179));
79                 }
80
81                 this.invm.setEventHandler(this);
82                 this.onCraftMatrixChanged(invm);
83         }
84
85
86
87         @Override
88         public void onCraftMatrixChanged(IInventory par1IInventory){
89                 this.invr.setInventorySlotContents(0, ChemiCraftAPI.instance().getCraftingManager().getChemicalCombinationResult(this.tileentity.getAtomsList(), this.tileentity.getAtomsAmountList()));
90                 if (par1IInventory instanceof InventoryChemicalCombinationTableResult) {
91                         PacketDispatcher.sendPacketToServer(this.tileentity.getDescriptionPacket());
92                 }
93         }
94
95         @Override
96         public boolean canInteractWith(EntityPlayer par1EntityPlayer){
97                 return this.worldObj.getBlockId(this.posX, this.posY, this.posZ) != ChemiCraftCore.instance.chemicalConbinationTableID ? false : par1EntityPlayer.getDistanceSq((double)this.posX + 0.5D, (double)this.posY + 0.5D, (double)this.posZ + 0.5D) <= 64.0D;
98         }
99
100         @Override
101         public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2) {
102                 ItemStack var3 = null;
103                 Slot var4 = (Slot)this.inventorySlots.get(par2);
104                 Slot var6 = (Slot)this.inventorySlots.get(0);
105
106                 if (var4 != null && var4.getHasStack()) {
107                         ItemStack var5 = var4.getStack();
108                         var3 = var5.copy();
109                         if (par2 >= 0 && par2 < 16) {
110                                 if (!this.mergeItemStack(var5, 16, 52, false)) {
111                                         return null;
112                                 }
113                         } else if (par2 >= 16 && par2 < 53) {
114                                 if (var6.isItemValid(var3)) {
115                                         if (!this.mergeItemStack(var5, 0, 15, false)) {
116                                                 return null;
117                                         }
118                                 }
119                         } else if(par2 == 16) {
120                                 if (!this.mergeItemStack(var5, 16, 52, false)) {
121                                         return null;
122                                 }
123                         }
124
125                         if (var5.stackSize == 0) {
126                                 var4.putStack((ItemStack)null);
127                         } else {
128                                 var4.onSlotChanged();
129                         }
130
131                         if (var5.stackSize == var3.stackSize) {
132                                 return null;
133                         }
134
135                         var4.onPickupFromSlot(par1EntityPlayer, var5);
136                 }
137
138                 return var3;
139         }
140
141         @Override
142         public ItemStack slotClick(int par1, int par2, int par3, EntityPlayer par4EntityPlayer) {
143                 if (par1 == 15) {
144                         if (this.invr.getStackInSlot(0) != null) {
145                                 this.tileentity.emptytoAtomsList();
146                         }
147                 }
148                 return super.slotClick(par1, par2, par3, par4EntityPlayer);
149         }
150
151         public void onCraftGuiClosed(EntityPlayer par1EntityPlayer)
152         {
153                 super.onCraftGuiClosed(par1EntityPlayer);
154         }
155
156 }