OSDN Git Service

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