OSDN Git Service

GUIのSlot追加
[chemicraft/chemicraft.git] / common / chemicraft / TileEntityChemicalCraftingTable.java
1 package chemicraft;
2
3 import java.io.DataOutputStream;
4 import java.util.ArrayList;
5
6 import net.minecraft.src.IInventory;
7 import net.minecraft.src.ItemStack;
8 import net.minecraft.src.NBTTagCompound;
9 import net.minecraft.src.NBTTagList;
10 import net.minecraft.src.Packet;
11 import net.minecraft.src.TileEntity;
12
13 import com.google.common.io.ByteArrayDataInput;
14
15 import cpw.mods.fml.common.network.PacketDispatcher;
16
17 public class TileEntityChemicalCraftingTable extends TileEntity {
18
19         /**
20          * DecompositionのInventory
21          */
22         public IInventory decompositionInv = new InventoryDecomposition();
23
24         /**
25          * ActiveなTabの名前
26          */
27         private String activeTab;
28
29
30         public TileEntityChemicalCraftingTable() {
31                 super();
32                 // ActiveTabのデフォルトを設定
33                 this.activeTab = "Decomposition";
34         }
35
36         int time = 0;
37         @Override
38         public void updateEntity() {
39                 super.updateEntity();
40         }
41
42
43         @Override
44         public void readFromNBT(NBTTagCompound par1) {
45                 super.readFromNBT(par1);
46
47                 this.activeTab = par1.getString("activeTab");
48
49                 NBTTagList var2 = par1.getTagList("Items");
50                 for (int var3 = 0; var3 < var2.tagCount(); ++var3)
51                 {
52                         NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
53                         int var5 = var4.getByte("Slot") & 255;
54
55                         if (var5 >= 0 && var5 < this.decompositionInv.getSizeInventory())
56                         {
57                                 this.decompositionInv.setInventorySlotContents(var5, ItemStack.loadItemStackFromNBT(var4));
58                         }
59                 }
60         }
61
62
63         @Override
64         public void writeToNBT(NBTTagCompound par1) {
65                 super.writeToNBT(par1);
66
67                 par1.setString("activeTab", this.activeTab);
68
69                 NBTTagList var2 = new NBTTagList();
70                 for (int var3 = 0; var3 < this.decompositionInv.getSizeInventory(); ++var3)
71                 {
72                         if (this.decompositionInv.getStackInSlot(var3) != null)
73                         {
74                                 NBTTagCompound var4 = new NBTTagCompound();
75                                 var4.setByte("Slot", (byte)var3);
76                                 this.decompositionInv.getStackInSlot(var3).writeToNBT(var4);
77                                 var2.appendTag(var4);
78                         }
79                 }
80                 par1.setTag("Items", var2);
81         }
82
83
84         public String getActiveTab() {
85                 return this.activeTab;
86         }
87
88
89         public void setActiveTab(String activeTab) {
90                 this.activeTab = activeTab;
91                 PacketDispatcher.sendPacketToServer(this.getDescriptionPacket());
92         }
93
94
95         public IInventory[] getActiveInventory(){
96                 ArrayList<IInventory> inventorys = new ArrayList<IInventory>();
97                 if(this.activeTab.equals("Decomposition")){
98                         inventorys.add(this.decompositionInv);
99                         return inventorys.toArray(new IInventory[1]);
100                 }else{
101                         return null;
102                 }
103         }
104
105
106         @Override
107         public Packet getDescriptionPacket() {
108                 return PacketHandler.getPacket(this);
109         }
110
111
112         public void readPacket(ByteArrayDataInput data) {
113                 try {
114                         this.activeTab = data.readUTF();
115                         for(int i = 0;i < this.decompositionInv.getSizeInventory();i++){
116                                 int id = data.readInt();
117                                 int damage = data.readInt();
118                                 int size = data.readInt();
119                                 if(id != 0){
120                                         this.decompositionInv.setInventorySlotContents(i, new ItemStack(id, size, damage));
121                                 }
122                         }
123                 } catch (Exception e) {
124                         e.printStackTrace();
125                 }
126         }
127
128
129         public void writePacket(DataOutputStream dos){
130                 try {
131                         dos.writeUTF(this.activeTab);
132                         for(int i = 0;i < this.decompositionInv.getSizeInventory();i++){
133                                 if(this.decompositionInv.getStackInSlot(i) != null){
134                                         dos.writeInt(this.decompositionInv.getStackInSlot(i).itemID);
135                                         dos.writeInt(this.decompositionInv.getStackInSlot(i).getItemDamage());
136                                         dos.writeInt(this.decompositionInv.getStackInSlot(i).stackSize);
137                                 }else{
138                                         dos.writeInt(0);
139                                         dos.writeInt(0);
140                                         dos.writeInt(0);
141                                 }
142                         }
143                 } catch (Exception e) {
144                         e.printStackTrace();
145                 }
146         }
147
148 }