OSDN Git Service

01235d47c0ef8b6800d76e7a0b85882af01b7c3f
[chemicraft/chemicraft.git] / common / chemicraft / tileentity / TileEntityChemicalCombinationTable.java
1 package chemicraft.tileentity;
2
3 import java.io.DataOutputStream;
4
5 import com.google.common.io.ByteArrayDataInput;
6
7 import chemicraft.inventory.InventoryChemicalCombinationTableMaterial;
8 import chemicraft.inventory.InventoryChemicalCombinationTableResult;
9 import chemicraft.system.PacketHandler;
10 import net.minecraft.src.ItemStack;
11 import net.minecraft.src.NBTTagCompound;
12 import net.minecraft.src.NBTTagList;
13 import net.minecraft.src.Packet;
14 import net.minecraft.src.TileEntity;
15
16 public class TileEntityChemicalCombinationTable extends TileEntity {
17
18         private InventoryChemicalCombinationTableMaterial invm = new InventoryChemicalCombinationTableMaterial();
19         private InventoryChemicalCombinationTableResult invr = new InventoryChemicalCombinationTableResult();
20
21         private String atomsList = "";
22
23         @Override
24         public void readFromNBT(NBTTagCompound par1){
25                 super.readFromNBT(par1);
26                 NBTTagList var2 = par1.getTagList("Items");
27                 for (int var3 = 0; var3 < var2.tagCount(); ++var3)
28                 {
29                         NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
30                         int var5 = var4.getByte("Slot") & 255;
31
32                         if (var5 >= 0 && var5 < this.invm.getSizeInventory())
33                         {
34                                 this.invm.setInventorySlotContents(var5, ItemStack.loadItemStackFromNBT(var4));
35                         }
36                 }
37
38                 NBTTagList var6 = par1.getTagList("Items2");
39                 for (int var3 = 0; var3 < var6.tagCount(); ++var3)
40                 {
41                         NBTTagCompound var7 = (NBTTagCompound)var6.tagAt(var3);
42                         int var8 = var7.getByte("Slot2") & 255;
43
44                         if (var8 >= 0 && var8 < this.invr.getSizeInventory())
45                         {
46                                 this.invr.setInventorySlotContents(var8, ItemStack.loadItemStackFromNBT(var7));
47                         }
48                 }
49
50                 this.atomsList = par1.getString("AtomsList");
51         }
52
53         @Override
54         public Packet getDescriptionPacket() {
55                 return PacketHandler.getPacket(this);
56         }
57
58         @Override
59         public void writeToNBT(NBTTagCompound par1){
60                 super.writeToNBT(par1);
61                 NBTTagList var2 = new NBTTagList();
62                 for (int var3 = 0; var3 < this.invm.getSizeInventory(); ++var3)
63                 {
64                         if (this.invm.getStackInSlot(var3) != null)
65                         {
66                                 NBTTagCompound var4 = new NBTTagCompound();
67                                 var4.setByte("Slot", (byte)var3);
68                                 this.invm.getStackInSlot(var3).writeToNBT(var4);
69                                 var2.appendTag(var4);
70                         }
71                 }
72                 par1.setTag("Items", var2);
73
74                 NBTTagList var5 = new NBTTagList();
75                 for (int var6 = 0; var6 < this.invr.getSizeInventory(); ++var6)
76                 {
77                         if (this.invr.getStackInSlot(var6) != null)
78                         {
79                                 NBTTagCompound var7 = new NBTTagCompound();
80                                 var7.setByte("Slot2", (byte)var6);
81                                 this.invr.getStackInSlot(var6).writeToNBT(var7);
82                                 var5.appendTag(var7);
83                         }
84                 }
85                 par1.setTag("Items2", var5);
86
87                 par1.setString("AtomsList", this.atomsList);
88         }
89
90         public void readPacket(ByteArrayDataInput data) {
91                 try {
92                         for(int i = 0;i < this.invm.getSizeInventory();i++){
93                                 int id = data.readInt();
94                                 int size = data.readByte();
95                                 int damage = data.readInt();
96                                 if(id != 0 && size != 0){
97                                         this.invm.setInventorySlotContents(i, new ItemStack(id, size, damage));
98                                 }else{
99                                         this.invm.setInventorySlotContents(i, null);
100                                 }
101                         }
102
103                         for(int i = 0;i < this.invr.getSizeInventory();i++){
104                                 int id = data.readInt();
105                                 int size = data.readByte();
106                                 int damage = data.readInt();
107                                 if(id != 0 && size != 0){
108                                         this.invr.setInventorySlotContents(i, new ItemStack(id, size, damage));
109                                 }else{
110                                         this.invr.setInventorySlotContents(i, null);
111                                 }
112                         }
113                 } catch (Exception e) {
114                         e.printStackTrace();
115                 }
116         }
117
118
119         public void writePacket(DataOutputStream dos){
120                 try {
121                         for(int i = 0;i < this.invm.getSizeInventory();i++){
122                                 int id = 0;
123                                 int size = 0;
124                                 int damage  = 0;
125                                 ItemStack itemstack = this.invm.getStackInSlot(i);
126                                 if(itemstack != null){
127                                         id = itemstack.itemID;
128                                         size = itemstack.stackSize;
129                                         damage = itemstack.getItemDamage();
130                                         dos.writeInt(id);
131                                         dos.writeByte(size);
132                                         dos.writeInt(damage);
133                                 }else{
134                                         dos.writeInt(0);
135                                         dos.writeByte(0);
136                                         dos.writeInt(0);
137                                 }
138                         }
139
140                         for(int i = 0;i < this.invr.getSizeInventory();i++){
141                                 int id = 0;
142                                 int size = 0;
143                                 int damage  = 0;
144                                 ItemStack itemstack = this.invr.getStackInSlot(i);
145                                 if(itemstack != null){
146                                         id = itemstack.itemID;
147                                         size = itemstack.stackSize;
148                                         damage = itemstack.getItemDamage();
149                                         dos.writeInt(id);
150                                         dos.writeByte(size);
151                                         dos.writeInt(damage);
152                                 }else{
153                                         dos.writeInt(0);
154                                         dos.writeByte(0);
155                                         dos.writeInt(0);
156                                 }
157                         }
158                 } catch (Exception e) {
159                         e.printStackTrace();
160                 }
161         }
162
163         public void setAtoms(String par1){
164                 this.atomsList = this.atomsList + par1;
165         }
166
167         public InventoryChemicalCombinationTableMaterial getInvMaterial(){
168                 return this.invm;
169         }
170
171         public InventoryChemicalCombinationTableResult getInvResult(){
172                 return this.invr;
173         }
174
175 }