OSDN Git Service

PacketHandler.javaにactuveTabを送受信に追加
[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 \r
32                         //TileEntityのx, y, z座標\r
33                         int x, y, z;\r
34                         //activeTab\r
35                         String activeTab;\r
36 \r
37                         //座標読み込み\r
38                         x = var1.readInt();\r
39                         y = var1.readInt();\r
40                         z = var1.readInt();\r
41                         //activeTab読み込み\r
42                         activeTab = var1.readUTF();\r
43 \r
44                         // TileEntityを取得し読み込んだデータを送る\r
45                         World world = ChemiCraft.proxy.getClientWorld();\r
46                         TileEntity tileEntity = world.getBlockTileEntity(x, y, z);\r
47                         if (tileEntity instanceof TileEntityScienceCraftingTable) {\r
48                                 TileEntityScienceCraftingTable var2 = (TileEntityScienceCraftingTable) tileEntity;\r
49                                 var2.setActiveTab(activeTab);\r
50                         }\r
51                 }\r
52         }\r
53 \r
54 \r
55         public static Packet getPacket(TileEntityScienceCraftingTable tileEntity) {\r
56                 ByteArrayOutputStream var1 = new ByteArrayOutputStream(128);\r
57                 DataOutputStream var2 = new DataOutputStream(var1);\r
58 \r
59                 //Blockのx, y, z座標\r
60                 int x, y, z;\r
61                 //activeTab\r
62                 String activeTab;\r
63 \r
64                 // x, y, z座標を代入\r
65                 x = tileEntity.xCoord;\r
66                 y = tileEntity.yCoord;\r
67                 z = tileEntity.zCoord;\r
68                 //activeTabに代入\r
69                 activeTab = tileEntity.getActiveTab();\r
70 \r
71                 try {\r
72                         var2.writeInt(x);\r
73                         var2.writeInt(y);\r
74                         var2.writeInt(z);\r
75                         var2.writeUTF(activeTab);\r
76                 } catch (IOException e) {\r
77                         e.printStackTrace();\r
78                 }\r
79 \r
80                 // パケットの作成\r
81                 Packet250CustomPayload packet = new Packet250CustomPayload();\r
82                 packet.channel = "chemicraft";\r
83                 packet.data = var1.toByteArray();\r
84                 packet.length = var1.size();\r
85                 packet.isChunkDataPacket = true;\r
86                 return packet;\r
87         }\r
88 }\r