Java Code Examples for net.minecraft.network.PacketBuffer#readableBytes()

The following examples show how to use net.minecraft.network.PacketBuffer#readableBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RpcCallCodec.java    From OpenModsLib with MIT License 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception {
	final PacketBuffer input = new PacketBuffer(msg.payload());

	final Side side = ctx.channel().attr(NetworkRegistry.CHANNEL_SOURCE).get();

	final IRpcTarget target;
	final MethodEntry method;
	final Object[] args;

	{
		final int targetId = input.readVarInt();
		final BiMap<Integer, TargetTypeProvider> idToEntryMap = CommonRegistryCallbacks.getEntryIdMap(targetRegistry).inverse();
		final TargetTypeProvider entry = idToEntryMap.get(targetId);
		target = entry.createRpcTarget();
		EntityPlayer player = getPlayer(msg);
		target.readFromStreamStream(side, player, input);
	}

	{
		final BiMap<MethodEntry, Integer> eventIdMap = CommonRegistryCallbacks.getEntryIdMap(methodRegistry);
		final int methodId = input.readVarInt();
		method = eventIdMap.inverse().get(methodId);
		args = method.paramsCodec.readArgs(input);
	}

	int bufferJunkSize = input.readableBytes();
	Preconditions.checkState(bufferJunkSize == 0, "%s junk bytes left in buffer, method = %s", bufferJunkSize, method);

	out.add(new RpcCall(target, method, args));
	input.release();
}
 
Example 2
Source File: NetworkEventCodec.java    From OpenModsLib with MIT License 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception {
	final Channel channel = ctx.channel();
	final Side side = channel.attr(NetworkRegistry.CHANNEL_SOURCE).get();

	final PacketBuffer payload = new PacketBuffer(msg.payload());
	final int typeId = payload.readVarInt();
	final NetworkEventEntry type = CommonRegistryCallbacks.getEntryIdMap(registry).inverse().get(typeId);

	final EventDirection validator = type.getDirection();
	Preconditions.checkState(validator != null && validator.validateReceive(side),
			"Invalid direction: receiving packet %s on side %s", msg.getClass(), side);

	final NetworkEvent event = type.createPacket();
	event.readFromStream(payload);
	event.dispatcher = msg.getDispatcher();

	event.side = side;

	final INetHandler handler = msg.handler();
	if (handler != null) event.sender = OpenMods.proxy.getPlayerFromHandler(handler);

	final int bufferJunkSize = payload.readableBytes();
	if (bufferJunkSize > 0) Log.warn("%s junk bytes left in buffer, event %s", bufferJunkSize, event);

	out.add(event);
	payload.release();
}
 
Example 3
Source File: StructuredDataSlave.java    From OpenModsLib with MIT License 5 votes vote down vote up
private void readElementPayload(SortedSet<Integer> ids, PacketBuffer input) {
	try {
		for (Integer id : ids) {
			final E element = elements.get(id);
			if (element == null) throw new ConsistencyCheckFailed("Element %d not found", id);
			element.readFromStream(input);
		}

		if (input.readableBytes() != 0) throw new ConsistencyCheckFailed("Element payload not fully consumed");
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example 4
Source File: SyncedTileEntity.java    From OpenModsLib with MIT License 5 votes vote down vote up
private NBTTagCompound serializeInitializationData(NBTTagCompound tag) {
	final PacketBuffer tmp = new PacketBuffer(Unpooled.buffer());
	try {
		getSyncMap().writeInitializationData(tmp);
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
	byte[] data = new byte[tmp.readableBytes()];
	tmp.readBytes(data);
	tag.setByteArray(TAG_SYNC_INIT, data);

	return tag;
}
 
Example 5
Source File: MixinNetHandlerPlayClient.java    From Hyperium with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Inject(method = "handleCustomPayload", at = @At("RETURN"))
private void handleCustomPayload(S3FPacketCustomPayload packetIn, CallbackInfo ci) {
    PacketBuffer packetBuffer = packetIn.getBufferData();
    try {
        int readableBytes = packetBuffer.readableBytes();

        if (readableBytes > 0) {
            byte[] payload = new byte[readableBytes - 1];
            packetBuffer.readBytes(payload);
            String message = new String(payload, Charsets.UTF_8);

            if (LoginReplyHandler.SHOW_MESSAGES) {
                GeneralChatHandler.instance().sendMessage("Packet message on channel " + packetIn.getChannelName() + " -> " + message);
            }

            if ("REGISTER".equalsIgnoreCase(packetIn.getChannelName())) {
                if (message.contains("Hyperium")) {
                    PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
                    buffer.writeString("Hyperium;" + Metadata.getVersion() + ";" + Metadata.getVersionID());
                    addToSendQueue(new C17PacketCustomPayload("REGISTER", buffer));
                    PacketBuffer addonbuffer = new PacketBuffer(Unpooled.buffer());
                    List<AddonManifest> addons = AddonBootstrap.INSTANCE.getAddonManifests();
                    addonbuffer.writeInt(addons.size());

                    for (AddonManifest addonmanifest : addons) {
                        String addonName = addonmanifest.getName();
                        String version = addonmanifest.getVersion();

                        if (addonName == null) addonName = addonmanifest.getMainClass();
                        if (version == null) version = "unknown";

                        addonbuffer.writeString(addonName);
                        addonbuffer.writeString(version);
                    }

                    addToSendQueue(new C17PacketCustomPayload("hyperium|Addons", addonbuffer));
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}