Remove a player from a List after time

Discussion in 'Plugin Development' started by Soxra, Feb 8, 2013.

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

    Soxra

    Hi,
    i want to create a very simple PvP Protection for Newbies. If they join the first time, a timer of 20minutes starts and they get added into a Player List(to know who is protected). After those 20minutes, the player should get removed from that list, but i don't know how i could do that.
    Would be nice if someone could help me.
     
  2. Make it a HashMap and store the PlayerName and the time their protection should expire.
    Then make a task (Think that's what they're called) to run every x minutes and check the list. When it checks the list it removes anyone with a time (or HashMap value) greater than their expiry time. Then add them to a list of "Already been protected" players. The list should be a file or a database so that the list of players isn't lost when the server restarts.
     
  3. Offline

    Soxra

    Yea i know timers and schedulers.
    My problem:
    New player joins -> player added to player list -> started timer -> after 20 minutes the timer shall remove the player from the player list(don't know how to do it if some players join the server at the same time).

    That's the part i don't knw how to implement.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  4. Code:
    //Create a HashMap to use
    HashMap<String,Integer> protectedPlayers = new HashMap<String,Integer>();
     
    //Adding someone to the list of protected players
    String key = "Player1";
    Integer unixTime = 1360360903;
    protectedPlayers.put(key,unixTime);
     
    //Checking if someone is in the HashMap
    protectedPlayers.containsKey("player1"); //Outputs a boolean true/false
     
    //Getting the value out of the HashMap
    protectedPlayers.get("player1"); //Returns 1360360903
     
    //Check each item in the HashMap
    if(protectedPlayers.size() > 0) {
        Iterator<Entry<String, Integer>> it = protectedPlayers.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, Integer> playerAndTime = it.next();
            playerAndTime.getKey(); //Gives the players name of this entry in the HashMap
            playerAndTime.getValue(); //Gets time stored for this player
        }
    }
    Each section of this code will need to be used in a different place.
    Be smart with each you use each section and this should be the all you need from the HashMap.
    You can change the HashMap to store whatever object you like such as:
    HashMap<String,Date> protectedPlayers = new HashMap<String,Date>();
     
    Soxra and Yukari like this.
  5. Offline

    Yukari

    Iterate through the list, assuming you store the players time in unix time, you would then do

    if ( hashMap.get(player) + 20 * 60 < curtime ) remove from list

    20 * 60 is 1200 seconds (20 minutes)
     
    Soxra and AlexLeporiday like this.
  6. This would work wonderfully with the solution I suggested. However other options are available. :)
     
    Yukari and Soxra like this.
  7. Offline

    Yukari

    Absolutely, this was just a quick post to guide him on the right path for his specific problem with this part. I actually didn't see your post until after I'd posted, your post is more thorough.
     
    Soxra likes this.
  8. Yukari
    Sorry but that is wrong. There are 20 ticks per second.

    So ticks per minute = 20 * 60 seconds = 1200 ticks.
    Ticks per 20 minutes = 1200 ticks/minute * 20 minutes = 24,000 ticks.
     
    Soxra likes this.
  9. Offline

    Yukari

    Read my post again
     
    Soxra likes this.
  10. Offline

    Soxra

    Thanks :)

    AlexLeporiday
    What i have now is not working:
    Code:
        @Override
        public void run() {
     
     
            //Check each item in the HashMap
            if(plugin.protectedPlayers.size() > 0) {
                Iterator<Entry<String, Long>> it = plugin.protectedPlayers.entrySet().iterator();
                while (it.hasNext()) {
                    Entry<String, Long> playerAndTime = it.next();
                    playerAndTime.getKey(); //Gives the players name of this entry in the HashMap
                    playerAndTime.getValue(); //Gets time stored for this player
         
                    if (playerAndTime.getValue() + 2 * 60 > plugin.getServerTime()){
                        playerAndTime.getKey();
                        plugin.protectedPlayers.remove(playerAndTime.getKey());
                        try{
                            Bukkit.getPlayer(playerAndTime.getKey()).sendMessage(ChatColor.GOLD + "Protection removed.");
                        }catch(Exception e){
                 
                        }
                    }
                }
            }
        }
    This void runs every minute.

    Server time is this:
    Code:
          public Long getServerTime() {
                long time = System.currentTimeMillis();
                return time;
              }
    And the protectedPlayers HashMap is now
    Code:
    public HashMap<String, Long> protectedPlayers = new HashMap<String, Long>();
    Join Event:
    Code:
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event){
            if (!event.getPlayer().hasPlayedBefore()){
                Player player = event.getPlayer();
                plugin.protectedPlayers.put(player.getName(), plugin.getServerTime());
                player.sendMessage(ChatColor.GOLD + "Guard ist um " + plugin.next().toString() + " abgelaufen.");
                plugin.check();
            }
     
       
        }
    And finally the check void:
    Code:
        public void check(){
            timer.schedule(new Task(this), 60000, 60000);
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  11. Offline

    Yukari

    You can't use HashMap.remove while iterating over it. Use playerAndTime.remove() instead.
     
    Soxra likes this.
  12. Offline

    Soxra

    there is no playerAndTime.remove()​
     
  13. Offline

    Yukari

    Sorry, I misread your code and thought playerAndTime was the iterator. Do it.remove()
     
    Soxra likes this.
  14. Offline

    Soxra

    Okay got it working without removing them from the HashMap, it,remove() isnt working.
    Edit: sorry, it.remove worked :) Thanks so much
     
Thread Status:
Not open for further replies.

Share This Page