OSDN Git Service

修正・変更
[chemicraft/chemicraft.git] / common / pcc / chemicraft / container / ContainerChemicalCombinationTable.java
1 package pcc.chemicraft.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.ChemiCraft;
10 import pcc.chemicraft.ChemiCraftAPI;
11 import pcc.chemicraft.inventory.InventoryChemicalCombinationTableMaterial;
12 import pcc.chemicraft.inventory.InventoryChemicalCombinationTableResult;
13 import pcc.chemicraft.slot.SlotChemicalCombinationTableMaterial;
14 import pcc.chemicraft.slot.SlotChemicalCombinationTableResult;
15 import pcc.chemicraft.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) != ChemiCraft.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                 if (!this.worldObj.isRemote)
156                 {
157                         for (int var2 = 0; var2 < 18; var2++){
158                                 ItemStack var3 = null;
159                                 if (var2 < 15){
160                                         var3 = this.invm.getStackInSlotOnClosing(var2);
161                                 } else if (var2 < 16) {
162                                         var3 = this.invr.getStackInSlotOnClosing(var2 - 15);
163                                 }
164
165                                 if (var3 != null){
166                                         par1EntityPlayer.dropPlayerItem(var3);
167                                 }
168                         }
169                 }
170         }
171
172 }