OSDN Git Service

Merge branch 'master' of git.sourceforge.jp:/gitroot/chemicraft/chemicraft
[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 <<<<<<< HEAD
35                         //activeTab\r
36                         String activeTab;\r
37 =======
38                         String tab;\r
39 >>>>>>> 820e884ca2933199d4be03f7de49a491a7f9e93e
40 \r
41                         //座標読み込み\r
42                         x = var1.readInt();\r
43                         y = var1.readInt();\r
44                         z = var1.readInt();\r
45 <<<<<<< HEAD
46                         //activeTab読み込み\r
47                         activeTab = var1.readUTF();\r
48 =======
49                         tab = var1.readUTF();\r
50 >>>>>>> 820e884ca2933199d4be03f7de49a491a7f9e93e
51 \r
52                         // TileEntityを取得し読み込んだデータを送る\r
53                         World world = ChemiCraft.proxy.getClientWorld();\r
54                         TileEntity tileEntity = world.getBlockTileEntity(x, y, z);\r
55                         if (tileEntity instanceof TileEntityScienceCraftingTable) {\r
56                                 TileEntityScienceCraftingTable var2 = (TileEntityScienceCraftingTable) tileEntity;\r
57 <<<<<<< HEAD
58                                 var2.setActiveTab(activeTab);\r
59 =======
60                                 var2.handlePacket(new Object[]{tab});\r
61 >>>>>>> 820e884ca2933199d4be03f7de49a491a7f9e93e
62                         }\r
63                 }\r
64         }\r
65 \r
66 \r
67         public static Packet getPacket(TileEntityScienceCraftingTable tileEntity) {\r
68                 ByteArrayOutputStream var1 = new ByteArrayOutputStream(128);\r
69                 DataOutputStream var2 = new DataOutputStream(var1);\r
70 \r
71                 //Blockのx, y, z座標\r
72                 int x, y, z;\r
73 <<<<<<< HEAD
74                 //activeTab\r
75                 String activeTab;\r
76 =======
77                 String tab;\r
78 >>>>>>> 820e884ca2933199d4be03f7de49a491a7f9e93e
79 \r
80                 // x, y, z座標を代入\r
81                 x = tileEntity.xCoord;\r
82                 y = tileEntity.yCoord;\r
83                 z = tileEntity.zCoord;\r
84 <<<<<<< HEAD
85                 //activeTabに代入\r
86                 activeTab = tileEntity.getActiveTab();\r
87 =======
88                 tab = tileEntity.getActiveTab();\r
89 >>>>>>> 820e884ca2933199d4be03f7de49a491a7f9e93e
90 \r
91                 try {\r
92                         var2.writeInt(x);\r
93                         var2.writeInt(y);\r
94                         var2.writeInt(z);\r
95 <<<<<<< HEAD
96                         var2.writeUTF(activeTab);\r
97 =======
98                         var2.writeUTF(tab);\r
99 >>>>>>> 820e884ca2933199d4be03f7de49a491a7f9e93e
100                 } catch (IOException e) {\r
101                         e.printStackTrace();\r
102                 }\r
103 \r
104                 // パケットの作成\r
105                 Packet250CustomPayload packet = new Packet250CustomPayload();\r
106                 packet.channel = "chemicraft";\r
107                 packet.data = var1.toByteArray();\r
108                 packet.length = var1.size();\r
109                 packet.isChunkDataPacket = true;\r
110                 return packet;\r
111         }\r
112 }\r