Jump to content

[1.16.4] Getting a better understanding of write/read methods with TileEntity, and storing Items in modded blocks


TheOGSeabass

Recommended Posts

I've managed to create my TileEntity class for a block I'm working with and I sort of understand the read and write methods I've overridden, but I'd also like to know if there is any way to get information back from the TileEntity on the client side. I want to be able to do something like this:

 

private final ItemStackHandler itemHandler = createHandler();

private final LazyOptional<IItemHandler> handler = LazyOptional.of(
        () -> itemHandler);

private Item contained;

private Item toDrop;

private ItemStackHandler createHandler(){
    return new ItemStackHandler(1){
        @Override
        protected void onContentsChanged(int slot){
            markDirty();
        }

        @Nonnull
        @Override
        public boolean isItemValid(int slot, @Nonnull ItemStack item){
            if(item.getItem() instanceof SwordItem){
                return true;
            }
            return false;
        }

        @Nonnull
        @Override
        public ItemStack insertItem(int slot, @Nonnull ItemStack item, boolean simulate){
            return super.insertItem(slot,item,simulate);
        }
    };
}

@Override
public void read(BlockState blockState, CompoundNBT nbt){
    super.read(blockState,nbt);
    itemHandler.deserializeNBT(nbt.getCompound("contained"));
}

@Override
public CompoundNBT write(CompoundNBT nbt){
    CompoundNBT ourNBT = super.write(nbt);
    this.toDrop = (Item)ourNBT.get("contained");
    ourNBT.put("contained",itemHandler.serializeNBT());
    this.contained = ((Item)ourNBT.get("contained"));
    return ourNBT;
}

 

The modded block should basically drop the item it is containing when the player right clicks the block with a new item, then store the new Item. I use an event for this, and my only problem is figuring out how I can call back something stored in the TileEntity.

I know in my code casting the

(Item)ourNBT.get("contained")

CompoundNBT to an Item doesn't work, but this is kind of just laying out my thought process. If anyone could give me some insight into what I'm missing and what I'm doing wrong I'd be super thankful! Also if my entire idea with using events to do this is flawed, then I'd be happy to hear that too haha.

Link to comment
Share on other sites

2 hours ago, TheOGSeabass said:

The modded block should basically drop the item it is containing when the player right clicks the block with a new item, then store the new Item. I use an event for this, and my only problem is figuring out how I can call back something stored in the TileEntity.

Events are for interfacing with pre-existing systems within Vanilla. As you are the creator, you can implement the required logic on the block itself.

 

Now let's discuss the logic.

3 hours ago, TheOGSeabass said:

(Item)ourNBT.get("contained")

This is irrelevant since the data is stored on the nbt instance of the tile entity and not the item itself. You should also probably read up on capabilities. I am attaching a community written version of the docs as the forge docs are still outdated on the manner. Finally, all you are trying to execute should be done on the logical server. None of this seems to require the logical client in the slightest. This can seemingly be done using Block#onBlockActivated and grabbing the associated tile entity to do the logic.

  • Thanks 1
Link to comment
Share on other sites

Ahhh thank you so much! That sounds about right too, I have trouble figuring out what class to implement stuff in sometimes so that's a huge help.
 

24 minutes ago, ChampionAsh5357 said:

This can seemingly be done using Block#onBlockActivated and grabbing the associated tile entity to do the logic.

And thank you so much for this and linking the doc as well, I'll definitely go that route!

Link to comment
Share on other sites

Just coming back to say thanks again, after reading through the doc and looking through the IItemHandler interface, how to use the TileEntity makes way more sense.

If anyone else is looking through this, you can see a great example of a TileEntity and Block being connected and having an "onBlockActivated" behaviour in the "JukeboxBlock" and "JukeboxTileEntity" minecraft classes. Looking through those is super useful for getting an idea of how a simple TileEntity can be used to interact with the game.

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.