OSDN Git Service

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