Jump to content

[SOLVED] Multiblock structure and neighborChanged


Jay Avery

Recommended Posts

I'm making a multiblock structure - an item which, when placed, places a set of three blocks in a particular layout. When any one of those blocks is broken, all of the others are removed too. I've made the neighborChanged method so that it checks the expected positions of the other blocks in the structure, and then removes the block if they aren't there. I've also made the onItemUse method to place all three blocks when the item is clicked.

 

But when I actually use the item in the game, the structure doesn't get placed. Because on each setBlockState call, the game checks for neighborChanged and finds the the other blocks aren't there yet. I've looked through the code for vanilla beds and I can't figure out how this problem is suppressed there. I could add a whole other block property to keep track of whether the structure is 'being built' or not, and only update it if it's false - but that seems excessive. Is there any other way to tell the game not to check neighborChanged until all the blocks of the structure have been placed via onItemUse?

 

Edit: I think I've solved it! Needed to add the third parameter to the setBlockState method, the flags which define whether the block updates the neighbours. I don't understand bitwise flags very well, but with some trial and error it seems that '2' is making it work as expected.

Solved with Choonster's advice in post #2.

Link to comment
Share on other sites

When a bed item is right clicked on a block:

  • The foot block is placed, calling
    Block#neighborChanged

    on the surrounding blocks (none of which are incomplete beds)

  • The head block is placed, calling
    Block#neighborChanged

    on the surrounding blocks (one of which is the foot of the now-complete bed)

 

Block#neighborChanged

is never called on an incomplete bed while the bed is being placed.

 

You should implement your

Block#neighborChanged

override such that it only checks the immediately adjacent blocks and not the whole structure.

 

Let's say your structure is composed of three blocks: left, right and centre. When your item is right clicked on a block:

  • Place the left block, calling
    Block#neighborChanged

    on the surrounding blocks. None of these are structure blocks.

  • Place the centre block, calling
    Block#neighborChanged

    on the surrounding blocks. One of these is the left block, which checks the immediately adjacent blocks, finds the centre block and does nothing because it's valid.

  • Place the right block, calling
    Block#neighborChanged

    on the surrounding blocks. One of these is the centre blocks, which checks the immediately adjacent blocks, finds both the left and right blocks and does nothing because they're valid.

 

Whenever a structure block is broken,

Block#neighborChanged

will be called on the surrounding blocks. The adjacent structure blocks will see that the structure is now incomplete and remove themselves, propagating the change along the structure.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Ahh, thank you! I was thinking that I needed every block in the structure to check every other block, but I see now that I can just have them trigger each others' checks in a chain.

 

This means that I actually don't need to set the third parameter in the

setBlockState

method, right?

Link to comment
Share on other sites

This means that I actually don't need to set the third parameter in the

setBlockState

method, right?

 

Correct, the two-argument overload of

World#setBlockState

(which uses 3 [1 + 2] as the flags) should work.

 

Oddly enough,

ItemBed

uses 11 (1 + 2 + 8) as the flags even though it only sets the block on the server and only the client-side implementation of

IWorldEventListener#notifyBlockUpdate

(indirectly called by

World#setBlockState

) actually checks for flag 8.

 

Edit: I always forget to disable smileys when posting something with "8)" in it.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.