OSDN Git Service

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