OSDN Git Service

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