get a block's durability - getMetaData()?

Discussion in 'Plugin Development' started by MrFlieder, Feb 8, 2015.

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

    MrFlieder

  2. Offline

    Konato_K

    @MrFlieder That's for metadatable, and it's for adding your own custom metadata and getting it

    I believe getData is the only way (someone tell me if I'm wrong).

    Edit: Now I think, you could use Block#getState#getData which returns a MaterialData
     
  3. Offline

    MrFlieder

    Block#getState#getData returns a MaterialData, right.
    And #toString returns the item name with its durability in brackets, so it's easy to get the durability that way.

    But how is it possible to SET a durability to an object of Block?
    MaterialData#setData is also deprecated.

    Following code just doesn't work:
    Code:
            MaterialData md = block.getState().getData();
            ItemStack is = md.toItemStack();
    
            is.setDurability(Byte.parseByte(material[1]));
            md = is.getData();
    
            Block bl = (Block) is;
            block.getState().setData(bl.getState().getData());
    When I broadcast is#getState#getData I get LIME WOOL (5).
    But when I broadcast block#getState#getData I get WOOL (0).

    Why doesn't it work? Something wrong with transmitting the block data. Does anyone have any ideas?

    It would be easy if there is a way to get a LIME WOOL block by Material#getMaterial("LIME WOOL"), but that just doesn't work also...
     
    Last edited: Feb 9, 2015
  4. #getData#getData()
     
  5. Offline

    Techy4198

    Off topic kinda: What is it with different people using . # and :: interchangably in Java when it's not actually in a block of code?
     
  6. Offline

    Konato_K

    @MrFlieder You need to cast your MaterialData to a org.bukkit.material.Wool (as long as it's a wool what you're using), then you can set getColor and setColor to avoid the deprecation in set and get data (which in my opinion is simpler).


    @Techy4198 The # is normally because in the javadocs the browser reads like that, about ::, it's a thing in java 8
     
  7. Offline

    MrFlieder

    @Konato_K:

    Yeah, but that doesn't work with leaves for example. I would have to implement a method that checks whether the material is leave or wool or something.

    So is there no other way?
     
  8. @MrFlieder
     
  9. Offline

    1Rogue

    # is how you refer to a method or class member

    :: is also a member reference, but you can use this in code as opposed to documentation

    . is the dereference operator, it takes an object (which is the value of a reference), dereferences it, and returns the value.
     
  10. Offline

    Konato_K

    @MrFlieder You'll have to use the proper MaterialData class (I think it's called Leaves), so yeah, using the getData and setData methods it's waaaaayyyyyy more simple.
     
Thread Status:
Not open for further replies.

Share This Page