Giving player certain things after being online for 10 minutes

Discussion in 'Plugin Development' started by xMichey, Sep 8, 2015.

Thread Status:
Not open for further replies.
  1. Like you can see in the very long title, I need help with some BukkitRunnables/Scheduler.
    I got my own economy system with giving players certain amounts of money. The only thing I can't figure out is how to give players an amount of money, when they are for 10 minutes on the server. ^^ Hope you can help :D
     
  2. Schedule a sync delayed task to run after 10 minutes, and when it finally runs, check if the player is not null and is online, then give the player the money.
     
  3. Thank you for your reply, but how do I cancel it (the scheduler), if the the player is leaving the game?
     
  4. Offline

    Evonoucono

    Code:
        @EventHandler
        public void onPlayerLogin(PlayerJoinEvent event){
            Player p = event.getPlayer();
    
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                @Override
                public void run() {
                    if (p != null){
                        if (p.isOnline()){
    
                            //PAY PLAYER
    
                        }
                    }
                }
            }, 12000); //Timer in ticks, 20 =  1 second. This is 10 minutes
        }
    Not sure if p != null is necessary but this should work. Only problem is if the player logs off, and back in within that ten minutes the delayed task will run twice for that same player. You could make an arraylist of strings and add the players name to that array list when the task starts, and don't allow the task to start again if the arraylist contains the players name. Then when the delayed task finished, remove the players name from the array list.
    edit: didn't see your next question. We relpied at the same time. On a playerquitevent you could get the player, add his name to an arraylist of strings, and then check in the delayed task if that players name is in that array list, if it is don't pay that player.
     
  5. Offline

    mythbusterma

    @Evonoucono

    Don't store a reference to a Player for an undetermined period of time and then never cancel the Runnable.

    Also, don't spoonfeed.

    "p" (a terrible variable name) will never be null, because you never lose the reference. Use their UUID instead. Then see if they're still online.
     
  6. I know how to make the scheduler, but I need to know how to CANCEL the scheduler/runnable.
     
  7. Online

    timtower Administrator Administrator Moderator

    @xMichey Why not have 1 runnable that handles all players instead of 1 for each player?
     
    Zombie_Striker likes this.
  8. Offline

    mythbusterma

  9. @timtower Okay, okay. I should have been more specific from the beginning on, my bad. The real question is, how to make a single runnable which handles all the players.. how do I do that?

    Do I create a new Runnable in onEnable() and check every time if a player enters the game or what? Hopefully, you can help now too :D ^^
     
  10. Online

    timtower Administrator Administrator Moderator

    @xMichey No, it is a runnable, you check when it runs. Then you give the things.
     
  11. Yes. You need to have a HashMap containing the players name/uuid and the join time. In the run() method loop through the HashMap and check if the time => 10 minutes (depends on how you store the time)
     
  12. Example please? :) (with storing time)
     
  13. Offline

    nj2miami

    Create a HashMap<UUID, Long> which stores the player UUID and the Long is the time in milliseconds.

    When your plugin starts, start you runnable. I would say run it every minute. It will not be exactly accurate but I think close enough to what you need. To be totally accurate you would need to repeat it every second. Your call on what level of time perfection you need.

    When the runnable runs, simply compare what is in your HashMap to the current time and award what you want based on that.

    onPlayerJoin() - add them to the HashMap with the current timestamp.
    onPlayerLeave() - remove them from the HashMap (since they logged out)

    Now when your runnable runs just iterate through the Map and do your time checks.
     
Thread Status:
Not open for further replies.

Share This Page