Leave event

Discussion in 'Plugin Development' started by Vixelate, Apr 12, 2014.

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

    Vixelate

    Hey!

    I tried the following code to make it so that when people leave the server they have 2 minutes to rejoin. If they do not rejoin in that time they are suppose to be removed from the white-list. I also only want this to be able to happen after the game has started. But I cannot get it to work.
    Any ideas on how to fix it?
    Thanks!

    Code:
    @EventHandler(priority=EventPriority.HIGHEST)
            public void onQuit(PlayerQuitEvent event){
                Player player = event.getPlayer();
                Bukkit.broadcastMessage(ChatColor.YELLOW +player.getName()+ " left the server and will be removed from white-list in 2 minutes if not re-joined.");
                delay2(player);
           
                }
           
            public void delay2(final Player player){
                getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                    public void run(){
                        if(player.isOnline()){
                   
                        }else{
     
                            player.setWhitelisted(false);
                            Bukkit.broadcastMessage(ChatColor.RED +player.getName()+" is out of the game!");
                   
                        }
                        }
                    }, 2400L);
     
  2. Offline

    FabeGabeMC

    Make a boolean called Countdown or something and make it true when the countdown starts, and false when it ends. Then, check if the Countdown is true and then remove the player from the whitelist. Technically this:
    Code:java
    1. //Top of class
    2. public boolean countdown = false;
    3.  
    4. @EventHandler
    5. public void onQuit(PlayerQuitEvent event){
    6. Player player = event.getPlayer();
    7. Bukkit.broadcastMessage(ChatColor.YELLOW +player.getName()+ " left the server and will be removed from white-list in 2 minutes if not re-joined.");
    8. if(Countdown = true){
    9. player.setWhitelisted(false);
    10. Bukkit.broadcastMessage(ChatColor.RED +player.getName()+" is out of the game!");
    11. }else return;
    12.  
    13. }
    14.  
    15. public void delay2(final Player player){
    16. getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    17. public void run(){
    18. //Beginning of countdown
    19. countdown = true;
    20. //End of countdown
    21. countdown = false;
    22. }
    23. }, 2400L);
    24. }


    That would technically be the code, but I explained so you wouldn't get confused.

    Hope this helped! :)

    ~Gabe
     
  3. Offline

    TigerHix

    FabeGabeMC

    This will not work when multiple players left.

    It's pretty simple, you just need to listen to PlayerJoinEvent.

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerJoin(PlayerJoinEvent event) {
    4. if (System.currentTimeMillis - ((OfflinePlayer) event.getPlayer()).getLastPlayed() > 60 * 2 * 1000) {
    5. ((OfflinePlayer) event.getPlayer()).setWhitelisted(false);
    6. event.getPlayer().kickPlayer("HAHAHA You haven't logged in for a soooo long time!!");
    7. }
    8. }
    9.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    FabeGabeMC

  5. FabeGabeMC With the syntax and logical errors, I can assure you that it doesn't.
     
  6. Offline

    TigerHix


    Not really.. you have not tested it yet, right.

    Code:java
    1. //Top of class
    2. public boolean countdown = false;
    3.  
    4. @EventHandler
    5. public void onQuit(PlayerQuitEvent event){
    6. Player player = event.getPlayer();
    7. Bukkit.broadcastMessage(ChatColor.YELLOW +player.getName()+ " left the server and will be removed from white-list in 2 minutes if not re-joined.");
    8. if(Countdown = true){
    9. player.setWhitelisted(false);
    10. Bukkit.broadcastMessage(ChatColor.RED +player.getName()+" is out of the game!");
    11. }else return;
    12.  
    13. }
    14.  
    15. public void delay2(final Player player){
    16. getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    17. public void run(){
    18. //Beginning of countdown
    19. countdown = true;
    20. //End of countdown
    21. countdown = false;
    22. }
    23. }, 2400L);
    24. }
    25.  


    1.
    Code:java
    1. if(Countdown = true)
    won't work.. first the 'C' is uppercase, second '=' operator is not same as '==', it's an assignment operator.

    2. What is the logic here in onQuit() and delay2() method? After broadcasting message, why you check if variable Countdown equals true? Also in the BukkitRunnable, you assign true to countdown, and you immediately assign false to countdown. What's the meaning?

    If your code really worked, you must made some changes to it like below.

    Code:java
    1. //Top of class
    2. public ArrayList<UUID> expired = new ArrayList<UUID>();
    3.  
    4. @EventHandler
    5. public void onJoin(PlayerJoinEvent event){
    6. if (expired.contains(event.getPlayer())) {
    7. event.getPlayer().kickPlayer("You have already expired.");
    8. }
    9. }
    10.  
    11. @EventHandler
    12. public void onQuit(PlayerQuitEvent event){
    13. Player player = event.getPlayer();
    14. Bukkit.broadcastMessage(ChatColor.YELLOW +player.getName()+ " left the server and will be removed from white-list in 2 minutes if not re-joined.");
    15. final UUID uuid = event.getPlayer().getUniqueId();
    16. new BukkitRunnable() {
    17. public void run() {
    18. for (Player player : Bukkit.getOnlinePlayers()) {
    19. if (player.getUniqueId() == uuid) { // Player is online again
    20. return;
    21. }
    22. }
    23. // If not
    24. expired.add(uuid);
    25. ((OfflinePlayer) event.getPlayer()).setWhitelisted(false);
    26. Bukkit.broadcastMessage(ChatColor.RED +player.getName()+" is out of the game!");
    27. }
    28. }.runTaskLater(2400L);
    29. }
    30.  


    And obviously, it's much complex.
     
  7. Offline

    FabeGabeMC

    TigerHix Just saying that it works for me. Not complaining that your method is wrong.
     
  8. Offline

    TigerHix

    Nope.. I just want to point out your one will not work, that's the problem. :)
     
  9. Offline

    FabeGabeMC

    TigerHix I don't want to get in an argument now. I was just stating that it works for me.
     
  10. Offline

    Wolfey

    How is that even possible?
     
  11. Offline

    FabeGabeMC

  12. Offline

    Wolfey

    FabeGabeMC I tested out the code you posted, it gave me a bunch of errors.

    Not that I needed to, just by looking at it, I could already tell.
     
  13. Offline

    FabeGabeMC

    Wolfey I just don't know. Works for me, lol.
     
  14. Offline

    TigerHix

    Stop replying like "yea it works for me, it works for me". When others point out you are wrong, and what they are saying are actually true and proved, please admit your mistake.

    Your code even has compiling error. How is it possible then, to work for you, "lol"?
     
Thread Status:
Not open for further replies.

Share This Page