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 Location newLoc = blockLoc.add(loc); world.getBlockAt(newLoc).setType(block.getType()); MaterialData data = new MaterialData(block.getType()); for (byte byt : block.getBlockDataAsString().getBytes()) { data.setData(byt); } world.getBlockAt(newLoc).getState().setData(data); Original: Pasting the map: 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 this.log("Original: " + block.getBlockDataAsString()); // Gets the data from the text file.this.log("New: " + world.getBlockAt(newLoc).getState().getData().toString());
@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.
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.
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 MultiBlockStructure struct = MultiBlockStructure.create(block.getBlockDataAsString(), null);struct.build(newLoc); 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().
@Xp10d3 Try this https://www.spigotmc.org/threads/block-setdata-method-issues.223614/#post-2292654 The main difference is that it references a world block. Use something like: Code: Block myBlock = world#getBlockAt(some location) BlockState myBlockState = myBlock#getState() myBlockState#setType(...) MaterialData myData = myBlockState#getData() myData#setData(someData) myBlockState#update()