Jump to content

Held item dependent rendering


Recommended Posts

Hello everyone.  This is my first post here.  I'm writing my first mod and seem to have hit a roadblock.


I'm attempting to make a utility block only visible in-world when a specific item is held by the player.  I verify the held item state with this:


if(Minecraft.getMinecraft().thePlayer.getHeldItem().getItem().itemID == Ids.scController)


This seems to work.  What would be the best way to make the block visible/invisible based on this boolean value, but still raise an event when the player walks through it?


Feel free to simply point me in the direction of the proper events/methods.  I'm a long time programmer, first time MC modder, but Java doesn't seem too complicated to pick up.


Thanks in advance for any help.

Link to comment
Share on other sites



I'd suggest to make your utility block a TileEntity, and render it in TileEntitySpecialRenderer (invisible if the player is not holding the item)


Some background info here might be useful - see the "The most important minecraft classes" topics and the Block Rendering topics.



If you want to remove the visible "hitbox" around the block when it's invisible, you could try overriding  collisionRayTrace() setBlockBoundsBasedOnState() in the block to change the block's bounds; see BlockDoor for example.


BTW your code line will crash if the player isn't holding an item, you need to check getHeldItem() == null.  And you should compare the item, not the itemID.


If you're new to Java but experienced in (say) C++ it shouldn't take you long to get up to speed.  Some of the things in this link might be helpful background info.






Link to comment
Share on other sites

Thank you, Grey.  In fact, the tutorials you linked me to were probably the most understandable high-level description of what is going on under the hood that I've seen yet.  Thanks!


The vanilla code I've looked in to has been fairly easy to interpret so far.  I've done a good bit of c++, but most projects I need code for at work now I do in c#.  The whole inheritance thing is old hat by now, and java's implementation seems pretty straight forward.

Link to comment
Share on other sites

The more efficient way would probably be to make a block renderer without using a TileEntity (What was that called again?) and a TickHandler that only checks the value once every tick and not once every frame. You could either store the value in the TickHandler or make a static variable in the renderer.

PM's regarding modding questions should belong in the Modder Support sub-forum and won't be answered.

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.

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.