OSDN Git Service

修正・変更
[chemicraft/chemicraft.git] / common / pcc / chemicraft / core / system / PacketHandler.java
1 package pcc.chemicraft.core.system;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.DataOutputStream;
5 import java.io.IOException;
6
7 import net.minecraft.entity.player.EntityPlayer;
8 import net.minecraft.network.INetworkManager;
9 import net.minecraft.network.packet.Packet;
10 import net.minecraft.network.packet.Packet250CustomPayload;
11 import net.minecraft.tileentity.TileEntity;
12 import net.minecraft.world.World;
13 import pcc.chemicraft.core.ChemiCraftCore;
14 import pcc.chemicraft.core.tileentity.TileEntityChemicalCombinationTable;
15 import pcc.chemicraft.core.tileentity.TileEntityChemicalCraftingTable;
16 import pcc.chemicraft.core.tileentity.TileEntityElectrolysisTable;
17 import pcc.chemicraft.core.tileentity.TileEntityPyrolysisTable;
18 import pcc.chemicraft.core.tileentity.TileEntityToolAndWeaponCraftingTable;
19
20 import com.google.common.io.ByteArrayDataInput;
21 import com.google.common.io.ByteStreams;
22
23 import cpw.mods.fml.common.network.IPacketHandler;
24 import cpw.mods.fml.common.network.Player;
25
26 /**
27  * サーバーとクライアントのデータの同期に必要なクラス
28  *
29  * @author Lilly
30  *
31  */
32 public class PacketHandler implements IPacketHandler {
33
34         @Override
35         public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) {
36                 if (packet.channel.equals("chemicraftcore")) {
37                         ByteArrayDataInput badi = ByteStreams.newDataInput(packet.data);
38
39                         World worldClient;
40                         World worldServer;
41                         TileEntity var1;
42
43                         // TileEntityのx, y, z座標
44                         int x, y, z;
45
46                         // 座標読み込み
47                         x = badi.readInt();
48                         y = badi.readInt();
49                         z = badi.readInt();
50
51                         worldClient = ChemiCraftCore.proxy.getClientWorld();
52                         worldServer = ((EntityPlayer)player).worldObj;
53                         if(worldClient != null && worldServer == null){
54                                 var1 = worldClient.getBlockTileEntity(x, y, z);
55                                 if(var1 instanceof TileEntityChemicalCombinationTable) ((TileEntityChemicalCombinationTable)var1).readPacket(badi);
56                                 if(var1 instanceof TileEntityChemicalCraftingTable) ((TileEntityChemicalCraftingTable)var1).readPacket(badi);
57                                 if(var1 instanceof TileEntityPyrolysisTable) ((TileEntityPyrolysisTable)var1).readPacket(badi);
58                                 if(var1 instanceof TileEntityToolAndWeaponCraftingTable) ((TileEntityToolAndWeaponCraftingTable)var1).readPacket(badi);
59                                 return;
60                         }
61                         if(worldServer != null){
62                                 var1 = worldServer.getBlockTileEntity(x, y, z);
63                                 if(var1 instanceof TileEntityChemicalCombinationTable) ((TileEntityChemicalCombinationTable)var1).readPacket(badi);
64                                 if(var1 instanceof TileEntityChemicalCraftingTable) ((TileEntityChemicalCraftingTable)var1).readPacket(badi);
65                                 if(var1 instanceof TileEntityPyrolysisTable) ((TileEntityPyrolysisTable)var1).readPacket(badi);
66                                 if(var1 instanceof TileEntityToolAndWeaponCraftingTable) ((TileEntityToolAndWeaponCraftingTable)var1).readPacket(badi);
67                         }
68                         return;
69                 }
70         }
71
72
73         public static Packet getPacket(TileEntityPyrolysisTable tileEntity) {
74                 ByteArrayOutputStream var1 = new ByteArrayOutputStream(128);
75                 DataOutputStream var2 = new DataOutputStream(var1);
76
77                 // Blockのx, y, z座標
78                 int x, y, z;
79
80                 // x, y, z座標を代入
81                 x = tileEntity.xCoord;
82                 y = tileEntity.yCoord;
83                 z = tileEntity.zCoord;
84
85                 try {
86                         // 座標書き込み
87                         var2.writeInt(x);
88                         var2.writeInt(y);
89                         var2.writeInt(z);
90                         tileEntity.writePacket(var2);
91                 } catch (IOException e) {
92                         e.printStackTrace();
93                 }
94
95                 // パケットの作成
96                 Packet250CustomPayload packet = new Packet250CustomPayload();
97                 packet.channel = "chemicraftcore";
98                 packet.data = var1.toByteArray();
99                 packet.length = var1.size();
100                 packet.isChunkDataPacket = true;
101                 return packet;
102         }
103
104         public static Packet getPacket(TileEntityChemicalCombinationTable tileEntity) {
105                 ByteArrayOutputStream var1 = new ByteArrayOutputStream(128);
106                 DataOutputStream var2 = new DataOutputStream(var1);
107
108                 // Blockのx, y, z座標
109                 int x, y, z;
110
111                 // x, y, z座標を代入
112                 x = tileEntity.xCoord;
113                 y = tileEntity.yCoord;
114                 z = tileEntity.zCoord;
115
116                 try {
117                         // 座標書き込み
118                         var2.writeInt(x);
119                         var2.writeInt(y);
120                         var2.writeInt(z);
121                         tileEntity.writePacket(var2);
122                 } catch (IOException e) {
123                         e.printStackTrace();
124                 }
125
126                 // パケットの作成
127                 Packet250CustomPayload packet = new Packet250CustomPayload();
128                 packet.channel = "chemicraftcore";
129                 packet.data = var1.toByteArray();
130                 packet.length = var1.size();
131                 packet.isChunkDataPacket = true;
132                 return packet;
133         }
134
135         public static Packet getPacket(TileEntityToolAndWeaponCraftingTable tileEntity) {
136                 ByteArrayOutputStream var1 = new ByteArrayOutputStream(128);
137                 DataOutputStream var2 = new DataOutputStream(var1);
138
139                 // Blockのx, y, z座標
140                 int x, y, z;
141
142                 // x, y, z座標を代入
143                 x = tileEntity.xCoord;
144                 y = tileEntity.yCoord;
145                 z = tileEntity.zCoord;
146
147                 try {
148                         // 座標書き込み
149                         var2.writeInt(x);
150                         var2.writeInt(y);
151                         var2.writeInt(z);
152                         tileEntity.writePacket(var2);
153                 } catch (IOException e) {
154                         e.printStackTrace();
155                 }
156
157                 // パケットの作成
158                 Packet250CustomPayload packet = new Packet250CustomPayload();
159                 packet.channel = "chemicraftcore";
160                 packet.data = var1.toByteArray();
161                 packet.length = var1.size();
162                 packet.isChunkDataPacket = true;
163                 return packet;
164         }
165
166         public static Packet getPacket(TileEntityChemicalCraftingTable tileEntity) {
167                 ByteArrayOutputStream var1 = new ByteArrayOutputStream(128);
168                 DataOutputStream var2 = new DataOutputStream(var1);
169
170                 // Blockのx, y, z座標
171                 int x, y, z;
172
173                 // x, y, z座標を代入
174                 x = tileEntity.xCoord;
175                 y = tileEntity.yCoord;
176                 z = tileEntity.zCoord;
177
178                 try {
179                         // 座標書き込み
180                         var2.writeInt(x);
181                         var2.writeInt(y);
182                         var2.writeInt(z);
183                         tileEntity.writePacket(var2);
184                 } catch (IOException e) {
185                         e.printStackTrace();
186                 }
187
188                 // パケットの作成
189                 Packet250CustomPayload packet = new Packet250CustomPayload();
190                 packet.channel = "chemicraftcore";
191                 packet.data = var1.toByteArray();
192                 packet.length = var1.size();
193                 packet.isChunkDataPacket = true;
194                 return packet;
195         }
196
197
198         public static Packet getPacket(TileEntityElectrolysisTable tileEntity)
199         {
200                 ByteArrayOutputStream var1 = new ByteArrayOutputStream(128);
201                 DataOutputStream var2 = new DataOutputStream(var1);
202
203                 // Blockのx, y, z座標
204                 int x, y, z;
205
206                 // x, y, z座標を代入
207                 x = tileEntity.xCoord;
208                 y = tileEntity.yCoord;
209                 z = tileEntity.zCoord;
210
211                 try {
212                         // 座標書き込み
213                         var2.writeInt(x);
214                         var2.writeInt(y);
215                         var2.writeInt(z);
216                         tileEntity.writePacket(var2);
217                 } catch (IOException e) {
218                         e.printStackTrace();
219                 }
220
221                 // パケットの作成
222                 Packet250CustomPayload packet = new Packet250CustomPayload();
223                 packet.channel = "chemicraftcore";
224                 packet.data = var1.toByteArray();
225                 packet.length = var1.size();
226                 packet.isChunkDataPacket = true;
227                 return packet;
228         }
229
230 }