OSDN Git Service

93212f5363c9735cbd54d308f763379c202eacc2
[chemicraft/chemicraft.git] / common / chemicraft / PacketHandler.java
1 package chemicraft;\r
2 \r
3 import java.io.ByteArrayOutputStream;\r
4 import java.io.DataInputStream;\r
5 import java.io.DataOutputStream;\r
6 import java.io.IOException;\r
7 \r
8 import com.google.common.io.ByteArrayDataInput;\r
9 import com.google.common.io.ByteStreams;\r
10 \r
11 import net.minecraft.src.NetworkManager;\r
12 import net.minecraft.src.Packet;\r
13 import net.minecraft.src.Packet250CustomPayload;\r
14 import net.minecraft.src.TileEntity;\r
15 import net.minecraft.src.World;\r
16 import cpw.mods.fml.common.network.IPacketHandler;\r
17 import cpw.mods.fml.common.network.Player;\r
18 \r
19 /**\r
20  * サーバーとクライアントのデータの同期に必要なクラス\r
21  *\r
22  * @author Lilly\r
23  *\r
24  */\r
25 public class PacketHandler implements IPacketHandler {\r
26 \r
27         @Override\r
28         public void onPacketData(NetworkManager manager, Packet250CustomPayload packet, Player player) {\r
29                 if (packet.channel.equals("chemicraft")) {\r
30                         ByteArrayDataInput var1 = ByteStreams.newDataInput(packet.data);\r
31                         int x, y, z;\r
32                         String tab;\r
33 \r
34                         // パケットからデータ読み込み\r
35                         x = var1.readInt();\r
36                         y = var1.readInt();\r
37                         z = var1.readInt();\r
38                         tab = var1.readUTF();\r
39 \r
40                         // TileEntityを取得し読み込んだデータを送る\r
41                         World world = ChemiCraft.proxy.getClientWorld();\r
42                         TileEntity tileEntity = world.getBlockTileEntity(x, y, z);\r
43                         if (tileEntity instanceof TileEntityScienceCraftingTable) {\r
44                                 TileEntityScienceCraftingTable var2 = (TileEntityScienceCraftingTable) tileEntity;\r
45                                 var2.handlePacket(new Object[]{tab});\r
46                         }\r
47                 }\r
48         }\r
49 \r
50 \r
51         public static Packet getPacket(TileEntityScienceCraftingTable tileEntity) {\r
52                 ByteArrayOutputStream var1 = new ByteArrayOutputStream(128);\r
53                 DataOutputStream var2 = new DataOutputStream(var1);\r
54                 int x, y, z;\r
55                 String tab;\r
56 \r
57                 // データの用意\r
58                 x = tileEntity.xCoord;\r
59                 y = tileEntity.yCoord;\r
60                 z = tileEntity.zCoord;\r
61                 tab = tileEntity.getActiveTab();\r
62 \r
63                 try {\r
64                         var2.writeInt(x);\r
65                         var2.writeInt(y);\r
66                         var2.writeInt(z);\r
67                         var2.writeUTF(tab);\r
68                 } catch (IOException e) {\r
69                         e.printStackTrace();\r
70                 }\r
71 \r
72                 // パケットの作成\r
73                 Packet250CustomPayload packet = new Packet250CustomPayload();\r
74                 packet.channel = "chemicraft";\r
75                 packet.data = var1.toByteArray();\r
76                 packet.length = var1.size();\r
77                 packet.isChunkDataPacket = true;\r
78                 return packet;\r
79         }\r
80 }\r