Chest Name

Discussion in 'Plugin Development' started by ItsMees, Dec 3, 2013.

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

    ItsMees

    Hi guys,
    I have an problem!
    I want it to make that if an chest has an custom name it will checks if its your name!
    If it your name you have acces to the chest if it is not you have no access to the chest!
    With my code he says everytime that the chest is protecten, even if it is my name!
    My code:
    Code:java
    1. @EventHandler
    2. public void onInventoryOpenEvent(InventoryOpenEvent event){
    3. Player player = (Player) event.getPlayer();
    4. if (event.getInventory().getHolder() instanceof Chest || event.getInventory().getHolder() instanceof DoubleChest){
    5. if (event.getInventory().getName().startsWith("Chest")){
    6. }else{
    7. if (event.getInventory().getName().startsWith("Large Chest")){
    8. }else{
    9. if (event.getInventory().getName() == player.getName()){
    10. player.sendMessage("Chest Open!");
    11. event.setCancelled(false);
    12. }else{
    13. event.setCancelled(true);
    14. event.setCancelled(false);
    15. player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[Security Chests]" + ChatColor.RED + "This chest has been protected by: " + event.getInventory().getName());
    16. }
    17. }
    18. }
    19. }
    20. }


    I hope someone can help me out!
     
  2. Offline

    Eniripsa96

    Compare strings with .equals(String) rather than ==
     
  3. Offline

    ItsMees

    I tryd this:
    Code:java
    1. if (event.getInventory().getName().startsWith.equals("Large Chest"))

    But it gives an error!
     
  4. Offline

    Eniripsa96

    remove .startsWith, just have getName().equals(String);
     
  5. Offline

    ItsMees

    but I want to check where the text starts with.....
     
  6. Offline

    Eniripsa96

    then just do startsWith(String)

    The thing I told you to replace with .equals(String) is when you compare using == when you check it against the player's name.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  7. Offline

    Noxyro

    1) You should use the PlayerInteractEvent instead the InventoryOpenEvent for chest protection systems.

    2) Where and how do you set the name of a chest? Because if(event.getInventory().getName()== player.getName()){ will never become true for 2 reasons:
    1. String are compared with string1.equals(string2) or string1.equalsIgnoreCase(string2)
    2. Before you check, if the chest inventory name equals the player name, you allready checked if its "Chest" or "Large Chest" ... so how could the chest inventory name ever equal the player name then? (besides the player is named "Chest" ... lol)

    3) You don't have to extra set event.setCanceled(false) if you never canceled it before (eventually from other listeners)
     
Thread Status:
Not open for further replies.

Share This Page