Inventory.getName() or Inventory.getTitle() doesn't work!

Discussion in 'Plugin Development' started by Agentleader1, Nov 24, 2014.

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

    Agentleader1

    I'm currently developing a plugin for my server! And one issue I have is Inventory.getName() or Inventory.getTitle(). This is actually the first time I posted a forum question, because I usually find the answer to any of my problems by debugging and trying over and over or searching it.

    I'm using an event, and trying to detect the Inventory Name. What I'm talking about is the "chest" name where it usually says "Chest". If you rename a chest, it would be called that on the top of the chest. I failed to retrieve the name, here is the code for the event:
    Code:java
    1. @SuppressWarnings("unused")
    2. @EventHandler
    3. public void onClick(InventoryClickEvent ice){
    4. FileConfiguration config = getConfig();
    5. if(ice.getWhoClicked() instanceof Player){
    6. Player player = (Player) ice.getWhoClicked();
    7. if(ice.getInventory().getTitle() == "§6Hub Features" || ice.getInventory().getTitle() == "§eHub Features" || ice.getInventory().getTitle() == " §6Hub Features" || ice.getInventory().getTitle() == " §eHub Features" || ice.getInventory().getName() == "§6Hub Features" || ice.getInventory().getName() == "§eHub Features" || ice.getInventory().getName() == " §6Hub Features" || ice.getInventory().getName() == " §eHub Features"){
    8. ice.setCancelled(true);
    9. if(ice.getCurrentItem().getTypeId() == 256 && ice.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(" §a§lPaintball Shotgun")){
    10. ice.setCancelled(true);
    11. try{
    12. boolean bool = config.getBoolean("Gadgets." + player.getName() + ".Shotgun");
    13. }catch(Exception ex){
    14. config.set("Gadgets." + player.getName() + ".Shotgun", false);
    15. }
    16. if(config.getBoolean("Gadgets." + player.getName() + ".Shotgun")){
    17. ItemStack shotgun = new ItemStack(256);
    18. ItemMeta im = shotgun.getItemMeta();
    19. im.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Paintball Shotgun");
    20. shotgun.setItemMeta(im);
    21. player.getInventory().setHeldItemSlot(2);
    22. player.getInventory().setItemInHand(shotgun);
    23. player.updateInventory();
    24. }else{
    25. if((config.getInt("Boltz." + player.getName()) > 1250) || (config.getInt("Boltz." + player.getName()) == 1250)){
    26. config.set("Gadgets." + player.getName() + ".Shotgun", true);
    27. config.set("Boltz." + player.getName(), config.getInt("Boltz." + player.getName()) - 1250);
    28. player.sendMessage(ChatColor.AQUA + "[DT-Hub] " + ChatColor.GREEN + "Successfully bought paintball shotgun!");
    29. ItemStack shotgun = new ItemStack(256);
    30. ItemMeta im = shotgun.getItemMeta();
    31. im.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Paintball Shotgun");
    32. shotgun.setItemMeta(im);
    33. player.getInventory().setHeldItemSlot(2);
    34. player.getInventory().setItemInHand(shotgun);
    35. player.updateInventory();
    36. updateScoreboard(player);
    37. }else{
    38. player.sendMessage(ChatColor.AQUA + "[DT-Hub] " + ChatColor.RED + "Insufficient funds!");
    39. }
    40. }
    41. }
    42. player.closeInventory();
    43. player.updateInventory();
    44. saveConfig();
    45. }
    46. }
    47. }


    Just ignore all that, I really care most for:

    Code:java
    1. if(ice.getInventory().getTitle() == "§6Hub Features" || ice.getInventory().getTitle() == "§eHub Features" || ice.getInventory().getTitle() == " §6Hub Features" || ice.getInventory().getTitle() == " §eHub Features" || ice.getInventory().getName() == "§6Hub Features" || ice.getInventory().getName() == "§eHub Features" || ice.getInventory().getName() == " §6Hub Features" || ice.getInventory().getName() == " §eHub Features"){


    I've tried so many different ways to fix this, but can't find a way. Also know that the code DOES NOT output any error whatsoever. How do I retrieve the name of any inventory currently opened?
     
  2. Offline

    tommyhoogstra

    Use .equals when checking strings, not sure if this is your problem though.
     
  3. Offline

    Agentleader1

    No, that's not the problem. The problem is getting the name of the inventory. And I typed the exact name in the boolean of the inventory I'm using, but it doesn't go on.
     
  4. Offline

    Skionz

    Agentleader1 What does Inventory#getTitle() outprint in the console when debugging?
     
  5. Offline

    Agentleader1

    Absolutely nothing. Not an error
     
  6. Offline

    Skionz

    Agentleader1 Player#getInventory() gets the players actual inventory, not the inventory that is currently open
     
  7. Offline

    Agentleader1

    I know
     
  8. Offline

    Skionz

    Agentleader1 Oh I misread your code, I apologize. Is the actual event being called? And as tommyhoogstra said you have to compare Strings with .equals instead of ==.
     
  9. Offline

    CullanP

    If this doesn't work, try changing the name of the inventory to be ChatColor.YELLOW/GOLD
    Code:
    Inventory inv = ice.getInventory();
    if(inv.getTitle().equals(ChatColor.GOLD + "Hub Features") || inv.getTitle().equals(ChatColor.YELLOW + "Hub Features")) {
     
  10. Offline

    Agentleader1

    CullanP Skionz does .equalsIgnoreCase("") count as well?

    CullanP Skionz Agentleader1

    Nevermind guys, got it! Thanks, it really helped. I F**K!** H8 Java Syntax!

    <Edit by mrCookieSlime: Merged posts. Please dont double post. There is an Edit-Button right next to the Date.>
     
  11. Offline

    tommyhoogstra

    Agentleader1 yes equalsIgnoreCase can be used, as you can see it's equals, that ignores uppercase/lowercase
     
Thread Status:
Not open for further replies.

Share This Page