Setting block data (1.8)

Discussion in 'Plugin Development' started by Xp10d3, Jun 21, 2022.

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

    Xp10d3

    Hello! I am quite stumped as I'm trying to set the block data given a string. I store an entire map in a text file with the block's state as a string (via block.getData().toString()), but now I'm trying to set that data. An example of the text data is this:
    Code:
    { x: -3.0, y: -3.0, z: -3.0, type: STAINED_CLAY, world: Urban, state: STAINED_CLAY(9) }
    { x: -3.0, y: -3.0, z: -2.0, type: STAINED_CLAY, world: Urban, state: STAINED_CLAY(9) }
    { x: -3.0, y: 1.0, z: -1.0, type: BRICK_STAIRS, world: Urban, state: BRICK_STAIRS(5) facing EAST inverted }
    { x: -3.0, y: 1.0, z: 0.0, type: BRICK_STAIRS, world: Urban, state: BRICK_STAIRS(5) facing EAST inverted }
    { x: -3.0, y: 1.0, z: 1.0, type: BRICK_STAIRS, world: Urban, state: BRICK_STAIRS(5) facing EAST inverted }
    { x: -3.0, y: 1.0, z: 2.0, type: BRICK_STAIRS, world: Urban, state: BRICK_STAIRS(5) facing EAST inverted }
    
    However, the function for setting the data of the block is deprecated and doesn't set the data properly (ex. clay isn't set to the correct color, stairs aren't updated properly, etc.). Is there a better way to do this?
    Code:java
    1.  
    2. Location newLoc = blockLoc.add(loc);
    3. world.getBlockAt(newLoc).setType(block.getType());
    4. MaterialData data = new MaterialData(block.getType());
    5. for (byte byt : block.getBlockDataAsString().getBytes()) {
    6. data.setData(byt);
    7. }
    8. world.getBlockAt(newLoc).getState().setData(data);
    9.  


    Original:
    [​IMG]
    Pasting the map:
    [​IMG]

    EDIT: I also logged the new and old block states stored in the text file.
    Code:
    [16:24:21 INFO]: Original: STAINED_CLAY(9)
    [16:24:21 INFO]: New: STAINED_CLAY(0)
    [16:24:21 INFO]: Original: AIR(0)
    [16:24:21 INFO]: New: AIR(0)
    [16:24:21 INFO]: Original: AIR(0)
    [16:24:21 INFO]: New: AIR(0)
    [16:24:21 INFO]: Original: AIR(0)
    [16:24:21 INFO]: New: AIR(0)
    [16:24:21 INFO]: Original: AIR(0)
    [16:24:21 INFO]: New: AIR(0)
    [16:24:21 INFO]: Original: AIR(0)
    [16:24:21 INFO]: New: AIR(0)
    [16:24:21 INFO]: Original: AIR(0)
    [16:24:21 INFO]: New: AIR(0)
    [16:24:21 INFO]: Original: AIR(0)
    [16:24:21 INFO]: New: AIR(0)
    [16:24:21 INFO]: Original: BRICK_STAIRS(2) facing NORTH
    [16:24:21 INFO]: New: BRICK_STAIRS(3) facing SOUTH
    
    As you can see, the block states are different.
    Code:java
    1.  
    2. this.log("Original: " + block.getBlockDataAsString()); // Gets the data from the text file.
    3. this.log("New: " + world.getBlockAt(newLoc).getState().getData().toString());
    4.  
     
    Last edited: Jun 21, 2022
  2. Offline

    CraftCreeper6

    @Xp10d3
    setData isn't deprecated as far as the docs go.

    The issue could be that you aren't updating the block data after you set it.

    Try using blockState#update() and see if the issue persists.

    EDIT: Just remembered you're 1.8, so setData might be deprecated but should still work.
     
    Last edited: Jun 22, 2022
  3. Offline

    Xp10d3

    Hey, sorry for the late response. I'm on vacation right now haha. .update didn't seem to do anything sadly. I'm just using a library for this instead.
     
  4. Offline

    Xp10d3

    Bit of an update for those who are interested. I'm using RedLib as a dependency and using MultiBlockStructure to save and paste blocks with block data.
    Code:java
    1.  
    2. MultiBlockStructure struct = MultiBlockStructure.create(block.getBlockDataAsString(), null);
    3. struct.build(newLoc);
    4.  

    However, I am not quite sure how to do this with only Bukkit. If anyone want's to look at the source code for RedLib, the GitHub is here:
    https://github.com/Redempt/RedLib
    Look at MultiBlockStructure specifically for getting/pasting blocks with block data. If anyone has ideas on how to accomplish this with vanilla Bukkit, feel free to reply here. So far, blockState#update() doesn't seem to work nor does #setData().
     
  5. Offline

    CraftCreeper6

Thread Status:
Not open for further replies.

Share This Page