PlayerInteractEvent Error

Discussion in 'Spigot Plugin Development' started by wiktem, Jun 9, 2016.

  1. Code (Text):
          public void onPlayerUse(PlayerInteractEvent e){
              Player p = e.getPlayer();
              if(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Turn on vanish") && (Stuff.ess.getUser(p).isVanished()))
              else if(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + "Turn off vanish") && (!Stuff.ess.getUser(p).isVanished()))
              else if(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GOLD + "Random Teleport") && (Stuff.Vanished.contains(p.getName())))
              if ((Bukkit.getOnlinePlayers().size() > 1)){
                ArrayList<Player> onlineplayers = new ArrayList<Player>();
                Random r = new Random();
                for(Player online : Bukkit.getServer().getOnlinePlayers()) {
                int index = r.nextInt(onlineplayers.size());
                Player loc = (Player) onlineplayers.get(index);
                p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Teleport")).replaceAll("%player%", loc.getName()));
              if ((Bukkit.getOnlinePlayers().size() < 2)){
                  p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No Players")));
  2. What is Line 48?
  3. it's this line:
    Code (Text):
     if(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Turn on vanish") && (Stuff.ess.getUser(p).isVanished()))
  4. The ChatColor.GREEN and the color code for Green are considered two separate objects if I remember correctly, so the issue is the fact that it's colored you need to add the § perhaps?
  5. I've always been told to only use ChatColor.COLOR, however, I will try this out
  6. It should work without too ChatColor.COLOR i use that too no the '§' but you can always test
  7. still got the same error.
  8. See if you have the issue when you remove color overall, debug a little bit. Less dependency on us.
  9. Still got the same error :eek: I really don't think it's the chatcolor.
  10. You need to first check if the item is null, if the item has item meta, and then whether or not it has a display name before trying to use any of the above.
    • Agree Agree x 1
    • Winner Winner x 1
  11. It's not then, I've had this issue before.. and me adjusting the text was the resolution :p

    Lets break down the line together.
    if(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Turn on vanish")
    Some items won't have a display name. Which could give a NPE (NullpointerException)

    to prevent this
    Code (Text):


    (Stuff.ess.getUser(p).isVanished())) <--- Boolean is a primitive datatype and it cannot be null True or False, if it's not assigned a value it'll be false by default.
    • Winner Winner x 1
  12. Boolean is not a primitive type, that's the class wrapper.

    Edit: I also said everything above what you did about 7 minutes before :p
  13. Thanks :) I will test it out tomorrow
  14. I did this:
    Code (Text):
              if(p.getItemInHand().getType() == Material.AIR || p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(null))
    and it worked perfectly! thank you :)
  15. Set this to solved, please.
  16. nevermind, I don't get any errors when holding nothing, however, I do get errors when clicking on an item.

    Code (Text):
              if(p.getItemInHand().getType() == Material.AIR || p.getItemInHand().hasItemMeta())
    @Kozs @Msrules123