Setting block data (1.8)

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

  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 at 8:49 AM

Share This Page