Player vanish not working - dye giving error!

Discussion in 'Spigot Plugin Development' started by MGPro, Nov 17, 2017.

  1. Hello spigot,
    I am coding a hub plugin for my server and i am currently trying to add a player vanish to the very right of my hotbar. So like there is 1 lime dye then you press it and the dye will change to a gray dye and all players will be invisible then you can press it again to be able to see them again but for some reason it is not working? I am running 1.7.10 and the server is 1.7.10-1.8 thanks!

    Code (Text):
        @EventHandler
        public void items(final PlayerInteractEvent e) {
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) {
                return;
            }
            if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.translateAlternateColorCodes('&', "&6&l» &9&lShow Players &6&l«"))) {
                final ItemStack vanish = new ItemStack(351, 1, (short)8);
                final ItemMeta vanishMeta = vanish.getItemMeta();
                vanishMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&9&l» &7&lHide Players &9&l«"));
                vanish.setItemMeta(vanishMeta);
                this.vanishd.remove(e.getPlayer().getName());
                Player[] onlinePlayers;
                for (int length = (onlinePlayers = Bukkit.getOnlinePlayers()).length, j = 0; j < length; ++j) {
                    final Player p = onlinePlayers[j];
                    if (!p.equals(e.getPlayer())) {
                        e.getPlayer().hidePlayer(p);
                    }
                }
                e.getPlayer().getInventory().setItemInHand(vanish);
                return;
            }
            if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.translateAlternateColorCodes('&', "&9&l» &7&lHide Players &9&l«"))) {
                final ItemStack vanish = new ItemStack(351, 1, (short)10);
                final ItemMeta vanishMeta = vanish.getItemMeta();
                vanishMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&6&l» &9&lShow Players &6&l«"));
                vanish.setItemMeta(vanishMeta);
                this.vanishd.add(e.getPlayer().getName());
                Player[] onlinePlayers2;
                for (int length2 = (onlinePlayers2 = Bukkit.getOnlinePlayers()).length, k = 0; k < length2; ++k) {
                    final Player p = onlinePlayers2[k];
                    if (!p.equals(e.getPlayer())) {
                        e.getPlayer().showPlayer(p);
                    }
                }
                e.getPlayer().getInventory().setItemInHand(vanish);
            }
        }
    and when i press the dye it comes up with a error!

    Code (Text):
    [Server thread/ERROR]: Could not pass event PlayerInteractEvent to HCMetroHub v1.5
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:297) ~[Server.jar:git-PaperSpigot-a925999]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[Server.jar:git-PaperSpigot-a925999]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:513) [Server.jar:git-PaperSpigot-a925999]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:498) [Server.jar:git-PaperSpigot-a925999]
        at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:242) [Server.jar:git-PaperSpigot-a925999]
        at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:212) [Server.jar:git-PaperSpigot-a925999]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:654) [Server.jar:git-PaperSpigot-a925999]
        at net.minecraft.server.v1_7_R4.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:50) [Server.jar:git-PaperSpigot-a925999]
        at net.minecraft.server.v1_7_R4.PacketPlayInBlockPlace.handle(PacketPlayInBlockPlace.java:86) [Server.jar:git-PaperSpigot-a925999]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:189) [Server.jar:git-PaperSpigot-a925999]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:103) [Server.jar:git-PaperSpigot-a925999]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:801) [Server.jar:git-PaperSpigot-a925999]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:286) [Server.jar:git-PaperSpigot-a925999]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:651) [Server.jar:git-PaperSpigot-a925999]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:557) [Server.jar:git-PaperSpigot-a925999]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [Server.jar:git-PaperSpigot-a925999]
    Caused by: java.lang.NullPointerException
        at net.hcmetro.hub.listeners.PlayerJoinListener.items(PlayerJoinListener.java:39) ~[?:?]
        at sun.reflect.GeneratedMethodAccessor7.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_151]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_151]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:293) ~[Server.jar:git-PaperSpigot-a925999]
    Thanks!
     
  2. You are getting error on line 39 of class PlayerJoinListener. Can you paste that line of code?
     
  3. Code (Text):
      if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.translateAlternateColorCodes('&', "&6&l» &9&lShow Players &6&l«"))) {
    here
     
  4. Please, the file.
     
    • Like Like x 1
  5. You want the hole code?

    Code (Text):
    package net.hcmetro.hub.listeners;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    import net.hcmetro.hub.utils.ItemStackBuilder;

    public class PlayerJoinListener implements Listener {

        private final String line = ChatColor.GRAY.toString() + ChatColor.STRIKETHROUGH
                + "-----------------------------------------------------";

        ArrayList<String> vanishd;
       
        public void PlayerListener() {
            this.vanishd = new ArrayList<String>();
        }
       
        @EventHandler
        public void items(final PlayerInteractEvent e) {
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) {
                return;
            }
            if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.translateAlternateColorCodes('&', "&6&l» &9&lShow Players &6&l«"))) {
                final ItemStack vanish = new ItemStack(351, 1, (short)8);
                final ItemMeta vanishMeta = vanish.getItemMeta();
                vanishMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&9&l» &7&lHide Players &9&l«"));
                vanish.setItemMeta(vanishMeta);
                this.vanishd.remove(e.getPlayer().getName());
                Player[] onlinePlayers;
                for (int length = (onlinePlayers = Bukkit.getOnlinePlayers()).length, j = 0; j < length; ++j) {
                    final Player p = onlinePlayers[j];
                    if (!p.equals(e.getPlayer())) {
                        e.getPlayer().hidePlayer(p);
                    }
                }
                e.getPlayer().getInventory().setItemInHand(vanish);
                return;
            }
            if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.translateAlternateColorCodes('&', "&9&l» &7&lHide Players &9&l«"))) {
                final ItemStack vanish = new ItemStack(351, 1, (short)10);
                final ItemMeta vanishMeta = vanish.getItemMeta();
                vanishMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&6&l» &9&lShow Players &6&l«"));
                vanish.setItemMeta(vanishMeta);
                this.vanishd.add(e.getPlayer().getName());
                Player[] onlinePlayers2;
                for (int length2 = (onlinePlayers2 = Bukkit.getOnlinePlayers()).length, k = 0; k < length2; ++k) {
                    final Player p = onlinePlayers2[k];
                    if (!p.equals(e.getPlayer())) {
                        e.getPlayer().showPlayer(p);
                    }
                }
                e.getPlayer().getInventory().setItemInHand(vanish);
            }
        }
       
        @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
        public void onJoin(PlayerJoinEvent event) {
            event.setJoinMessage(null);
            Player player = event.getPlayer();
           
            player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 3));
           
            player.getInventory().setItem(0, new ItemStackBuilder(Material.ENDER_PEARL).build());
       
            player.getInventory().setItem(8, new ItemStack(351, 64, (short)10));
           
            player.sendMessage(line);
            player.sendMessage(ChatColor.YELLOW + "Welcome to " + ChatColor.GOLD.toString() + ChatColor.BOLD
                    + "HCMetro " + ChatColor.GRAY + "Network" + ChatColor.YELLOW + ".");
            player.sendMessage("");
            player.sendMessage(ChatColor.YELLOW + "Store" + ChatColor.GRAY + " » " + ChatColor.GOLD + "store.hcmetro.com");
            player.sendMessage(ChatColor.YELLOW + "Voice" + ChatColor.GRAY + " » " + ChatColor.GOLD + "ts.hcmetro.com");
            player.sendMessage(
                    ChatColor.YELLOW + "Twitter" + ChatColor.GRAY + " » " + ChatColor.GOLD + "twitter.com/HCMetroNetwork");
            player.sendMessage(line);
            player.getInventory().setItem(4,
                    new ItemStackBuilder(Material.COMPASS).setName(ChatColor.YELLOW + "Server Selector").build());
        }
       
          @EventHandler
          public void onPlayerDamage(EntityDamageEvent event)
          {
            if ((event.getEntity() instanceof Player)) {
              event.setCancelled(true);
            }
          }
    }
     
  6. Please verify if itemInHand is != null before doing another verification
     
  7. Strahan

    Benefactor

    Plus you get getting item meta when whatever they have may not have meta, and may not have a display name. Always verify, never assume.