Getting Data from Block Location

Discussion in 'Plugin Development' started by electro4fun, Jul 2, 2014.

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

    electro4fun

    So I was wondering how I would go about getting the data for a specific block in a world. Currently I have:
    Code:java
    1. loc.getBlock().getData();

    However the method .getData() is depreciated so my real question is what is the intended way instead of using the older way to get block data. I have also tried:
    Code:java
    1. loc.getBlock().getState().getData().toItemstack().getDurability();

    But that didn't seem to work :/.
     
  2. Offline

    Heirteir

    electro4fun
    Your question is unclear on exactly what your trying to do.
     
  3. Offline

    electro4fun

    Heirteir
    I am trying to get the data of a block at a given location. For example wool, stone-brick and other blocks that are distinguished by their data as well as their type.
     
  4. Offline

    Heirteir

    electro4fun
    So your trying to get the type of block it is?
    Code:java
    1. block.getType().equals(Material.DIAMOND_BLOCK);
    2.  

    like that?
     
  5. Offline

    electro4fun

    Heirteir
    Nope the data of the block. For example the old id used to be 95:6, 6 being the byte data value, also what I am trying to get.
     
  6. Offline

    Heirteir

    electro4fun
    I have only seen this possible with ItemStack's
     
  7. Offline

    mythbusterma

    electro4fun
    Block#getData()?

    It's deprecated but it does what you're looking for.
     
  8. Offline

    electro4fun

    mythbusterma I am wondering why it is depreciated, and since it is depreciated there must be another 'right' way of doing it.
     
  9. Offline

    mythbusterma

    If you look at its documentation, it's deprecated for "magic value" i.e. data value 4 doesn't really mean much to someone without knowing exactly what the block is. The prefered alternative is to cast the BlockState of the Block to its appropriate material and then reading in the data from that.

    For example, instead of reading in the data value for wool, you can use the interface Wool and call the method Wool#getDyeColor() to see what the block data is in a meaningful way, in this case DyeColor.RED or something of that nature.

    You'd have to do this with every type of Block you plan to be acting on.

    EDIT: P.S. Deprecated =/= depreciated, I know it's an easy mistake to make, I used to make it all the time.
     
  10. Offline

    electro4fun

    mythbusterma Ah, thank you for the explanation, one that I could not find elsewhere.
     
    mythbusterma likes this.
Thread Status:
Not open for further replies.

Share This Page