How to get a placed block's name?

Discussion in 'Plugin Development' started by EgyptianKing, Jul 9, 2014.

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

    EgyptianKing

    So I have this chest with a certain name. I gave this chest that name using ItemMeta.setDisplayName(). Afterwards I placed it on the ground and realized that it still has the custom name I gave it. How can I check whether a player right clicked that chest and opened it?

    I think I've made a post similar to this earlier, but it wasn't solved so I apologize if this is considered a repost. But that post I made was a while ago.
     
  2. Offline

    wesley27

    It's late at night so excuse the minimum code I've posted(and possible errors), but I believe you could use something like this:

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. Block blk = event.getClickedBlock();
    4. Player player = event.getPlayer();
    5.  
    6. if(blk != null) {
    7. if(blk.getType == Material.CHEST) {
    8. Chest chest = (Chest)getItemMeta().getDisplayName();
    9.  
    10. if(chest = your custom name) {
    11. //send a message or whatever you want after the player clicks the chest
    12. }
    13. }
    14. }
    15. }
    16.  
     
  3. wesley27 You try to cast the display name to a chest? xD

    EDIT: And you should not save block variables. You should save their state. (Block#getState())
     
    KingFaris11 likes this.
  4. Offline

    wesley27

    TheBigSuperCraft lol sorry? Its the only thing I could think of by memory at the moment :)
     
    TheBigSuperCraft likes this.
  5. wesley27 I know, you said that it's late, but it's still funny to see those kind of errors :p
     
    wesley27 likes this.
  6. Offline

    FabeGabeMC

    wesley27 Also, shouldn't the name be stored as a String?
     
  7. Offline

    wesley27

    FabeGabeMC yes, you'd have to get the state of the block as TheBigSuperCraft said and then save the name as a string with the call that I said in my stupid reply :p
     
Thread Status:
Not open for further replies.

Share This Page