How to set block data value?

Myst picture Myst · Jan 28, 2015 · Viewed 7.2k times · Source

Bukkit's setData(data) and getData() are deprecated. But there's no replacement. Bukkit/Spigot JavaDoc says this about setData():

Deprecated. Magic value

Why is that?

Answer

Jojodmo picture Jojodmo · Jan 29, 2015

So far, the only way to do it is by using:

Block.setData(byte data);

So, you could do something like this:

myBlock.setData(2); // Set block data to 2

Although Block.setData() is deprecated, it still works, and will continue to work (deprecated methods in Bukkit are rarely removed, especially those for which there is no alternative). I wish I could give a better answer, but that's the only thing that you can do, as of now.

The reason it is deprecated is because Minecraft is moving away from item IDs, and switching to item names, to make it easier to expand in the future. Where you used to have to run /give player 19, you are now supposed to run /give player minecraft:sponge (although the ID still works). The same thing is going to happen to data values, instead of giving someone 35:14, you now give them red wool.

To get rid of the warning given by using a deprecated method, put @SuppressWarnings("deprecation") above the deprecated method when you use it, or above the method in which it is used.

To set the type of the block, you could use:

Block.setType(Material type);

An example is:

myBlock.setType(Material.GOLD_BLOCK); // Set block to gold block

You could also use MaterialData, but no one really knows how to use it (as far as I know). It's one of the things included in the Bukkit API, but no one knows why.

The source of WorldEdit and most other big plugins look messy because they use a lot of interfaces. To the developers, it seems very organized, but to someone reading it, it looks very messy, unless you can actually visualize the hierarchy.