Having trouble hiding players on player join

Discussion in 'Plugin Development' started by Tezlastorme, Aug 16, 2012.

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

    Tezlastorme

    Hey guys, I'm trying to vanish players and it's working except that I can't make it keep people invisible when someone joins the game. The easiest way to explain this is to show you my listener class so here it is:

    Code:
    package me.tezlastorme.Boom;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
     
    public class BoomPlayerListener implements Listener, BoomObjectHolder{
       
        public Boom plugin;
     
        public BoomPlayerListener(Boom instance) {
            plugin = instance;
        }
       
        @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerJoin(PlayerJoinEvent event){
            if(boom.invisible.contains(event.getPlayer())){
                Player invisible = event.getPlayer();
                for(Player player: Bukkit.getServer().getOnlinePlayers()){
                    player.hidePlayer(invisible);
                }
            }else{
                Player blinded = event.getPlayer();
                for(Player player: Bukkit.getServer().getOnlinePlayers()){
                    if(!boom.invisible.contains(player)){
                        blinded.showPlayer(player);
                    }
                }
                for(Player player: Bukkit.getServer().getOnlinePlayers()){
                    if(boom.invisible.contains(player)){
                        blinded.hidePlayer(player);
                    }
                }
            }
        }
    }
    That's the bit relevant to vanishing anyway.

    Thanks in advance for the replies :)
     
  2. Offline

    keensta

    May i ask what you are using to store the current hidden players.

    Also i use something a bit like this. The players that are invisable(Disguised) in my plugin are placed into a hashmap (String, Block). So here is the code i use. Basicly you should use something like this but change it if you A. Don't use a string and use Player. B. If you are not using a hashmap.

    for (String name : disguised.keySet()) {
    Player ply2 = Bukkit.getPlayerExact(name);
    if (ply2 == null)
    continue;

    ply.hidePlayer(ply2);
    }
     
  3. Offline

    Jogy34

    First you should always use the player's name not the actually Player object when storing players. If you use the Player object it will typically cause memory leaks.

    Second bukkit fires the events 1 tick before they actually happen so what you are actually doing is trying to hide a player before they actually join so you may have to delay it for 1 tick in order to get it to work properly.
     
  4. Offline

    Tezlastorme

    How do I delay it then? That would be useful in other plugins as well.
     
  5. Offline

    Jogy34

    Something like this:
    Code:
    plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()
    {
        public void run()
        {
            //do stuff
        }
    }, 1); //Replace the 1 with how many ticks you want to delay it for
    
    If this is in your main class you can replace 'plugin' with 'this'. Otherwise you have to use an instance of your class that extends JavaPlugin there.
     
  6. Offline

    rjVapes

    What Jogy said, but you'll probably want scheduleSyncDelayedTask, just a typo I think, the signature is for that function as well, so that's all you should need to change.
     
  7. Offline

    Jogy34

    Ya I meant a delayed not repeating task.
     
  8. Offline

    whitehooder

    In the newer builds the event should not be triggered before player has spawned. But for now its like Jogy34 said ;)
     
  9. Offline

    CorrieKay

    Wut.

    I dont think this is correct. They fire just before the event actually takes place, but it really dont think it happens as far back as one entire tick...
     
  10. Offline

    whitehooder

    Well, it's probably not a tick, but it is more than nothing. ;) Which makes you unable to do anything without scheduling it ;)
     
  11. Offline

    CorrieKay

    i beg to differ, my invisibility class doesnt use scheduling to hide players when they join :p
     
  12. Offline

    whitehooder

    Which build are you at?
    The latest dev builds has this error fixed i believe.
     
  13. Offline

    CorrieKay

    ...er, its not an error. lol. The reason you dont have to schedule any changes at a join event is because by this point, the player shouldnt be denied access to the server. The server has already generated the player object, and all of its fields are persistant. It is a fully fledged object and you can do whatever you like with it at this point. In fact, during the player join event is the PRIME time to hide a player, as if you do it during this point, it should hide them before it adds them to the other players entity trackers.

    i believe it goes like this
    >player attempts to log in
    >player pre login event (the player is handshaking the server)
    >player login event (i could be wrong, but i believe authentication happens here.(either there or at the prelogin event))
    >server accepts the login, and generates the player object
    >player login event is fired
    >player actually joins
     
  14. Offline

    whitehooder

    So do I. I totally agree with this, but in the newer releases I believe I've read about setting the event to trigger later than before. Which will fix it. I've fixed quite a lot of errors with teleporting and such on multiple events by simply scheduling it.
    Edit: I may be mixing this up with the respawnevent, but I believe it's the same principle.
     
  15. Offline

    Tezlastorme

    What do I put that code round?
     
Thread Status:
Not open for further replies.

Share This Page