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 @EventHandler public void onInventoryOpenEvent(InventoryOpenEvent event){ Player player = (Player) event.getPlayer(); if (event.getInventory().getHolder() instanceof Chest || event.getInventory().getHolder() instanceof DoubleChest){ if (event.getInventory().getName().startsWith("Chest")){ }else{ if (event.getInventory().getName().startsWith("Large Chest")){ }else{ if (event.getInventory().getName() == player.getName()){ player.sendMessage("Chest Open!"); event.setCancelled(false); }else{ event.setCancelled(true); event.setCancelled(false); player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "[Security Chests]" + ChatColor.RED + "This chest has been protected by: " + event.getInventory().getName()); } } } } } I hope someone can help me out!
I tryd this: Code:java if (event.getInventory().getName().startsWith.equals("Large Chest")) But it gives an error!
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.
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)