OSDN Git Service

ab84c7c148084995c57d670f0cb3c6c98f551eb1
[chemicraft/chemicraft.git] / common / chemicraft / tileentity / TileEntityChemicalCombinationTable.java
1 package chemicraft.tileentity;
2
3 import java.io.DataOutputStream;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6
7 import net.minecraft.src.EntityItem;
8 import net.minecraft.src.IInventory;
9 import net.minecraft.src.ItemStack;
10 import net.minecraft.src.NBTTagCompound;
11 import net.minecraft.src.NBTTagList;
12 import net.minecraft.src.Packet;
13 import net.minecraft.src.TileEntity;
14 import net.minecraft.src.WorldServer;
15 import chemicraft.ChemiCraft;
16 import chemicraft.ChemiCraftData;
17 import chemicraft.inventory.InventoryChemicalCombinationTableMaterial;
18 import chemicraft.inventory.InventoryChemicalCombinationTableResult;
19 import chemicraft.system.PacketHandler;
20 import chemicraft.util.ComparatorItemStack;
21
22 import com.google.common.io.ByteArrayDataInput;
23
24 import cpw.mods.fml.common.network.PacketDispatcher;
25
26 public class TileEntityChemicalCombinationTable extends TileEntity {
27
28         private InventoryChemicalCombinationTableMaterial invm = new InventoryChemicalCombinationTableMaterial();
29         private InventoryChemicalCombinationTableResult invr = new InventoryChemicalCombinationTableResult();
30
31         private ArrayList<String> atomsList = new ArrayList<String>();
32         private ArrayList<Integer> atomsAmountList = new ArrayList<Integer>();
33         private int atomsListSize = 0;
34
35         private ItemStack dropItems;
36
37         private boolean isProvidePower;
38
39         @Override
40         public void updateEntity(){
41                 super.updateEntity();
42                 if (this.isProvidePower && !this.worldObj.isRemote) {
43                         this.field_00001();
44                         this.isProvidePower = false;
45                         PacketDispatcher.sendPacketToAllPlayers(this.getDescriptionPacket());
46                 }
47                 if (this.worldObj instanceof WorldServer) {
48                         System.out.println(this.atomsList + ":Server");
49                         System.out.println(this.atomsAmountList + ":Server");
50                 } else {
51                         System.out.println(this.atomsList + ":Client");
52                         System.out.println(this.atomsAmountList + ":Client");
53                 }
54         }
55
56         @Override
57         public void readFromNBT(NBTTagCompound par1){
58                 super.readFromNBT(par1);
59                 NBTTagList var2 = par1.getTagList("Items");
60                 for (int var3 = 0; var3 < var2.tagCount(); ++var3)
61                 {
62                         NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
63                         int var5 = var4.getByte("Slot") & 255;
64
65                         if  (var5 >= 0 && var5 < this.invm.getSizeInventory())
66                         {
67                                 this.invm.setInventorySlotContents(var5, ItemStack.loadItemStackFromNBT(var4));
68                         }
69                 }
70
71                 NBTTagList var6 = par1.getTagList("Items2");
72                 for (int var3 = 0; var3 < var6.tagCount(); ++var3)
73                 {
74                         NBTTagCompound var7 = (NBTTagCompound)var6.tagAt(var3);
75                         int var8 = var7.getByte("Slot2") & 255;
76
77                         if  (var8 >= 0 && var8 < this.invr.getSizeInventory())
78                         {
79                                 this.invr.setInventorySlotContents(var8, ItemStack.loadItemStackFromNBT(var7));
80                         }
81                 }
82
83                 this.atomsListSize = par1.getInteger("atomsListSize");
84                 for(int i = 0;i < this.atomsListSize;i++){
85                         this.atomsList.add(par1.getString("atomsList" + i));
86                 }
87                 for(int i = 0;i < this.atomsListSize;i++){
88                         this.atomsAmountList.add(par1.getInteger("atomsAmountList" + i));
89                 }
90
91                 this.isProvidePower = par1.getBoolean("isProvidePower");
92         }
93
94         @Override
95         public void writeToNBT(NBTTagCompound par1){
96                 super.writeToNBT(par1);
97                 NBTTagList var2 = new NBTTagList();
98                 for (int var3 = 0; var3 < this.invm.getSizeInventory(); ++var3)
99                 {
100                         if  (this.invm.getStackInSlot(var3) != null)
101                         {
102                                 NBTTagCompound var4 = new NBTTagCompound();
103                                 var4.setByte("Slot", (byte)var3);
104                                 this.invm.getStackInSlot(var3).writeToNBT(var4);
105                                 var2.appendTag(var4);
106                         }
107                 }
108                 par1.setTag("Items", var2);
109
110                 NBTTagList var5 = new NBTTagList();
111                 for (int var6 = 0; var6 < this.invr.getSizeInventory(); ++var6)
112                 {
113                         if  (this.invr.getStackInSlot(var6) != null)
114                         {
115                                 NBTTagCompound var7 = new NBTTagCompound();
116                                 var7.setByte("Slot2", (byte)var6);
117                                 this.invr.getStackInSlot(var6).writeToNBT(var7);
118                                 var5.appendTag(var7);
119                         }
120                 }
121                 par1.setTag("Items2", var5);
122
123                 this.atomsList.trimToSize();
124                 this.atomsAmountList.trimToSize();
125                 for(int i = 0;i < this.atomsList.size();i++){
126                         par1.setString("atomsList" + i, this.atomsList.get(i));
127                 }
128                 for(int i = 0;i < this.atomsAmountList.size();i++){
129                         par1.setInteger("atomsAmountList" + i, this.atomsAmountList.get(i));
130                 }
131
132                 this.atomsListSize = this.atomsList.size();
133                 par1.setInteger("atomsListSize", this.atomsListSize);
134
135                 par1.setBoolean("isProvidePower", this.isProvidePower);
136         }
137
138         @Override
139         public Packet getDescriptionPacket() {
140                 return PacketHandler.getPacket(this);
141         }
142
143         public void readPacket(ByteArrayDataInput data) {
144                 try {
145                         for(int i = 0;i < this.invm.getSizeInventory();i++){
146                                 int id = data.readInt();
147                                 int size = data.readByte();
148                                 int damage = data.readInt();
149                                 if (id != 0 && size != 0){
150                                         this.invm.setInventorySlotContents(i, new ItemStack(id, size, damage));
151                                 }else{
152                                         this.invm.setInventorySlotContents(i, null);
153                                 }
154                         }
155
156                         for(int i = 0;i < this.invr.getSizeInventory();i++){
157                                 int id = data.readInt();
158                                 int size = data.readByte();
159                                 int damage = data.readInt();
160                                 if (id != 0 && size != 0){
161                                         this.invr.setInventorySlotContents(i, new ItemStack(id, size, damage));
162                                 }else{
163                                         this.invr.setInventorySlotContents(i, null);
164                                 }
165                         }
166
167                         this.atomsListSize = data.readInt();
168                         this.atomsList.clear();
169                         this.atomsAmountList.clear();
170                         for(int i = 0;i < this.atomsListSize;i++){
171                                 this.atomsList.add(data.readUTF());
172                         }
173                         for(int i = 0;i < this.atomsListSize;i++){
174                                 this.atomsAmountList.add(data.readInt());
175                         }
176
177                         ItemStack itemstack = new ItemStack(data.readInt(), data.readByte(), data.readInt());
178                         if(itemstack.itemID != 0 && !this.worldObj.isRemote){
179                                 this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, this.xCoord, this.yCoord + 1, this.zCoord, itemstack));
180                         }
181                         this.dropItems = null;
182
183                         this.isProvidePower = data.readBoolean();
184                 } catch (Exception e) {
185                         e.printStackTrace();
186                 }
187         }
188
189         public void writePacket(DataOutputStream dos){
190                 try {
191                         for(int i = 0;i < this.invm.getSizeInventory();i++){
192                                 int id = 0;
193                                 int size = 0;
194                                 int damage  = 0;
195                                 ItemStack itemstack = this.invm.getStackInSlot(i);
196                                 if (itemstack != null){
197                                         id = itemstack.itemID;
198                                         size = itemstack.stackSize;
199                                         damage = itemstack.getItemDamage();
200                                         dos.writeInt(id);
201                                         dos.writeByte(size);
202                                         dos.writeInt(damage);
203                                 }else{
204                                         dos.writeInt(0);
205                                         dos.writeByte(0);
206                                         dos.writeInt(0);
207                                 }
208                         }
209
210                         for(int i = 0;i < this.invr.getSizeInventory();i++){
211                                 int id = 0;
212                                 int size = 0;
213                                 int damage  = 0;
214                                 ItemStack itemstack = this.invr.getStackInSlot(i);
215                                 if (itemstack != null){
216                                         id = itemstack.itemID;
217                                         size = itemstack.stackSize;
218                                         damage = itemstack.getItemDamage();
219                                         dos.writeInt(id);
220                                         dos.writeByte(size);
221                                         dos.writeInt(damage);
222                                 }else{
223                                         dos.writeInt(0);
224                                         dos.writeByte(0);
225                                         dos.writeInt(0);
226                                 }
227                         }
228
229                         dos.writeInt(this.atomsList.size());
230                         for(int i = 0;i < this.atomsList.size();i++){
231                                 dos.writeUTF(this.atomsList.get(i));
232                         }
233                         for(int i = 0;i < this.atomsAmountList.size();i++){
234                                 dos.writeInt(this.atomsAmountList.get(i));
235                         }
236
237                         if (this.dropItems != null){
238                                 int id = this.dropItems.itemID;
239                                 int size = this.dropItems.stackSize;
240                                 int damage = this.dropItems.getItemDamage();
241                                 dos.writeInt(id);
242                                 dos.writeByte(size);
243                                 dos.writeInt(damage);
244                         }else{
245                                 dos.writeInt(0);
246                                 dos.writeByte(0);
247                                 dos.writeInt(0);
248                         }
249                         this.dropItems = null;
250
251                         dos.writeBoolean(this.isProvidePower);
252                 } catch (Exception e) {
253                         e.printStackTrace();
254                 }
255         }
256
257         public void setAtoms(String par1, int par2){
258                 if (this.atomsList.indexOf(par1) != -1){
259                         int var1 = this.atomsList.indexOf(par1);
260                         this.atomsAmountList.set(var1, this.atomsAmountList.get(var1) + par2);
261                 }else{
262                         this.atomsList.add(par1);
263                         this.atomsAmountList.add(par2);
264                 }
265                 PacketDispatcher.sendPacketToAllPlayers(this.getDescriptionPacket());
266         }
267
268         public void setDropItems(ItemStack par1ItemStack){
269                 this.dropItems = par1ItemStack;
270         }
271
272         public String getAtoms(){
273                 StringBuffer var1 = new StringBuffer();
274                 for(int i = 0;i < this.atomsList.size();i++){
275                         if (this.atomsAmountList.get(i) != 1){
276                                 var1.append(this.atomsList.get(i) + this.atomsAmountList.get(i));
277                         }else{
278                                 var1.append(this.atomsList.get(i));
279                         }
280                 }
281                 return var1.toString();
282         }
283
284         public ArrayList<String> getAtomsList(){
285                 return this.atomsList;
286         }
287
288         public ArrayList<Integer> getAtomsAmountList(){
289                 return this.atomsAmountList;
290         }
291
292         public void emptytoAtomsList(){
293                 this.atomsList.clear();
294                 this.atomsAmountList.clear();
295         }
296
297         public void setProvidePower(boolean par1) {
298                 this.isProvidePower = par1;
299         }
300
301         public boolean getProvidePower() {
302                 return this.isProvidePower;
303         }
304
305         public void field_00001() {
306                 IInventory inv = this.getInvMaterial();
307                 ItemStack[] stacks = new ItemStack[inv.getSizeInventory()];
308                 for(int i = 0;i < stacks.length;i++){
309                         if(inv.getStackInSlot(i) != null){
310                                 stacks[i] = inv.getStackInSlot(i);
311                         }
312                 }
313                 Arrays.sort(stacks, new ComparatorItemStack());
314                 stacks = ChemiCraft.instance.arrayAuxiliary.deleteNull(stacks);
315
316                 for(int i = 0;i < stacks.length;i++){
317                         if(stacks[i].stackSize > 1){
318                                 this.setAtoms(ChemiCraftData.ATOMSLIST[stacks[i].getItemDamage()], stacks[i].stackSize);
319                         }else{
320                                 this.setAtoms(ChemiCraftData.ATOMSLIST[stacks[i].getItemDamage()], 1);
321                         }
322                 }
323
324                 for(int i = 0;i < inv.getSizeInventory();i++){
325                         inv.setInventorySlotContents(i, null);
326                 }
327         }
328
329         public void field_00002() {
330                 ArrayList<String> var1 = this.getAtomsList();
331                 ArrayList<Integer> var2 = this.getAtomsAmountList();
332
333                 for (int i = 0;i < var1.size();i++) {
334                         for (int j = 0;j < ChemiCraftData.ATOMSLIST.length;j++) {
335                                 if (var1.get(i).equals(ChemiCraftData.ATOMSLIST[j])) {
336                                         int var3 = var2.get(i);
337                                         while(var3 > 0){
338                                                 if(var3 > 64){
339                                                         ItemStack itemstack = new ItemStack(ChemiCraft.instance.itemAtoms, 64, j);
340                                                         this.setDropItems(itemstack);
341                                                         PacketDispatcher.sendPacketToServer(this.getDescriptionPacket());
342                                                         var3 -= 64;
343                                                 }else{
344                                                         ItemStack itemstack = new ItemStack(ChemiCraft.instance.itemAtoms, var3, j);
345                                                         this.setDropItems(itemstack);
346                                                         PacketDispatcher.sendPacketToServer(this.getDescriptionPacket());
347                                                         var3 = 0;
348                                                 }
349                                         }
350                                         break;
351                                 }
352                         }
353                 }
354                 IInventory invr = this.getInvResult();
355                 if (invr.getStackInSlot(0) != null) {
356                         invr.setInventorySlotContents(0, null);
357                 }
358                 this.emptytoAtomsList();
359         }
360
361         protected boolean isNumber(String par1){
362                 try {
363                         int var1 = Integer.valueOf(new String(par1));
364                 } catch (Exception e) {
365                         return false;
366                 }
367                 return true;
368         }
369
370         public InventoryChemicalCombinationTableMaterial getInvMaterial(){
371                 return this.invm;
372         }
373
374         public InventoryChemicalCombinationTableResult getInvResult(){
375                 return this.invr;
376         }
377
378 }