Hi, I've been struggling with this issue for a few days and haven't found a viable solution (all methods result in the same error).
I've got a GUI that doesn't use container, so I need to send a net message across with the data for the GUI so the player can edit the data.
I know the code at the moment has a few issues with its layout, I've been mucking around with it trying to isolate the issue for a while, and it needs a good rewrite and reorganization ;-;
Currenty this code runs into the error:
[14:14:03] [Server thread/ERROR] [FML]: FMLIndexedMessageCodec exception caught
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.sendTo(SimpleNetworkWrapper.java:250) [SimpleNetworkWrapper.class:?]
at net.geoff.forcefield.blocks.forcegenerator.BlockFFGen.onBlockActivated(BlockFFGen.java:31) [BlockFFGen.class:?]
at net.minecraft.server.management.PlayerInteractionManager.processRightClickBlock(PlayerInteractionManager.java:472) [PlayerInteractionManager.class:?]
at net.minecraft.network.NetHandlerPlayServer.processTryUseItemOnBlock(NetHandlerPlayServer.java:767) [NetHandlerPlayServer.class:?]
at net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock.processPacket(CPacketPlayerTryUseItemOnBlock.java:68) [CPacketPlayerTryUseItemOnBlock.class:?]
at net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock.processPacket(CPacketPlayerTryUseItemOnBlock.java:13) [CPacketPlayerTryUseItemOnBlock.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_151]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_151]
at net.minecraft.util.Util.runTask(Util.java:53) [Util.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:798) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:743) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192) [IntegratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:592) [MinecraftServer.class:?]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_151]
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.geoff.forcefield.network.GuiFFGenOpenMessage.toBytes(GuiFFGenOpenMessage.java:39) ~[GuiFFGenOpenMessage.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:77) ~[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]
... 23 more
Relevant Code:
ForcefieldMod.java preinit event (and creation of packetHandle var)
public static final SimpleNetworkWrapper packetHandle = NetworkRegistry.INSTANCE.newSimpleChannel(ForcefieldMod.modId);
public static Logger logger;
@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event) {
logger = event.getModLog();
int netId = 0;
packetHandle.registerMessage(GuiFFGenOpenMessage.Handler.class, GuiFFGenOpenMessage.class, netId++, Side.CLIENT);
}
BlockFFGen.java
package net.geoff.forcefield.blocks.forcegenerator;
import javax.annotation.Nullable;
import net.geoff.forcefield.ForcefieldMod;
import net.geoff.forcefield.blocks.BlockTileEntity;
import net.geoff.forcefield.network.GuiFFGenOpenMessage;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class BlockFFGen extends BlockTileEntity<TileEntityFFGen> {
public BlockFFGen() {
super(Material.ROCK, "forcefield_generator");
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand,
EnumFacing side, float hitX, float hitY, float hitZ) {
if (!world.isRemote) {
TileEntityFFGen tile = getTileEntity(world, pos);
if(player.isSneaking()) {
tile.setForceField(!tile.isGenerated);
} else {
ForcefieldMod.packetHandle.sendTo(new GuiFFGenOpenMessage(tile.xSize,tile.ySize,tile.zSize), (EntityPlayerMP) player);
}
}
return true;
}
@Override
public Class<TileEntityFFGen> getTileEntityClass() {
return TileEntityFFGen.class;
}
@Nullable
@Override
public TileEntityFFGen createTileEntity(World world, IBlockState state) {
return new TileEntityFFGen();
}
}
GuiFFGenOpenMessage.java
package net.geoff.forcefield.network;
import io.netty.buffer.ByteBuf;
import net.geoff.forcefield.ForcefieldMod;
import net.geoff.forcefield.blocks.forcegenerator.GuiFFGen;
import net.minecraft.client.Minecraft;
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 GuiFFGenOpenMessage implements IMessage {
public GuiFFGenOpenMessage() {}
private int xSize;
private int ySize;
private int zSize;
public GuiFFGenOpenMessage(int xSize, int ySize, int zSize) {
// TODO Auto-generated constructor stub
this.xSize = xSize;
this.ySize = ySize;
this.zSize = zSize;
}
@Override
public void fromBytes(ByteBuf buf) {
// TODO Auto-generated method stub
ForcefieldMod.logger.info(xSize + "" + ySize + "" + zSize);
buf.writeInt(xSize);
buf.writeInt(ySize);
buf.writeInt(zSize);
}
@Override
public void toBytes(ByteBuf buf) {
// TODO Auto-generated method stub
ForcefieldMod.logger.info(buf);
xSize = buf.readInt();
ySize = buf.readInt();
zSize = buf.readInt();
}
public static class Handler implements IMessageHandler<GuiFFGenOpenMessage, IMessage> {
@Override
public IMessage onMessage(GuiFFGenOpenMessage message, MessageContext ctx) {
int x = message.xSize;
int y = message.ySize;
int z = message.zSize;
Minecraft.getMinecraft().addScheduledTask(() -> {
Minecraft.getMinecraft().displayGuiScreen(new GuiFFGen(x,y,z));
});
return null;
}
}
}
The main reason I'm here is I can't find any posts online that reflect the failure to read the information back from the buffer (I have verified with logger outputs that it is writing the data, but is unable to read the data)
Thanks all.