Player Visibility

Discussion in 'Spigot Plugin Development' started by MaxNatural, Jun 21, 2016.

  1. This code works but when I right click it it does change to the slime ball but then doesn't switch it back to magma cube.

    It also doesn't hide the players :confused:

    Code (Text):
        @EventHandler
        public void onPInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (p.getItemInHand().getType() == Material.MAGMA_CREAM) {
                    for (Player hplayer : Bukkit.getOnlinePlayers())
                        p.hidePlayer(hplayer);
                    p.sendMessage(Hub.vis + "Players are now hidden.");
                    ItemStack visoff = new ItemStack(Material.SLIME_BALL);
                    ItemMeta voff = visoff.getItemMeta();
                    voff.setDisplayName(ChatColor.YELLOW + "Players: " + ChatColor.RED + "OFF");
                    visoff.setItemMeta(voff);
                    p.getInventory().setItemInMainHand(visoff);
                    return;

                }
            }
            //Vis off
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (p.getItemInHand().getType() == Material.SLIME_BALL) {
                    for (Player hplayer : Bukkit.getOnlinePlayers())
                        p.showPlayer(hplayer);
                    p.sendMessage(Hub.vis + "Players are now shown.");
                    ItemStack vison = new ItemStack(Material.SLIME_BALL);
                    ItemMeta von = vison.getItemMeta();
                    von.setDisplayName(ChatColor.YELLOW + "Players: " + ChatColor.GREEN + "ON");
                    vison.setItemMeta(von);
                    p.getInventory().setItemInMainHand(vison);
                    return;
                }
            }
        }
    }
     
  2. Why do you check if the action is right click block twice? Just put it in the same if statement and make it an if else for the item in hand thing. Also the "return;" is completely pointless.
     
  3. Is new to java, but nice that you tell him so he can fix them xD
     
  4. Added the else if statement and removed the return statements.

    When I right click the magma cube this happens and when it gives me the slime ball but if I right click the slime ball it doesn't change and just sends the shown all players message.
    https://gyazo.com/9327deca901f950732fd221d337d037f

    Also it sends the same message twice with the slimeball.

    Code (Text):
        @EventHandler
        public void onPInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            //Player vis on
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (p.getItemInHand().getType() == Material.MAGMA_CREAM) {
                    for (Player hplayer : Bukkit.getOnlinePlayers())
                        p.hidePlayer(hplayer);
                    p.sendMessage(Hub.vis + "You can no longer see other players!");
                    ItemStack visoff = new ItemStack(Material.SLIME_BALL);
                    ItemMeta voff = visoff.getItemMeta();
                    voff.setDisplayName(ChatColor.YELLOW + "Players: " + ChatColor.RED + "OFF");
                    visoff.setItemMeta(voff);
                    p.getInventory().setItemInMainHand(visoff);
                } else if (p.getItemInHand().getType() == Material.SLIME_BALL) {
                        for (Player hplayer : Bukkit.getOnlinePlayers())
                            p.showPlayer(hplayer);
                        p.sendMessage(Hub.vis + "You can now see other players!");
                        ItemStack vison = new ItemStack(Material.SLIME_BALL);
                        ItemMeta von = vison.getItemMeta();
                        von.setDisplayName(ChatColor.YELLOW + "Players: " + ChatColor.GREEN + "ON");
                        vison.setItemMeta(von);
                        p.getInventory().setItemInMainHand(vison);
            }
            }
        }
    }
     
  5. Well here's why it wont go back to a magma:

    Code (Text):
    ItemStack vison = new ItemStack(Material.SLIME_BALL);
     
  6. Now it doesn't switch back to slime ball. I think I have the items mixed up :confused:

    Code (Text):
        @EventHandler
        public void onPInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            //Player vis on
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (p.getItemInHand().getType() == Material.MAGMA_CREAM) {
                    for (Player hplayer : Bukkit.getOnlinePlayers())
                        p.hidePlayer(hplayer);
                    p.sendMessage(Hub.vis + "You can no longer see other players!");
                    ItemStack visoff = new ItemStack(Material.SLIME_BALL);
                    ItemMeta voff = visoff.getItemMeta();
                    voff.setDisplayName(ChatColor.YELLOW + "Players: " + ChatColor.RED + "OFF");
                    visoff.setItemMeta(voff);
                    p.getInventory().setItemInMainHand(visoff);
                } else if (p.getItemInHand().getType() == Material.SLIME_BALL) {
                        for (Player hplayer : Bukkit.getOnlinePlayers())
                            p.showPlayer(hplayer);
                        p.sendMessage(Hub.vis + "You can now see other players!");
                        ItemStack vison = new ItemStack(Material.MAGMA_CREAM);
                        ItemMeta von = vison.getItemMeta();
                        von.setDisplayName(ChatColor.YELLOW + "Players: " + ChatColor.GREEN + "ON");
                        vison.setItemMeta(von);
                        p.getInventory().setItemInMainHand(vison);
            }
            }
        }