Jump to content

[1.12] Cannot add ItemStack to Chest


Tut

Recommended Posts

With this item, I'm trying to combine two chest's inventories into one list, then put both lists into a chest. I know that this code is probably very, very wrong. I have a moderate understanding of Java, but at this point, I'm basically just praying that someone else has had the same issues as me, I barely passed my APCS exam last year. :/

 

I know this has a lot of bad coding practice, and I'm a bit embarrassed to show it, but here's a git repo with the working code that I have so far. (I know I set it up wrong too. I'm working on one thing at a time. :S )

 

Specifically, the issue is on line 104 of the HuskItem class.

tutmodPacketHandler.INSTANCE.sendToServer(new tutmodMessage(locatedChest, newlist, airList));

 

I've tried so many things to deposit the itemstacks in the newlist List into the chest. I've tried some different solutions, but nothing I've done will get the items to appear in the Chest. I'm not sure if it's just really bad logic on my part because my code is so bad, or if I'm using something wrong. I'm not even sure if I got the sending and receiving of packets right.

 

I'll work on the other issues after I get this one fixed, I'd just like to know how I can take the itemstacks from the newlist List and deposit them into locatedChest. I'd like as much help as one can give - I know I did a lot wrong. Just trying to learn, after all.

 

Thank you!

Link to comment
Share on other sites

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

1 hour ago, Draco18s said:

I'm not sure. Like I said, I'm not exactly sure how I would accomplish what I'm looking to do. I get now that it's where you would actually send the data, I'm just lost on how you would turn the three variables that I'm using to store information into a usable format for the buf.

Edited by Tut
Link to comment
Share on other sites

You need to encode the information you want to send.  ByteBufUtils will help.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

23 hours ago, Draco18s said:

You need to encode the information you want to send.  ByteBufUtils will help.

 

12 hours ago, diesieben07 said:

Also, those fields should not be static.

 

Alright. Seems to have fixed my issue. One more question: Since it seems that the code from the Message is actually performed on the server after the OnItemUseFirst method is completed, should I just move the ClearChestStorage functionality to the Message class?

 

The code was working before I made the Message class but it doesn't work afterward, and even so, I feel like it would be reaching across sides. Am I correct in assuming this or would I just be doing too much computation in the handle method?

 

Edit: Forgot to say thanks for the help!

Edited by Tut
Link to comment
Share on other sites

7 hours ago, diesieben07 said:

and (even worse) across client & server

(On the single player integrated server, anyway)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

15 hours ago, diesieben07 said:
  • At the place where you send the packet to the server you are already on the server. This will not work and makes no sense.
  • Why are these fields?
  • Things like this must not be stored as fields in the Item class. There is only one instance of that class, everything you store in there will be shared across all "instances" of your item and (even worse) across client & server. You must store this in the ItemStack.

Ok, I understand the last two points. However, I'm not sure how I would be on the server when I send the packet. I could be thinking about it all wrong, but from what I understand I am sending the packet on line 103 of the HuskItem class. How could I be on the server when I send the packet? Once again, I'm probably wrong here but FMLCommonHandler.getSide() returns that (when I'm sending the packet) I'm calling it from the client. Am I seeing something the wrong way?

Edited by Tut
Link to comment
Share on other sites

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Alright, rebuilt how the item works now. I think it's much better this time around, but I'm having a strange crash. Is there some sort of limit on what I can write / read from the buffer? Or am I still doing something wrong and dumb?

 

The code in the main post has been updated to the newest version.

 

Crash log:

Spoiler


[19:36:22] [Netty Server IO #1/ERROR] [FML]: FMLIndexedMessageCodec exception caught
io.netty.handler.codec.DecoderException: java.lang.IndexOutOfBoundsException: readerIndex(12) + length(4) exceeds writerIndex(12): UnpooledSlicedByteBuf(ridx: 12, widx: 12, cap: 12/12, unwrapped: UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 13, cap: 256))
	at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:98) ~[MessageToMessageDecoder.class:4.1.9.Final]
	at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) ~[MessageToMessageCodec.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334) [DefaultChannelPipeline$HeadContext.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926) [DefaultChannelPipeline.class:4.1.9.Final]
	at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:274) [EmbeddedChannel.class:4.1.9.Final]
	at net.minecraftforge.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:108) [FMLProxyPacket.class:?]
	at net.minecraft.network.NetworkManager.channelRead0(NetworkManager.java:155) [NetworkManager.class:?]
	at net.minecraft.network.NetworkManager.channelRead0(NetworkManager.java:49) [NetworkManager.class:?]
	at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) [SimpleChannelInboundHandler.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [AbstractChannelHandlerContext.class:4.1.9.Final]
	at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.handleServerSideCustomPacket(NetworkDispatcher.java:446) [NetworkDispatcher.class:?]
	at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.channelRead0(NetworkDispatcher.java:268) [NetworkDispatcher.class:?]
	at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.channelRead0(NetworkDispatcher.java:72) [NetworkDispatcher.class:?]
	at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) [SimpleChannelInboundHandler.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334) [DefaultChannelPipeline$HeadContext.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [AbstractChannelHandlerContext.class:4.1.9.Final]
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926) [DefaultChannelPipeline.class:4.1.9.Final]
	at io.netty.channel.local.LocalChannel.finishPeerRead0(LocalChannel.java:443) [LocalChannel.class:4.1.9.Final]
	at io.netty.channel.local.LocalChannel.access$500(LocalChannel.java:49) [LocalChannel.class:4.1.9.Final]
	at io.netty.channel.local.LocalChannel$5.run(LocalChannel.java:397) [LocalChannel$5.class:4.1.9.Final]
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) [AbstractEventExecutor.class:4.1.9.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403) [SingleThreadEventExecutor.class:4.1.9.Final]
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:442) [NioEventLoop.class:4.1.9.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) [SingleThreadEventExecutor$5.class:4.1.9.Final]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_144]
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(12) + length(4) exceeds writerIndex(12): UnpooledSlicedByteBuf(ridx: 12, widx: 12, cap: 12/12, unwrapped: UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 13, 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 network.tutmodHuskItemMessage.fromBytes(tutmodHuskItemMessage.java:48) ~[tutmodHuskItemMessage.class:?]
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleIndexedCodec.decodeInto(SimpleIndexedCodec.java:36) ~[SimpleIndexedCodec.class:?]
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleIndexedCodec.decodeInto(SimpleIndexedCodec.java:26) ~[SimpleIndexedCodec.class:?]
	at net.minecraftforge.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:103) ~[FMLIndexedMessageToMessageCodec.class:?]
	at net.minecraftforge.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:40) ~[FMLIndexedMessageToMessageCodec.class:?]
	at io.netty.handler.codec.MessageToMessageCodec$2.decode(MessageToMessageCodec.java:81) ~[MessageToMessageCodec$2.class:4.1.9.Final]
	at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:88) ~[MessageToMessageDecoder.class:4.1.9.Final]
	... 35 more

 

 

 

Link to comment
Share on other sites

51 minutes ago, diesieben07 said:
  • Please use a list for your chest positions! NBTTagList is what you want.
  • Only modify the NBT data on the server.
  • Why are you checking if you are on the client and sending a packet? Just do it right there and check for being on the server... You do not need a packet here at all!
  • Look very closely at these lines (not that you need this code, but this is why you are getting the exception).

Holy shit I'm dumb. This is what I get for copy/pasting.

 

Edit: Will do what you said as well. I can't believe that I haven't seen that though.

Edited by Tut
Link to comment
Share on other sites

1 hour ago, diesieben07 said:
  • Please use a list for your chest positions! NBTTagList is what you want.
  • Only modify the NBT data on the server.
  • Why are you checking if you are on the client and sending a packet? Just do it right there and check for being on the server... You do not need a packet here at all!
  • Look very closely at these lines (not that you need this code, but this is why you are getting the exception).

Alright, one more question now that I've realized my issues. If I want to set the inventory of any TileEntity, I can do it straight from HuskItem's method? Are there any examples I can look at where syncing the inventory between client and server is necessary then? I think I'm just confused about when and where to use packets, I guess. 

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.

Announcements



×
×
  • Create New...

Important Information

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