OSDN Git Service

GUIのSlot追加
[chemicraft/chemicraft.git] / common / chemicraft / PacketHandler.java
1 package chemicraft;\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 \r
14 import com.google.common.io.ByteArrayDataInput;\r
15 import com.google.common.io.ByteStreams;\r
16 \r
17 import cpw.mods.fml.common.network.IPacketHandler;\r
18 import cpw.mods.fml.common.network.Player;\r
19 \r
20 /**\r
21  * サーバーとクライアントのデータの同期に必要なクラス\r
22  *\r
23  * @author Lilly\r
24  *\r
25  */\r
26 public class PacketHandler implements IPacketHandler {\r
27 \r
28         @Override\r
29         public void onPacketData(NetworkManager manager, Packet250CustomPayload packet, Player player) {\r
30                 if (packet.channel.equals("chemicraft")) {\r
31                         ByteArrayDataInput var1 = ByteStreams.newDataInput(packet.data);\r
32 \r
33                         World worldClient;\r
34                         World worldServer;\r
35                         TileEntity tileEntity;\r
36                         TileEntityChemicalCraftingTable var2;\r
37 \r
38                         // TileEntityのx, y, z座標\r
39                         int x, y, z;\r
40 \r
41                         // 座標読み込み\r
42                         x = var1.readInt();\r
43                         y = var1.readInt();\r
44                         z = var1.readInt();\r
45 \r
46                         worldClient = ChemiCraft.instance.proxy.getClientWorld();\r
47                         worldServer = ((EntityPlayer)player).worldObj;\r
48                         if(worldClient != null){\r
49                                 tileEntity = worldClient.getBlockTileEntity(x, y, z);\r
50                                 if (tileEntity instanceof TileEntityChemicalCraftingTable) {\r
51                                         var2 = (TileEntityChemicalCraftingTable) tileEntity;\r
52                                         var2.readPacket(var1);\r
53                                 }\r
54                                 return;\r
55                         }\r
56                         if(worldServer != null){\r
57                                 tileEntity = worldServer.getBlockTileEntity(x, y, z);\r
58                                 if (tileEntity instanceof TileEntityChemicalCraftingTable) {\r
59                                         var2 = (TileEntityChemicalCraftingTable) tileEntity;\r
60                                         var2.readPacket(var1);\r
61                                 }\r
62                                 return;\r
63                         }\r
64                 }\r
65         }\r
66 \r
67 \r
68         public static Packet getPacket(TileEntityChemicalCraftingTable tileEntity) {\r
69                 ByteArrayOutputStream var1 = new ByteArrayOutputStream(128);\r
70                 DataOutputStream var2 = new DataOutputStream(var1);\r
71 \r
72                 // Blockのx, y, z座標\r
73                 int x, y, z;\r
74 \r
75                 // activeTab\r
76                 String activeTab;\r
77 \r
78                 // x, y, z座標を代入\r
79                 x = tileEntity.xCoord;\r
80                 y = tileEntity.yCoord;\r
81                 z = tileEntity.zCoord;\r
82 \r
83                 try {\r
84                         // 座標書き込み\r
85                         var2.writeInt(x);\r
86                         var2.writeInt(y);\r
87                         var2.writeInt(z);\r
88                         tileEntity.writePacket(var2);\r
89                 } catch (IOException e) {\r
90                         e.printStackTrace();\r
91                 }\r
92 \r
93                 // パケットの作成\r
94                 Packet250CustomPayload packet = new Packet250CustomPayload();\r
95                 packet.channel = "chemicraft";\r
96                 packet.data = var1.toByteArray();\r
97                 packet.length = var1.size();\r
98                 packet.isChunkDataPacket = true;\r
99                 return packet;\r
100         }\r
101 }\r