-
Notifications
You must be signed in to change notification settings - Fork 1
/
PacketHandler.java
90 lines (72 loc) · 2.26 KB
/
PacketHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package net.minecraft.graffiti;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Random;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;
public class PacketHandler implements IPacketHandler
{
private Random rand = new Random();
@Override
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player)
{
ByteArrayDataInput data = ByteStreams.newDataInput(packet.data);
int x = data.readInt();
int y = data.readInt();
int z = data.readInt();
TileEntity tileentity = null;
World worldClient = FMLClientHandler.instance().getClient().theWorld;
World worldServer = ((EntityPlayer)player).worldObj;
if(worldClient != null && worldServer == null)
{
tileentity = worldClient.getBlockTileEntity(x, y, z);
}
if(worldServer != null)
{
tileentity = worldServer.getBlockTileEntity(x, y, z);
}
if (tileentity != null) {
if(tileentity instanceof TileEntityGraffitiBlock)
{
((TileEntityGraffitiBlock)tileentity).readToPacket(data);
}
}
}
public static Packet getPacket(TileEntityGraffitiBlock tileentity)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
int x = tileentity.xCoord;
int y = tileentity.yCoord;
int z = tileentity.zCoord;
try
{
dos.writeInt(x);
dos.writeInt(y);
dos.writeInt(z);
tileentity.writeToPacket(dos);
}
catch (Exception e)
{
e.printStackTrace();
}
Packet250CustomPayload packet = new Packet250CustomPayload();
packet.data = bos.toByteArray();
packet.length = bos.size();
packet.isChunkDataPacket = true;
packet.channel = "graffiti";
return packet;
}
}