OSDN Git Service

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