Giving a player something if they have been online for x

Discussion in 'Plugin Development' started by Jack3885, Jan 13, 2015.

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

    Jack3885

    Hi im devoloping a plugin for my server and would like to log when a player joins, then if they are still online an hour later award them with a 'point'. Code I have so for(The config part)
    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerLogin(PlayerLoginEvent event){
    4. FileConfiguration config = null;
    5. int DPoints;
    6. DPoints = 1;
    7. File file = new File("plugins"+File.separator+"RankSystem"+File.separator+"users"+File.separator+event.getPlayer().getName()+".yml");
    8. if(!file.exists()){
    9. System.out.println("File Created: "+ event.getPlayer().getName() + ".yml");
    10. try {
    11. file.createNewFile();
    12. } catch (IOException e) {
    13. e.printStackTrace();
    14. }
    15. config = YamlConfiguration.loadConfiguration(file);
    16. config.set("Playtime", DPoints);
    17. try {
    18. config.save(file);
    19. } catch (IOException e) {
    20. e.printStackTrace();
    21. }
    22. }
    23. }
    24. [syntax][/syntax]
     
  2. Offline

    Skionz

    @Jack3885 Save the current time in milliseconds with the player in a Map and every five minutes or so check if the current time is 3600000 higher then the one saved in the Map. Make sure to clean the Map up when necessary.
     
  3. Offline

    Jack3885

    @Skionz Could you give me an short example of this, as im still learning Java really and this is only my third pluign?

    Thanks for the fast reply :)
     
  4. Offline

    Neilnet

    Declare a field, playersOnline;
    Code:
    HashMap<Player, Integer> playersOnline = new HashMap<Player, Integer>
    
    Create le BukkitRunnable

    Code:
    new BukkitRunnable() {
        @Override
       public void run() {
           for (Player p : Bukkit.getOnlinePlayers()) {
                playersOnline.put(p, playersOnline.getValue(p)++);
           }
       }
    
    }.runTaskTimer(this, 0L, (20*60)*5L);
    
    This one basically just adds players online to a big HashMap, every 5 mins it increases the integer inside. My logic is, if that integer gets to 12 (since 12 * 5 = 60 THUS one hour). Then give that player some shit.

    See? None of that system.millisecond stuff :p

    Oh yeah, and when the player joins (PlayerJoinEvent) you want to put him in that hashmap
    playersOnline.put(player, 0);

    and when he leaves, get rid of him :) But first you may wanna learn some hashmaps if you don't know what they are and also look up Bukkit tasks and scheduler (basically you can repeat code every x amount of seconds).
     
  5. @Neilnet What you've shown and explained so for does nothing other than throw some NullPointerExceptions ;)
     
  6. Offline

    Neilnet

    Heh, that's right. I'm really lazy. But I wanted to help.
     
    AdamQpzm likes this.
  7. Offline

    Jack3885

  8. Offline

    _Filip

  9. Offline

    Jack3885

    @_Filip
    He said that, the code posted gives an error
     
  10. @Jack3885 Try to create a system such that @Skionz described - add a player to a map with the time they join, removed them when they leave, check every 5 minutes or so if they've been online for the hour.
     
  11. Offline

    TehHypnoz

    Modified the code by @Neilnet, this should work (haven't tested it!):

    Code:
    HashMap<String, Integer> playersOnline = new HashMap<String, Integer>();
    
    new BukkitRunnable(){
    @Override
       public void run() {
          for (Player p : Bukkit.getOnlinePlayers()) {
          if(!playersOnline.contains(p.getName())) playersOnline.put(p, 0);
          playersOnline.put(p.getName(), playersOnline.getValue(p)++);
          if(playersOnline.get(p.getName()) > [insert amount here]){
             // Do stuff.
          }
          }
        }
    }.runTaskTimer(this, 0L, (20*60)*5L);
    
    
     
  12. Offline

    Jack3885

    @TehHypnoz
    Sorry for noob question, but what is > insert amount here ?
    What do I need there
     
  13. Offline

    Skionz

  14. Offline

    Jack3885

    @TehHypnoz @Skionz
    The amount of what, players, time...
    Also Eclipse is saying insert } to complete method body, although if I do add } it ends my class?
     
    Last edited: Jan 14, 2015
  15. Offline

    unrealdesign

    You either deleted to many brackets or didn't paste enough.
     
  16. Offline

    Neilnet

    I told you to watch a tut on Hashmaps, basically you're getting that integer that increments every 5 mins, and you're checking to see if it's GREATER THAN (>) than X (so 12 would be 12 * 5 (amt interval you check) which is 60 mins so an hour)
     
    Jack3885 likes this.
  17. Offline

    Skionz

    Understanding basic Java syntax is an obvious prerequisite to Bukkit.
     
  18. Offline

    Jack3885

    @TehHypnoz playersOnline.getValue(p)++); Invalid argument to operation ++/--
     
  19. Offline

    SuperOriginal

    @Jack3885 Use the get() method instead of getValue().
     
  20. Offline

    Jack3885

    @SuperOriginal Same error

    bump..

    @TehHypnoz
    How can I fix
    playersOnline.put(p.getName(), playersOnline.getValue(p)++);
    Invalid argument to operation ++/-- ? I have tried .get

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

    TehHypnoz

    Can't test right now but try this:

    Code:
    playersOnline.put(p.getName(), playersOnline.getValue(p) + 1);
    or

    Code:
    Integer value = playersOnline.get(p.getName());
    value++;
    playersOnline.put(p.getName(), value);
    
     
  22. Offline

    InfamousSheep

    You can save the System.currentTimeMillis() when the player first joins the server, then from then on check when they join the server if the System.currentTimeMillis() minus the saved time is more than or equal to X amount.
     
Thread Status:
Not open for further replies.

Share This Page