OSDN Git Service

a55e9c5be2a7ab61ba5a49b7a85e45da9858a525
[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 \r
33             // パケットからデータ読み込み\r
34             x = var1.readInt();\r
35             y = var1.readInt();\r
36             z = var1.readInt();\r
37 \r
38             // TileEntityを取得し読み込んだデータを送る\r
39             World world = ChemiCraft.proxy.getClientWorld();\r
40             TileEntity tileEntity = world.getBlockTileEntity(x, y, z);\r
41             if (tileEntity instanceof TileEntityScienceCraftingTable) {\r
42                 TileEntityScienceCraftingTable var2 = (TileEntityScienceCraftingTable) tileEntity;\r
43                 var2.handlePacket(null);\r
44             }\r
45         }\r
46     }\r
47 \r
48 \r
49     public static Packet getPacket(TileEntityScienceCraftingTable tileEntity) {\r
50         ByteArrayOutputStream var1 = new ByteArrayOutputStream(128);\r
51         DataOutputStream var2 = new DataOutputStream(var1);\r
52         int x, y, z;\r
53 \r
54         // データの用意\r
55         x = tileEntity.xCoord;\r
56         y = tileEntity.yCoord;\r
57         z = tileEntity.zCoord;\r
58 \r
59         try {\r
60             var2.writeInt(x);\r
61             var2.writeInt(y);\r
62             var2.writeInt(z);\r
63         } catch (IOException e) {\r
64             e.printStackTrace();\r
65         }\r
66 \r
67         // パケットの作成\r
68         Packet250CustomPayload packet = new Packet250CustomPayload();\r
69         packet.channel = "chemicraft";\r
70         packet.data = var1.toByteArray();\r
71         packet.length = var1.size();\r
72         packet.isChunkDataPacket = true;\r
73         return packet;\r
74     }\r
75 }\r