Set Block State from Inputted String

Discussion in 'Plugin Development' started by Guru2764, Sep 1, 2019.

  1. Offline

    Guru2764

    Hello!

    I am having issues understanding the different Block-related classes, and I was unable to find exactly what I need by searching.

    I am trying to get information from an inputted String and set a block in the world to be that type and state. So currently it takes a String named block (after ensuring it is a valid block), and sets the type:

    currentBlock.setType(Material.getMaterial(block));

    I would like to expand this to set not only the material of the block, but also the block state. So if stone_brick_stairs[north,top] was inputted, it would set currentBlock to be stone brick stairs facing north on the top half of the block. I understand that I may have to split the string, using the brackets to separate the data, but I am unsure how to turn something like "facing=north", or just "north", into something that I can use in a method to change the block state of currentBlock. I would like this to work with as many block states as possible if possible, like "snowy=true" etc.

    Thanks for any help or advice you can provide!
     
  2. Offline

    MrDaniel

    @Guru2764

    To convert string to anything else use
    Code:
    something.valueOf("String")
     
  3. Offline

    DerDonut

    The problem is that BlockState is just an Interface and doesn't not provide functions for all the blocks. For example to work with "facing=north" you would have to cast the blockstate to Directorial, but you have no function "setSnowy" or something like that in Directorial. So you probably have to check what kind of blockstate the specific block has and handle the input depending on that.

    Another way would be to work with subIds. Probably easier, but not supported by new bukkit versions
     
  4. Offline

    Guru2764

    Alright, I will look into this, maybe using a switch statement would be best to handle each possible state. Thanks for the advice!
     

Share This Page