Reputation: 67
I would like to get block data from structure blocks, in the same way if you would the following command:
/data get block 1 2 3
which gives 1, 2, 3 has the following block data: {metadata: "abc", mirror: "NONE", ignoreEntities: 1b, powered: 0b, seed: 0:, author: "myplayername", rotation:"NONE", posX: 0, mode: "DATA", posY: 1, sizeX: 0, posZ: 0, integrity: 1.0f, showair: 0b, x: 1, name:"", y: 2, z:3, id: "minecraft:structure_block", sizeY: 0, sizeZ: 0, showboundingbox: 1b}
Specifically I need the data in "metadata", ie the stuff you find if you just enter into "Custom Data Tag Name". I have tried block.getBlockData()
, block.getMetadata("")
, block.getMetadata(null)
and block.getData()
. Scouring the bukkit and spigot docs and help threads for a while yielded the examples I have just tried, and pretty much everywhere else I looked had no related help.
Thanks in advance.
Upvotes: 1
Views: 1240
Reputation: 67
It turns out you need to cast the Block
type to the Structure
type to get more info about the structure block that the basic block.getType()
etc can't provide.
To get the raw string of structure block metadata (which is different to block metadata, for example this metadata remains after a server restart) do:
Structure structure_block = (Structure) block.getState(); String metadata_string = structure_block.getMetadata();
and to check that you've got it all right:
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.block.Structure; //this bit is required to implement the solution above
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
Player player = event.getPlayer();
if(block.getType() == Material.STRUCTURE_BLOCK) { //just make sure it is a structure block before trying to cast types
event.setCancelled(true); //this makes the block not break when you punch it
Structure structure_block = (Structure) block.getState(); //this bit is the important casting operation
player.sendMessage(structure_block.getMetadata()); //send the raw string data to the player directly
}
}
This code contains a whole copy-pasteable implementation (hopefully), but the important bits relevant to the answer are commented as such.
Upvotes: 1
Reputation: 1246
it seems all the stuff that is stored in the Metadata is just accesible, e.g. like so:
block.getBiome();
block.getLocation();
block.getType();
What exactly do you want to do with the metadata?
Also beware that the metadata is deleted every time the server restart's so it's not really a viable source of information , except you know for sure, that it contains proper data, e.g. if you get your block from a BlockPlaceEvent
or something.
Upvotes: 0