Jump to content

[SOLVED] [1.7.2] Simple Block with Inventory


mercrutio

Recommended Posts

Hey, I'm having a heck of a time setting up a block that can hold items between sessions.  It's obviously a problem with Server/Client syncing, but I don't know how to approach it.  I feel like there should be a simple solution by extending and overriding the right classes/files, but I can't figure it out.

 

Assume, for simplicity, that I just want to make a 'custom' Furnace.  I have 1.) a Block implementing ITileEntityProvider, 2.) a TileEntity implementing IInventory, 3.) a Container, and 4.) a GuiContainer.  I also have a GuiHandler class that points to the container/guiContainer as appropriate.  I have registered the block and the tile entity with GameRegistry.

 

I can get the gui to render just fine, and I can even interact with it as a simple storage.  I haven't tried anything relating to burning.  The problem is encountered when logging out and back in:  any items placed in the tile entity go back to the player inventory.  I'll post some code, but is there anything I'm obviously not doing?

 

It seems that there aren't, in fact, any methods in place to handle the disparity between the Server and Client.  I have written a custom packet to inform the server every time an item is placed into the inventory clientside, but now the client doesn't seem to get the data from the server.  And it's a horribly messy way to be doing it.

 

Solution: There was a bug in Forge 10.12.0.998 (and probably below). It was fixed in build 999, and syncing the TileEntity is just as you would think.  If you're dealing with obfuscation, func_145844_m() is getDescriptionPacket(), and onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) is legible.  And yes, you do need to use S35PacketUpdateTileEntity.  Thanks to the guys here and around who told me about the bug, and thanks x100 to the powers that be (cpw, I'm pretty sure) for fixing this promptly.

Link to comment
Share on other sites

Synching tile entities is super easy.

 

        public Packet getDescriptionPacket() {
                NBTTagCompound nbtTag = new NBTTagCompound();
                this.writeToNBT(nbtTag);
                return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag);
        }

        public void onDataPacket(INetworkManager net, Packet132TileEntityData packet) {
                readFromNBT(packet.data);
        }

 

As long as your TE saves its inventory correctly in writeToNBT and reads it back in readFromNBT, that's all you need.

 

https://github.com/Draco18s/Artifacts/blob/master/draco18s/artifacts/entity/TileEntityDisplayPedestal.java

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

Draco18s, I appreciate the input.  The idea is sound, but it doesn't work correctly under 10.12.0.998, which is Forge-latest as of this writing.

 

After obfuscating and updating to deal with Netty, I got this:

 

public Packet func_145844_m() {
        NBTTagCompound nbtTag = new NBTTagCompound();
        this.func_145841_b(nbtTag);
        return new S35PacketUpdateTileEntity(this.field_145851_c, this.field_145848_d, this.field_145849_e, 1, nbtTag);
}

public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) {
func_145839_a(packet.func_148857_g());
}

 

which works as described in the OP.

 

I can't be certain until I can test it, but I'm pretty sure it's exactly what EdgarAllen says.  Anyway, thank you, I was really excited for a second there.

Link to comment
Share on other sites

Personally, I'm just waiting till forge pulls in the commit. I don't expect it to be more than a couple days. I know my gui code is correct. In the mean time I just did a mock that has it's inventory filled with random items so I can test things without having to open the gui.

 

Link to comment
Share on other sites

Personally, I'm just waiting till forge pulls in the commit.

 

Personally I'm waiting for more deobfuscation.

 

I can handle the occasional func_* and field_* but when 95% of what I'm using looks like that, the code is unreadable and very hard to work with.

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

Man, tell me about it.  I haven't been programming for fun in a while, and when I saw Forge had just come out for 1.7.2 I was all, "Sweet!"  Now the names have got my head spinning.  I'm learning a lot about Minecraft and Forge though, and it's satisfying as heck when things work.

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.