Solved "The Magic Clock" - Player visibility working but wut?

Discussion in 'Plugin Development' started by Krotass, Dec 9, 2013.

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

    Krotass

    Hey guys. I'm currently coding a lobby plugin for my friends minigame server, just a simple one just what every other minigame server is using and I'm having some trouble setting up "The Magic Clock" It works fine but the message it sents will be the same as how many players there's only. Here's aan example

    Example: 1 player online = 1 message to the player if he uses the clock
    2 players online = 2 messages to the player if he uses the clock
    The message is sent to the person using the clock and he's the only person who's getting the message, so that's working fine

    Please help me :D (sry I'm bad at explaining stuff)

    Code

    Code:
        ArrayList<String> players = new ArrayList<String>();
       
        @EventHandler
        public void OnJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            players.add(player.getName());
        } 
       
       
        @EventHandler
        public void onPlayerClickEvent(PlayerInteractEvent event){
        Player sender = event.getPlayer();
        if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)){
        if(event.getPlayer().getItemInHand().getType().equals(Material.WATCH)){
        if(!this.players.contains(event.getPlayer().getName())){
        for(Player p : this.getServer().getOnlinePlayers()){
        event.getPlayer().hidePlayer(p);
        sender.sendMessage(ChatColor.YELLOW + "You have toggled player visibility!");
        }
        event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.BURP, 6F, 1F);
        this.players.add(event.getPlayer().getName());
        }else{
        for(Player p : this.getServer().getOnlinePlayers()){
        event.getPlayer().showPlayer(p);
        sender.sendMessage(ChatColor.YELLOW + "Players are now visible again!");
        }
        event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.BURP, 6F, 1F);
        this.players.remove(event.getPlayer().getName());
        }
        }
        }
        }
     
  2. Offline

    SourceForums

    Krotass
    This is what I did for my 'magic clock'. I had the some error as you but I somehow fixed it. :p
    Code:java
    1. @EventHandler
    2. public void onPlayerClockUseEvent(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. ItemStack clock = new ItemStack(Material.WATCH, 1);
    5. ItemMeta clockMeta = (ItemMeta) clock.getItemMeta();
    6. clockMeta.setDisplayName(ChatColor.GOLD + "Visibility");
    7. clock.setItemMeta(clockMeta);
    8. if(player.getItemInHand().equals(clock)){
    9. for(Player targetPlayers : Bukkit.getOnlinePlayers()){
    10. if(player.canSee(targetPlayers)){
    11. player.hidePlayer(targetPlayers);
    12. player.sendMessage(ChatColor.GOLD + "Player visibility has been toggled to: ON");
    13. }else if(!player.canSee(targetPlayers)){
    14. player.showPlayer(targetPlayers);
    15. player.sendMessage(ChatColor.GOLD + "Player visibility has been toggled to: OFF");
    16. }
    17. }
    18. }
    19. }
     
  3. Offline

    Krotass

  4. Offline

    SourceForums

    Krotass
    What do you mean? If everything's all fixed, remember to mark the thread as solved. :D
     
  5. Offline

    Aqgorn

    Try indenting your hidePlayer under for(Player p: ...).
     
  6. Offline

    Krotass

    SourceForums It's not fixed. I can't make your code work
     
  7. Offline

    PluginMaker

    just put the sendmessage out of the loop on your original code.
    there's no wonder you send multiple messages- you are sending them inside a loop...
     
  8. Offline

    L33m4n123

    You are doing following

    • Iteratete through all online players
    • For every online player hide them AND send the message that he toggled it
    What you want to do is
    • Iterate through all online players
    • hide them
    • OUTSIDE the for-loop send him the message that he toggled it
     
    JPG2000 likes this.
  9. Offline

    Krotass

  10. Offline

    Awesomedanguy

    Also, Krotass to fix a lot of bugs, under the "public void onPlayerClockUseEvent" you would want to say,

    Code:java
    1. Action a = event.getAction();
    2.  
    3. if(a == Action.PHYSICAL || event.getItem() == null || event.getItem().getType() == Material.AIR) return;


    That will basically make sure if the person is going on a pressure plate and is holding a clock it just returns nothing & if the item is == null or == air it returns .
     
Thread Status:
Not open for further replies.

Share This Page