OSDN Git Service

0ebfc9c967a439675491283c169bb8a2f962582a
[chemicraft/chemicraft.git] / common / chemicraft / PacketHandler.java
1 package chemicraft;\r
2 \r
3 import java.io.ByteArrayOutputStream;\r
4 import java.io.DataOutputStream;\r
5 import java.io.IOException;\r
6 \r
7 import net.minecraft.src.EntityPlayer;\r
8 import net.minecraft.src.NetworkManager;\r
9 import net.minecraft.src.Packet;\r
10 import net.minecraft.src.Packet250CustomPayload;\r
11 import net.minecraft.src.TileEntity;\r
12 import net.minecraft.src.World;\r
13 \r
14 import com.google.common.io.ByteArrayDataInput;\r
15 import com.google.common.io.ByteStreams;\r
16 \r
17 import cpw.mods.fml.common.network.IPacketHandler;\r
18 import cpw.mods.fml.common.network.Player;\r
19 \r
20 /**\r
21  * サーバーとクライアントのデータの同期に必要なクラス\r
22  *\r
23  * @author Lilly\r
24  *\r
25  */\r
26 public class PacketHandler implements IPacketHandler {\r
27 \r
28         @Override\r
29         public void onPacketData(NetworkManager manager, Packet250CustomPayload packet, Player player) {\r
30                 if (packet.channel.equals("chemicraft")) {\r
31                         ByteArrayDataInput var1 = ByteStreams.newDataInput(packet.data);\r
32 \r
33                         World worldClient;\r
34                         World worldServer;\r
35                         TileEntity tileEntity;\r
36                         TileEntityDecompositionTable var2;\r
37 \r
38                         // TileEntityのx, y, z座標\r
39                         int x, y, z;\r
40 \r
41                         // 座標読み込み\r
42                         x = var1.readInt();\r
43                         y = var1.readInt();\r
44                         z = var1.readInt();\r
45 \r
46                         worldClient = ChemiCraft.instance.proxy.getClientWorld();\r
47                         worldServer = ((EntityPlayer)player).worldObj;\r
48                         if(worldClient != null){\r
49                                 tileEntity = worldClient.getBlockTileEntity(x, y, z);\r
50                                 if (tileEntity instanceof TileEntityDecompositionTable) {\r
51                                         var2 = (TileEntityDecompositionTable) tileEntity;\r
52                                         var2.readPacket(var1);\r
53                                 }\r
54                                 return;\r
55                         }\r
56                         if(worldServer != null){\r
57                                 tileEntity = worldServer.getBlockTileEntity(x, y, z);\r
58                                 if (tileEntity instanceof TileEntityDecompositionTable) {\r
59                                         var2 = (TileEntityDecompositionTable) tileEntity;\r
60                                         var2.readPacket(var1);\r
61                                 }\r
62                                 return;\r
63                         }\r
64                 }\r
65         }\r
66 \r
67 \r
68         public static Packet getPacket(TileEntityDecompositionTable tileEntity) {\r
69                 ByteArrayOutputStream var1 = new ByteArrayOutputStream(128);\r
70                 DataOutputStream var2 = new DataOutputStream(var1);\r
71 \r
72                 // Blockのx, y, z座標\r
73                 int x, y, z;\r
74 \r
75                 // activeTab\r
76                 String activeTab;\r
77 \r
78                 // x, y, z座標を代入\r
79                 x = tileEntity.xCoord;\r
80                 y = tileEntity.yCoord;\r
81                 z = tileEntity.zCoord;\r
82 \r
83                 try {\r
84                         // 座標書き込み\r
85                         var2.writeInt(x);\r
86                         var2.writeInt(y);\r
87                         var2.writeInt(z);\r
88                         tileEntity.writePacket(var2);\r
89                 } catch (IOException e) {\r
90                         e.printStackTrace();\r
91                 }\r
92 \r
93                 // パケットの作成\r
94                 Packet250CustomPayload packet = new Packet250CustomPayload();\r
95                 packet.channel = "chemicraft";\r
96                 packet.data = var1.toByteArray();\r
97                 packet.length = var1.size();\r
98                 packet.isChunkDataPacket = true;\r
99                 return packet;\r
100         }\r
101 \r
102         public static Packet getPacket(TileEntityChemicalCombinationTable tileEntity) {\r
103                 ByteArrayOutputStream var1 = new ByteArrayOutputStream(128);\r
104                 DataOutputStream var2 = new DataOutputStream(var1);\r
105 \r
106                 // Blockのx, y, z座標\r
107                 int x, y, z;\r
108 \r
109                 // activeTab\r
110                 String activeTab;\r
111 \r
112                 // x, y, z座標を代入\r
113                 x = tileEntity.xCoord;\r
114                 y = tileEntity.yCoord;\r
115                 z = tileEntity.zCoord;\r
116 \r
117                 try {\r
118                         // 座標書き込み\r
119                         var2.writeInt(x);\r
120                         var2.writeInt(y);\r
121                         var2.writeInt(z);\r
122                         tileEntity.writePacket(var2);\r
123                 } catch (IOException e) {\r
124                         e.printStackTrace();\r
125                 }\r
126 \r
127                 // パケットの作成\r
128                 Packet250CustomPayload packet = new Packet250CustomPayload();\r
129                 packet.channel = "chemicraft";\r
130                 packet.data = var1.toByteArray();\r
131                 packet.length = var1.size();\r
132                 packet.isChunkDataPacket = true;\r
133                 return packet;\r
134         }\r
135 \r
136         public static Packet getPacket(TileEntityToolAndWeaponCraftingTable tileEntity) {\r
137                 ByteArrayOutputStream var1 = new ByteArrayOutputStream(128);\r
138                 DataOutputStream var2 = new DataOutputStream(var1);\r
139 \r
140                 // Blockのx, y, z座標\r
141                 int x, y, z;\r
142 \r
143                 // activeTab\r
144                 String activeTab;\r
145 \r
146                 // x, y, z座標を代入\r
147                 x = tileEntity.xCoord;\r
148                 y = tileEntity.yCoord;\r
149                 z = tileEntity.zCoord;\r
150 \r
151                 try {\r
152                         // 座標書き込み\r
153                         var2.writeInt(x);\r
154                         var2.writeInt(y);\r
155                         var2.writeInt(z);\r
156                         tileEntity.writePacket(var2);\r
157                 } catch (IOException e) {\r
158                         e.printStackTrace();\r
159                 }\r
160 \r
161                 // パケットの作成\r
162                 Packet250CustomPayload packet = new Packet250CustomPayload();\r
163                 packet.channel = "chemicraft";\r
164                 packet.data = var1.toByteArray();\r
165                 packet.length = var1.size();\r
166                 packet.isChunkDataPacket = true;\r
167                 return packet;\r
168         }\r
169 \r
170         public static Packet getPacket(TileEntityMaterialCraftingTable tileEntity) {\r
171                 ByteArrayOutputStream var1 = new ByteArrayOutputStream(128);\r
172                 DataOutputStream var2 = new DataOutputStream(var1);\r
173 \r
174                 // Blockのx, y, z座標\r
175                 int x, y, z;\r
176 \r
177                 // activeTab\r
178                 String activeTab;\r
179 \r
180                 // x, y, z座標を代入\r
181                 x = tileEntity.xCoord;\r
182                 y = tileEntity.yCoord;\r
183                 z = tileEntity.zCoord;\r
184 \r
185                 try {\r
186                         // 座標書き込み\r
187                         var2.writeInt(x);\r
188                         var2.writeInt(y);\r
189                         var2.writeInt(z);\r
190                         tileEntity.writePacket(var2);\r
191                 } catch (IOException e) {\r
192                         e.printStackTrace();\r
193                 }\r
194 \r
195                 // パケットの作成\r
196                 Packet250CustomPayload packet = new Packet250CustomPayload();\r
197                 packet.channel = "chemicraft";\r
198                 packet.data = var1.toByteArray();\r
199                 packet.length = var1.size();\r
200                 packet.isChunkDataPacket = true;\r
201                 return packet;\r
202         }\r
203 \r
204 }\r