Change block data on BlockPlaceEvent (1.3.1)

Discussion in 'Bukkit Discussion' started by orgin_org, Aug 7, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    orgin_org

    Hello.

    I'm trying to write a plugin that changes the behaviour of the way logs are placed. Basically I'm trying to change the rotation of log blocks when they are placed via a BlockPlacedEvent handler.

    Simplified code example of the BlockPlacedEvent handler:
    Code:
    Block block = event.getBlock();
    BlockState state = block.getState();
    MaterialData mData = state.getData();
    if(mData instanceof Tree)
    {
    Tree tree = (Tree)mData;
    tree.setDirection(BlockFace.UP);
    state.setData((MaterialData)tree);
    state.update(true);
    }
    In this example the new direction is ignored. And after some testing it seems that any change to the BlockState followed with an update(true) call is completely ignored.

    Using the setType() and setData() methods directly on the block object does effect the end result. But any change attemted via a BlockState has no effect (including changing the type and data).

    Is this behavor a bukkit bug?

    Any suggestions on how to achieve what I'm trying to do here?

    Well I haven't found a proper solution to this and I think it needs to be fixed in Bukkit. But anyway, to anyone having the same problem you can circumvent this by calling the state.update() method in a separate thread.

    So create a new class that inherits from "Thread".With a constructor that takes a BlockState. And a run() method that does little more than call update(true) on the BlockState that was supplied to the constructor. I added a small delay 50ms before the call but don't know if that's really necessary.

    So instead of calling state.update(true) from your event handler you can use the following code.

    Code:
    YourThread th = new YourThread(state);
                        th.start();
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  2. Offline

    nisovin

Thread Status:
Not open for further replies.

Share This Page