OSDN Git Service

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