Jump to content

IndexArrayOutOfBoundsException on packet send


Insane96MCP

Recommended Posts

As I try to send a test packet (PacketHandler.SendToServer(new TestMessage(10))) to server I get this huge exception:

Spoiler

io.netty.handler.codec.EncoderException: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 1, cap: 256)
	at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:106) ~[MessageToMessageEncoder.class:4.1.9.Final]
	at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116) ~[MessageToMessageCodec.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:738) ~[AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeWriteAndFlush(AbstractChannelHandlerContext.java:801) ~[AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:814) ~[AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:794) ~[AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:831) ~[AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:1032) ~[DefaultChannelPipeline.class:4.1.9.Final]
	at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:296) ~[AbstractChannel.class:4.1.9.Final]
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper.sendToServer(SimpleNetworkWrapper.java:289) [SimpleNetworkWrapper.class:?]
	at net.insane96mcp.iguanatweaks.network.PacketHandler.SendToServer(PacketHandler.java:19) [PacketHandler.class:?]
	at net.insane96mcp.iguanatweaks.modules.ModuleDrops.PlayerToss(ModuleDrops.java:76) [ModuleDrops.class:?]
	at net.insane96mcp.iguanatweaks.events.ItemToss.EventItemToss(ItemToss.java:11) [ItemToss.class:?]
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_33_ItemToss_EventItemToss_ItemTossEvent.invoke(.dynamic) [?:?]
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) [ASMEventHandler.class:?]
	at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:179) [EventBus.class:?]
	at net.minecraftforge.common.ForgeHooks.onPlayerTossEvent(ForgeHooks.java:681) [ForgeHooks.class:?]
	at net.minecraft.entity.player.EntityPlayer.dropItem(EntityPlayer.java:780) [EntityPlayer.class:?]
	at net.minecraft.network.NetHandlerPlayServer.processPlayerDigging(NetHandlerPlayServer.java:679) [NetHandlerPlayServer.class:?]
	at net.minecraft.network.play.client.CPacketPlayerDigging.processPacket(CPacketPlayerDigging.java:56) [CPacketPlayerDigging.class:?]
	at net.minecraft.network.play.client.CPacketPlayerDigging.processPacket(CPacketPlayerDigging.java:12) [CPacketPlayerDigging.class:?]
	at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:21) [PacketThreadUtil$1.class:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_161]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_161]
	at net.minecraft.util.Util.runTask(Util.java:53) [Util.class:?]
	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:796) [MinecraftServer.class:?]
	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:741) [MinecraftServer.class:?]
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192) [IntegratedServer.class:?]
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:590) [MinecraftServer.class:?]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_161]
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 1, cap: 256)
	at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1396) ~[AbstractByteBuf.class:4.1.9.Final]
	at io.netty.buffer.AbstractByteBuf.readInt(AbstractByteBuf.java:766) ~[AbstractByteBuf.class:4.1.9.Final]
	at net.minecraft.network.PacketBuffer.readInt(PacketBuffer.java:953) ~[PacketBuffer.class:?]
	at net.insane96mcp.iguanatweaks.network.TestMessage.toBytes(TestMessage.java:20) ~[TestMessage.class:?]
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:30) ~[SimpleIndexedCodec.class:?]
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:26) ~[SimpleIndexedCodec.class:?]
	at net.minecraftforge.fml.common.network.FMLIndexedMessageToMessageCodec.encode(FMLIndexedMessageToMessageCodec.java:71) ~[FMLIndexedMessageToMessageCodec.class:?]
	at io.netty.handler.codec.MessageToMessageCodec$1.encode(MessageToMessageCodec.java:67) ~[MessageToMessageCodec$1.class:4.1.9.Final]
	at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:88) ~[MessageToMessageEncoder.class:4.1.9.Final]
	... 29 more
Mar 26, 2018 12:03:38 PM io.netty.channel.embedded.EmbeddedChannel recordException
WARNING: More than one exception was raised. Will report only the first one and log others.
io.netty.handler.codec.EncoderException: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 1, cap: 256)
	at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:106)
	at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116)
	at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:738)
	at io.netty.channel.AbstractChannelHandlerContext.invokeWriteAndFlush(AbstractChannelHandlerContext.java:801)
	at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:814)
	at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:794)
	at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:831)
	at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:1032)
	at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:296)
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper.sendToServer(SimpleNetworkWrapper.java:289)
	at net.insane96mcp.iguanatweaks.network.PacketHandler.SendToServer(PacketHandler.java:19)
	at net.insane96mcp.iguanatweaks.modules.ModuleDrops.PlayerToss(ModuleDrops.java:76)
	at net.insane96mcp.iguanatweaks.events.ItemToss.EventItemToss(ItemToss.java:11)
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_33_ItemToss_EventItemToss_ItemTossEvent.invoke(.dynamic)
	at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)
	at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:179)
	at net.minecraftforge.common.ForgeHooks.onPlayerTossEvent(ForgeHooks.java:681)
	at net.minecraft.entity.player.EntityPlayer.dropItem(EntityPlayer.java:780)
	at net.minecraft.network.NetHandlerPlayServer.processPlayerDigging(NetHandlerPlayServer.java:679)
	at net.minecraft.network.play.client.CPacketPlayerDigging.processPacket(CPacketPlayerDigging.java:56)
	at net.minecraft.network.play.client.CPacketPlayerDigging.processPacket(CPacketPlayerDigging.java:12)
	at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:21)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at net.minecraft.util.Util.runTask(Util.java:53)
	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:796)
	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:741)
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192)
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:590)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(4) exceeds writerIndex(1): UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 1, cap: 256)
	at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1396)
	at io.netty.buffer.AbstractByteBuf.readInt(AbstractByteBuf.java:766)
	at net.minecraft.network.PacketBuffer.readInt(PacketBuffer.java:953)
	at net.insane96mcp.iguanatweaks.network.TestMessage.toBytes(TestMessage.java:20)
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:30)
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleIndexedCodec.encodeInto(SimpleIndexedCodec.java:26)
	at net.minecraftforge.fml.common.network.FMLIndexedMessageToMessageCodec.encode(FMLIndexedMessageToMessageCodec.java:71)
	at io.netty.handler.codec.MessageToMessageCodec$1.encode(MessageToMessageCodec.java:67)
	at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:88)
	... 29 more

 

 

PacketHandler

Spoiler

package net.insane96mcp.iguanatweaks.network;

import net.insane96mcp.iguanatweaks.IguanaTweaks;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.relauncher.Side;

public class PacketHandler {
	public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(IguanaTweaks.MOD_ID);
	private static int discriminator = 0;
	
	public static void Init() {
		INSTANCE.registerMessage(TestMessageHandler.class, TestMessage.class, discriminator++, Side.SERVER);
	}
	
	public static void SendToServer(TestMessage message) {
		INSTANCE.sendToServer(message);
	}
}

 

 

IMessage implementation

Spoiler

package net.insane96mcp.iguanatweaks.network;

import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;

public class TestMessage implements IMessage {

	public int toSend;
	public TestMessage(int toSend) {
		this.toSend = toSend;
	}
	
	@Override
	public void fromBytes(ByteBuf buf) {
		buf.writeInt(toSend);
	}

	@Override
	public void toBytes(ByteBuf buf) {
		this.toSend = buf.readInt();
	}

}

 

 

IMessageHandler implementation

Spoiler

package net.insane96mcp.iguanatweaks.network;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class TestMessageHandler implements IMessageHandler<TestMessage, IMessage> {

	@Override
	public IMessage onMessage(TestMessage message, MessageContext ctx) {
		EntityPlayerMP serverPlayer = ctx.getServerHandler().player;
		int amount = message.toSend;
		serverPlayer.getServerWorld().addScheduledTask(() -> {
			serverPlayer.inventory.addItemStackToInventory(new ItemStack(Items.DIAMOND, amount));
	    });
		return null;
	}

}

 

 

The Packet handler has been registered in the PreInit of the CommonProxy with PacketHandler.Init()
I really don't know what I'm doing wrong since I've followed the docs of SimpleImpl

Edited by Insane96MCP
Link to comment
Share on other sites

You've got your fromBytes and toBytes implementations around the wrong way. fromBytes needs to read the data from the byte buffer into the fields of the packet; toBytes needs to write the data from the fields of the packet into the byte buffer.

  • Thanks 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.