2013-07-16 17:58:02 +10:00
|
|
|
package codechicken.lib.packet;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import codechicken.lib.data.MCDataInput;
|
|
|
|
|
import codechicken.lib.data.MCDataOutput;
|
|
|
|
|
import codechicken.lib.vec.BlockCoord;
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
|
|
|
|
import cpw.mods.fml.common.ModContainer;
|
|
|
|
|
import cpw.mods.fml.common.network.*;
|
|
|
|
|
import cpw.mods.fml.common.network.handshake.NetworkDispatcher;
|
|
|
|
|
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
|
2013-07-16 17:58:02 +10:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
|
import io.netty.channel.ChannelHandler;
|
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
|
|
|
import io.netty.channel.SimpleChannelInboundHandler;
|
|
|
|
|
import io.netty.handler.codec.compression.Snappy;
|
|
|
|
|
import io.netty.util.AttributeKey;
|
2013-07-16 17:58:02 +10:00
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
2014-04-07 14:02:47 +10:00
|
|
|
import net.minecraft.item.Item;
|
2013-07-16 17:58:02 +10:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2014-04-07 14:02:47 +10:00
|
|
|
import net.minecraft.network.INetHandler;
|
|
|
|
|
import net.minecraft.network.NetHandlerPlayServer;
|
|
|
|
|
import net.minecraft.network.Packet;
|
|
|
|
|
import net.minecraft.network.play.INetHandlerPlayClient;
|
|
|
|
|
import net.minecraft.network.play.INetHandlerPlayServer;
|
2013-07-16 17:58:02 +10:00
|
|
|
import net.minecraft.server.MinecraftServer;
|
2014-04-09 13:20:40 +10:00
|
|
|
import net.minecraft.server.management.PlayerManager;
|
2014-04-07 14:02:47 +10:00
|
|
|
import net.minecraft.server.management.PlayerManager.PlayerInstance;
|
2013-07-16 17:58:02 +10:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
import net.minecraft.world.WorldServer;
|
|
|
|
|
import net.minecraftforge.fluids.FluidStack;
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
import java.util.EnumMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.zip.Deflater;
|
|
|
|
|
import java.util.zip.Inflater;
|
|
|
|
|
|
2013-07-16 17:58:02 +10:00
|
|
|
public final class PacketCustom implements MCDataInput, MCDataOutput
|
|
|
|
|
{
|
2014-04-07 14:02:47 +10:00
|
|
|
public static interface ICustomPacketHandler
|
2013-07-16 17:58:02 +10:00
|
|
|
{
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
2013-07-16 17:58:02 +10:00
|
|
|
public interface IClientPacketHandler extends ICustomPacketHandler
|
|
|
|
|
{
|
2014-04-07 14:02:47 +10:00
|
|
|
public void handlePacket(PacketCustom packetCustom, Minecraft mc, INetHandlerPlayClient handler);
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
2013-07-16 17:58:02 +10:00
|
|
|
public interface IServerPacketHandler extends ICustomPacketHandler
|
|
|
|
|
{
|
2014-04-07 14:02:47 +10:00
|
|
|
public void handlePacket(PacketCustom packetCustom, EntityPlayerMP sender, INetHandlerPlayServer handler);
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public static AttributeKey<CustomInboundHandler> cclHandler = new AttributeKey<CustomInboundHandler>("ccl:handler");
|
2013-07-16 17:58:02 +10:00
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
@ChannelHandler.Sharable
|
|
|
|
|
public static class CustomInboundHandler extends SimpleChannelInboundHandler<FMLProxyPacket>
|
2013-07-16 17:58:02 +10:00
|
|
|
{
|
2014-04-07 14:02:47 +10:00
|
|
|
public EnumMap<Side, CustomHandler> handlers = Maps.newEnumMap(Side.class);
|
2013-07-16 17:58:02 +10:00
|
|
|
|
|
|
|
|
@Override
|
2014-04-07 14:02:47 +10:00
|
|
|
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
|
super.handlerAdded(ctx);
|
|
|
|
|
ctx.channel().attr(cclHandler).set(this);
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-04-07 14:02:47 +10:00
|
|
|
protected void channelRead0(ChannelHandlerContext ctx, FMLProxyPacket msg) throws Exception {
|
|
|
|
|
handlers.get(ctx.channel().attr(NetworkRegistry.CHANNEL_SOURCE).get())
|
|
|
|
|
.handle(ctx.channel().attr(NetworkRegistry.NET_HANDLER).get(),
|
|
|
|
|
ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get(),
|
|
|
|
|
new PacketCustom(msg.payload()));
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
private static interface CustomHandler
|
2013-07-16 17:58:02 +10:00
|
|
|
{
|
2014-04-07 14:02:47 +10:00
|
|
|
public void handle(INetHandler handler, String channel, PacketCustom packet) throws Exception;
|
|
|
|
|
}
|
2013-07-16 17:58:02 +10:00
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public static class ClientInboundHandler implements CustomHandler
|
|
|
|
|
{
|
|
|
|
|
private IClientPacketHandler handler;
|
|
|
|
|
|
|
|
|
|
public ClientInboundHandler(ICustomPacketHandler handler) {
|
|
|
|
|
this.handler = (IClientPacketHandler) handler;
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-04-07 14:02:47 +10:00
|
|
|
public void handle(INetHandler netHandler, String channel, PacketCustom packet) throws Exception {
|
|
|
|
|
if (netHandler instanceof INetHandlerPlayClient)
|
|
|
|
|
handler.handlePacket(packet, Minecraft.getMinecraft(), (INetHandlerPlayClient) netHandler);
|
|
|
|
|
else
|
|
|
|
|
System.err.println("Invalid INetHandler for PacketCustom on channel: " + channel);
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public static class ServerInboundHandler implements CustomHandler
|
2013-07-16 17:58:02 +10:00
|
|
|
{
|
2014-04-07 14:02:47 +10:00
|
|
|
private IServerPacketHandler handler;
|
2013-07-16 17:58:02 +10:00
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public ServerInboundHandler(ICustomPacketHandler handler) {
|
|
|
|
|
this.handler = (IServerPacketHandler) handler;
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
@Override
|
|
|
|
|
public void handle(INetHandler netHandler, String channel, PacketCustom packet) throws Exception {
|
|
|
|
|
if (netHandler instanceof NetHandlerPlayServer)
|
|
|
|
|
handler.handlePacket(packet, ((NetHandlerPlayServer) netHandler).playerEntity, (INetHandlerPlayServer) netHandler);
|
2013-07-16 17:58:02 +10:00
|
|
|
else
|
2014-04-07 14:02:47 +10:00
|
|
|
System.err.println("Invalid INetHandler for PacketCustom on channel: " + channel);
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public static interface IHandshakeHandler
|
2013-08-05 08:23:26 +10:00
|
|
|
{
|
2014-04-07 14:02:47 +10:00
|
|
|
public void handshakeRecieved(NetHandlerPlayServer netHandler);
|
2013-08-05 08:23:26 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public static class HandshakeInboundHandler extends ChannelInboundHandlerAdapter
|
2013-07-16 17:58:02 +10:00
|
|
|
{
|
2014-04-07 14:02:47 +10:00
|
|
|
public IHandshakeHandler handler;
|
2013-07-16 17:58:02 +10:00
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public HandshakeInboundHandler(IHandshakeHandler handler) {
|
|
|
|
|
this.handler = handler;
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-04-07 14:02:47 +10:00
|
|
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
|
|
|
|
if (evt instanceof NetworkHandshakeEstablished) {
|
|
|
|
|
INetHandler netHandler = ((NetworkDispatcher) ctx.channel().attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).get()).getNetHandler();
|
|
|
|
|
if (netHandler instanceof NetHandlerPlayServer)
|
|
|
|
|
handler.handshakeRecieved((NetHandlerPlayServer) netHandler);
|
|
|
|
|
} else
|
|
|
|
|
ctx.fireUserEventTriggered(evt);
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public static String channelName(Object channelKey) {
|
|
|
|
|
if (channelKey instanceof String)
|
|
|
|
|
return (String) channelKey;
|
|
|
|
|
if (channelKey instanceof ModContainer)
|
|
|
|
|
return ((ModContainer) channelKey).getModId();
|
2013-07-16 17:58:02 +10:00
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
ModContainer mc = FMLCommonHandler.instance().findContainerFor(channelKey);
|
|
|
|
|
if (mc != null)
|
|
|
|
|
return mc.getModId();
|
2013-07-16 17:58:02 +10:00
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
throw new IllegalArgumentException("Invalid channel: " + channelKey);
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public static FMLEmbeddedChannel getOrCreateChannel(String channelName, Side side) {
|
|
|
|
|
if (!NetworkRegistry.INSTANCE.hasChannel(channelName, side))
|
|
|
|
|
NetworkRegistry.INSTANCE.newChannel(channelName, new CustomInboundHandler());
|
|
|
|
|
return NetworkRegistry.INSTANCE.getChannel(channelName, side);
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public static void assignHandler(Object channelKey, ICustomPacketHandler handler) {
|
|
|
|
|
String channelName = channelName(channelKey);
|
|
|
|
|
Side side = handler instanceof IServerPacketHandler ? Side.SERVER : Side.CLIENT;
|
|
|
|
|
FMLEmbeddedChannel channel = getOrCreateChannel(channelName, side);
|
|
|
|
|
channel.attr(cclHandler).get().handlers.put(side, side == Side.SERVER ? new ServerInboundHandler(handler) : new ClientInboundHandler(handler));
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public static void assignHandshakeHandler(Object channelKey, IHandshakeHandler handler) {
|
|
|
|
|
FMLEmbeddedChannel channel = getOrCreateChannel(channelName(channelKey), Side.SERVER);
|
|
|
|
|
channel.pipeline().addLast(new HandshakeInboundHandler(handler));
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
private ByteBuf byteBuf;
|
|
|
|
|
private String channel;
|
|
|
|
|
private int type;
|
|
|
|
|
|
|
|
|
|
public PacketCustom(ByteBuf payload) {
|
|
|
|
|
byteBuf = payload;
|
|
|
|
|
|
|
|
|
|
type = byteBuf.readUnsignedByte();
|
|
|
|
|
if (type > 0x80)
|
|
|
|
|
decompress();
|
|
|
|
|
type &= 0x7F;
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public PacketCustom(Object channelKey, int type) {
|
|
|
|
|
if (type <= 0 || type >= 0x80)
|
|
|
|
|
throw new IllegalArgumentException("Packet type: " + type + " is not within required 0 < t < 0x80");
|
|
|
|
|
|
|
|
|
|
this.channel = channelName(channelKey);
|
|
|
|
|
this.type = type;
|
|
|
|
|
byteBuf = Unpooled.buffer();
|
|
|
|
|
byteBuf.writeByte(type);
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Decompresses the remaining ByteBuf (after type has been read) using Snappy
|
|
|
|
|
*/
|
|
|
|
|
private void decompress() {
|
2013-07-16 17:58:02 +10:00
|
|
|
Inflater inflater = new Inflater();
|
2014-04-07 14:02:47 +10:00
|
|
|
try {
|
|
|
|
|
int len = byteBuf.readInt();
|
|
|
|
|
ByteBuf out = Unpooled.buffer(len);
|
|
|
|
|
inflater.setInput(byteBuf.array(), byteBuf.readerIndex(), byteBuf.readableBytes());
|
|
|
|
|
inflater.inflate(out.array());
|
|
|
|
|
out.writerIndex(len);
|
|
|
|
|
byteBuf = out;
|
|
|
|
|
} catch (Exception e) {
|
2013-07-16 17:58:02 +10:00
|
|
|
throw new RuntimeException(e);
|
2014-04-07 14:02:47 +10:00
|
|
|
} finally {
|
2013-07-16 17:58:02 +10:00
|
|
|
inflater.end();
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Compresses the payload ByteBuf after the type byte
|
|
|
|
|
*/
|
|
|
|
|
private void do_compress() {
|
2013-07-16 17:58:02 +10:00
|
|
|
Deflater deflater = new Deflater();
|
2014-04-07 14:02:47 +10:00
|
|
|
try {
|
|
|
|
|
byteBuf.readerIndex(1);
|
|
|
|
|
int len = byteBuf.readableBytes();
|
|
|
|
|
deflater.setInput(byteBuf.array(), byteBuf.readerIndex(), len);
|
2013-07-16 17:58:02 +10:00
|
|
|
deflater.finish();
|
2014-04-07 14:02:47 +10:00
|
|
|
ByteBuf out = Unpooled.buffer(len + 5);
|
|
|
|
|
int clen = deflater.deflate(out.array(), 5, len);
|
|
|
|
|
if (clen >= len - 5 || !deflater.finished())//not worth compressing, gets larger
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
out.setByte(0, type | 0x80);
|
|
|
|
|
out.setInt(1, len);
|
|
|
|
|
out.writerIndex(clen + 5);
|
|
|
|
|
byteBuf = out;
|
|
|
|
|
} catch (Exception e) {
|
2013-07-16 17:58:02 +10:00
|
|
|
throw new RuntimeException(e);
|
2014-04-07 14:02:47 +10:00
|
|
|
} finally {
|
2013-07-16 17:58:02 +10:00
|
|
|
deflater.end();
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public boolean incoming() {
|
|
|
|
|
return channel == null;
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public int getType() {
|
|
|
|
|
return type & 0x7F;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ByteBuf getByteBuf() {
|
|
|
|
|
return byteBuf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PacketCustom compress() {
|
|
|
|
|
if (incoming())
|
|
|
|
|
throw new IllegalStateException("Tried to compress an incoming packet");
|
|
|
|
|
if ((type & 0x80) != 0)
|
|
|
|
|
throw new IllegalStateException("Packet already compressed");
|
|
|
|
|
type |= 0x80;
|
2013-07-16 17:58:02 +10:00
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public PacketCustom writeBoolean(boolean b) {
|
|
|
|
|
byteBuf.writeBoolean(b);
|
2013-07-16 17:58:02 +10:00
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public PacketCustom writeByte(int b) {
|
|
|
|
|
byteBuf.writeByte(b);
|
2013-07-16 17:58:02 +10:00
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public PacketCustom writeShort(int s) {
|
|
|
|
|
byteBuf.writeShort(s);
|
2013-07-16 17:58:02 +10:00
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public PacketCustom writeInt(int i) {
|
|
|
|
|
byteBuf.writeInt(i);
|
2013-07-16 17:58:02 +10:00
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public PacketCustom writeFloat(float f) {
|
|
|
|
|
byteBuf.writeFloat(f);
|
2013-07-16 17:58:02 +10:00
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public PacketCustom writeDouble(double d) {
|
|
|
|
|
byteBuf.writeDouble(d);
|
2013-07-16 17:58:02 +10:00
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public PacketCustom writeLong(long l) {
|
|
|
|
|
byteBuf.writeLong(l);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 17:58:02 +10:00
|
|
|
@Override
|
2014-04-07 14:02:47 +10:00
|
|
|
public PacketCustom writeChar(char c) {
|
|
|
|
|
byteBuf.writeChar(c);
|
2013-07-16 17:58:02 +10:00
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public PacketCustom writeVarInt(int i) {
|
|
|
|
|
ByteBufUtils.writeVarInt(byteBuf, i, 5);
|
2013-07-16 17:58:02 +10:00
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public PacketCustom writeVarShort(int s) {
|
|
|
|
|
ByteBufUtils.writeVarShort(byteBuf, s);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PacketCustom writeByteArray(byte[] barray) {
|
|
|
|
|
byteBuf.writeBytes(barray);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PacketCustom writeString(String s) {
|
|
|
|
|
ByteBufUtils.writeUTF8String(byteBuf, s);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PacketCustom writeCoord(int x, int y, int z) {
|
2013-07-16 17:58:02 +10:00
|
|
|
writeInt(x);
|
|
|
|
|
writeInt(y);
|
|
|
|
|
writeInt(z);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public PacketCustom writeCoord(BlockCoord coord) {
|
2013-07-16 17:58:02 +10:00
|
|
|
writeInt(coord.x);
|
|
|
|
|
writeInt(coord.y);
|
|
|
|
|
writeInt(coord.z);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public PacketCustom writeItemStack(ItemStack stack) {
|
2013-07-16 17:58:02 +10:00
|
|
|
writeItemStack(stack, false);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public PacketCustom writeItemStack(ItemStack stack, boolean large) {
|
|
|
|
|
if (stack == null) {
|
2013-07-16 17:58:02 +10:00
|
|
|
writeShort(-1);
|
2014-04-07 14:02:47 +10:00
|
|
|
} else {
|
|
|
|
|
writeShort(Item.getIdFromItem(stack.getItem()));
|
|
|
|
|
if (large)
|
2013-07-16 17:58:02 +10:00
|
|
|
writeInt(stack.stackSize);
|
|
|
|
|
else
|
|
|
|
|
writeByte(stack.stackSize);
|
|
|
|
|
writeShort(stack.getItemDamage());
|
|
|
|
|
writeNBTTagCompound(stack.stackTagCompound);
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public PacketCustom writeNBTTagCompound(NBTTagCompound compound) {
|
|
|
|
|
ByteBufUtils.writeTag(byteBuf, compound);
|
2013-07-16 17:58:02 +10:00
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public PacketCustom writeFluidStack(FluidStack fluid) {
|
|
|
|
|
if (fluid == null) {
|
2013-07-16 17:58:02 +10:00
|
|
|
writeShort(-1);
|
2014-04-07 14:02:47 +10:00
|
|
|
} else {
|
2013-07-16 17:58:02 +10:00
|
|
|
writeShort(fluid.fluidID);
|
2014-04-07 14:02:47 +10:00
|
|
|
writeVarInt(fluid.amount);
|
2013-07-16 17:58:02 +10:00
|
|
|
writeNBTTagCompound(fluid.tag);
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public boolean readBoolean() {
|
|
|
|
|
return byteBuf.readBoolean();
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public short readUByte() {
|
|
|
|
|
return byteBuf.readUnsignedByte();
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public int readUShort() {
|
|
|
|
|
return byteBuf.readUnsignedShort();
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public byte readByte() {
|
|
|
|
|
return byteBuf.readByte();
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public short readShort() {
|
|
|
|
|
return byteBuf.readShort();
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public int readInt() {
|
|
|
|
|
return byteBuf.readInt();
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public float readFloat() {
|
|
|
|
|
return byteBuf.readFloat();
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public double readDouble() {
|
|
|
|
|
return byteBuf.readDouble();
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public long readLong() {
|
|
|
|
|
return byteBuf.readLong();
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public char readChar() {
|
|
|
|
|
return byteBuf.readChar();
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int readVarShort() {
|
|
|
|
|
return ByteBufUtils.readVarShort(byteBuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int readVarInt() {
|
|
|
|
|
return ByteBufUtils.readVarInt(byteBuf, 5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BlockCoord readCoord() {
|
2013-07-16 17:58:02 +10:00
|
|
|
return new BlockCoord(readInt(), readInt(), readInt());
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public byte[] readByteArray(int length) {
|
|
|
|
|
byte[] barray = new byte[length];
|
|
|
|
|
byteBuf.readBytes(barray, 0, length);
|
|
|
|
|
return barray;
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public String readString() {
|
|
|
|
|
return ByteBufUtils.readUTF8String(byteBuf);
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public ItemStack readItemStack() {
|
2013-07-16 17:58:02 +10:00
|
|
|
return readItemStack(false);
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public ItemStack readItemStack(boolean large) {
|
2013-07-16 17:58:02 +10:00
|
|
|
ItemStack item = null;
|
|
|
|
|
short itemID = readShort();
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
if (itemID >= 0) {
|
2013-07-16 17:58:02 +10:00
|
|
|
int stackSize = large ? readInt() : readByte();
|
|
|
|
|
short damage = readShort();
|
2014-04-07 14:02:47 +10:00
|
|
|
item = new ItemStack(Item.getItemById(itemID), stackSize, damage);
|
2013-07-16 17:58:02 +10:00
|
|
|
item.stackTagCompound = readNBTTagCompound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public NBTTagCompound readNBTTagCompound() {
|
|
|
|
|
return ByteBufUtils.readTag(byteBuf);
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public FluidStack readFluidStack() {
|
2013-07-16 17:58:02 +10:00
|
|
|
FluidStack fluid = null;
|
|
|
|
|
short fluidID = readShort();
|
|
|
|
|
|
|
|
|
|
if (fluidID >= 0)
|
|
|
|
|
fluid = new FluidStack(fluidID, readInt(), readNBTTagCompound());
|
|
|
|
|
|
|
|
|
|
return fluid;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public FMLProxyPacket toPacket() {
|
|
|
|
|
if (incoming())
|
|
|
|
|
throw new IllegalStateException("Tried to write an incoming packet");
|
|
|
|
|
|
|
|
|
|
if (byteBuf.readableBytes() > 32000 || (type & 0x80) != 0)
|
|
|
|
|
do_compress();
|
|
|
|
|
|
|
|
|
|
//FML packet impl returns the whole of the backing array, copy used portion of array to another ByteBuf
|
|
|
|
|
return new FMLProxyPacket(byteBuf.copy(), channel);
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public void sendToPlayer(EntityPlayer player) {
|
2013-07-16 17:58:02 +10:00
|
|
|
sendToPlayer(toPacket(), player);
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public static void sendToPlayer(Packet packet, EntityPlayer player) {
|
|
|
|
|
if (player == null)
|
2013-07-16 17:58:02 +10:00
|
|
|
sendToClients(packet);
|
|
|
|
|
else
|
2014-04-07 14:02:47 +10:00
|
|
|
((EntityPlayerMP) player).playerNetServerHandler.sendPacket(packet);
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public void sendToClients() {
|
2013-07-16 17:58:02 +10:00
|
|
|
sendToClients(toPacket());
|
2014-04-07 14:02:47 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void sendToClients(Packet packet) {
|
2013-07-16 17:58:02 +10:00
|
|
|
MinecraftServer.getServer().getConfigurationManager().sendPacketToAllPlayers(packet);
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public void sendPacketToAllAround(double x, double y, double z, double range, int dim) {
|
2013-07-16 17:58:02 +10:00
|
|
|
sendToAllAround(toPacket(), x, y, z, range, dim);
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public static void sendToAllAround(Packet packet, double x, double y, double z, double range, int dim) {
|
2013-07-16 17:58:02 +10:00
|
|
|
MinecraftServer.getServer().getConfigurationManager().sendToAllNear(x, y, z, range, dim, packet);
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public void sendToDimension(int dim) {
|
2013-07-16 17:58:02 +10:00
|
|
|
sendToDimension(toPacket(), dim);
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public static void sendToDimension(Packet packet, int dim) {
|
2013-07-16 17:58:02 +10:00
|
|
|
MinecraftServer.getServer().getConfigurationManager().sendPacketToAllPlayersInDimension(packet, dim);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public void sendToChunk(World world, int chunkX, int chunkZ) {
|
2013-07-16 17:58:02 +10:00
|
|
|
sendToChunk(toPacket(), world, chunkX, chunkZ);
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public static void sendToChunk(Packet packet, World world, int chunkX, int chunkZ) {
|
2014-04-09 13:20:40 +10:00
|
|
|
PlayerManager playerManager = ((WorldServer)world).getPlayerManager();
|
|
|
|
|
for (EntityPlayerMP player : (List<EntityPlayerMP>) MinecraftServer.getServer().getConfigurationManager().playerEntityList)
|
|
|
|
|
if(playerManager.isPlayerWatchingChunk(player, chunkX, chunkZ))
|
|
|
|
|
sendToPlayer(packet, player);
|
|
|
|
|
|
|
|
|
|
/* Commented until forge accepts access tranformer request
|
2014-04-07 14:02:47 +10:00
|
|
|
PlayerInstance p = ((WorldServer) world).getPlayerManager().getOrCreateChunkWatcher(chunkX, chunkZ, false);
|
|
|
|
|
if (p != null)
|
2014-04-09 13:20:40 +10:00
|
|
|
p.sendToAllPlayersWatchingChunk(packet);*/
|
2013-07-16 17:58:02 +10:00
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
|
|
|
|
public void sendToOps() {
|
2013-07-16 17:58:02 +10:00
|
|
|
sendToOps(toPacket());
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-07 14:02:47 +10:00
|
|
|
public static void sendToOps(Packet packet) {
|
|
|
|
|
for (EntityPlayerMP player : (List<EntityPlayerMP>) MinecraftServer.getServer().getConfigurationManager().playerEntityList)
|
|
|
|
|
if (MinecraftServer.getServer().getConfigurationManager().isPlayerOpped(player.getCommandSenderName()))
|
2013-07-16 17:58:02 +10:00
|
|
|
sendToPlayer(packet, player);
|
|
|
|
|
}
|
2014-04-07 14:02:47 +10:00
|
|
|
|
2013-07-16 17:58:02 +10:00
|
|
|
@SideOnly(Side.CLIENT)
|
2014-04-07 14:02:47 +10:00
|
|
|
public void sendToServer() {
|
2013-07-16 17:58:02 +10:00
|
|
|
sendToServer(toPacket());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
2014-04-07 14:02:47 +10:00
|
|
|
public static void sendToServer(Packet packet) {
|
2013-07-16 17:58:02 +10:00
|
|
|
Minecraft.getMinecraft().getNetHandler().addToSendQueue(packet);
|
|
|
|
|
}
|
|
|
|
|
}
|