[Closed] PlayedTime

Discussion in 'Spigot Plugin Development' started by Just_Jitse, Jun 7, 2015.

  1. Hello people,
    so I was making a plugin which saves the played time of a specific player and saves it in a config file.
    My problem is that the played time gets saves only the first time and doesn't save the next time + old time.
    Could someone please help me?

    Main.java:
    Code (Text):
    package me.Just_Jitse.Survival;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin{
       
        static Plugin plugin;
       
        @Override
        public void onEnable(){
           
            Main.plugin = this;
           
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(new Time(), this);

                    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
                        public void run(){
                            for(Player player : Bukkit.getServer().getOnlinePlayers()){
                                if(Main.this.getConfig().contains(player.getName())){ // config contains player?
                                    long x = Main.this.getConfig().getLong(player.getName());
                                    Main.this.getConfig().set(player.getName(), x + Time.t1);
                                }else{
                                    Main.this.getConfig().set(player.getName(), Time.t1);
                                }
                               
                            }
                        }
                    }, 20, 20);
            }
       
        @Override
        public void onDisable(){
            saveConfig();
        }

        public static Plugin getMain(){
            return plugin;
        }
    }
     
    Time.java:
    Code (Text):
    package me.Just_Jitse.Survival;

    import java.util.HashMap;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.event.player.PlayerQuitEvent;

    public class Time implements Listener {
       
        public static long t1;

        @EventHandler
        public void onJoin(PlayerJoinEvent e){
           
            Player p = e.getPlayer();
           
            int ping = ((CraftPlayer) p).getHandle().ping;
            p.sendMessage(ChatColor.BLUE + "You ping is " + ping);
        }
       
        HashMap<UUID, Long> time = new HashMap<UUID, Long>();
       
        @EventHandler
        public void onLogin(PlayerLoginEvent event) {
            time.put(event.getPlayer().getUniqueId(), System.currentTimeMillis());
        }
       
        @EventHandler
        public void onLeave(PlayerQuitEvent event) {
            Player player = event.getPlayer();
            long t = System.currentTimeMillis() - time.get(player.getUniqueId());
            Bukkit.broadcastMessage(ChatColor.RED + "" + t);
           
            t1 = t;
           
            }
        }

        //1 sec = 1000 milisec.

     
     
  2. This has 36 views and doesn't need to be bumped. Just be patient. Wait 1 day then bump it.
     
  3. @Just_Jitse why not use the statistic? Anyway, aside your static abuse, all your players will use the same variable to store the time, which obviously doesn't work. Create a getter in your listener class which returns the time played for a specific player (use the start time for the Player you stored in the Map)
     
  4. @DarkSeraphim because the statistic is client depending, if a player reinstalls mc it would also delete de stats for the played time.
     
  5. @mansionwill yeah okay true, but I just wanted a answer srry :s
     
  6. The server keeps track of the statistics itself.
     
  7. clip

    Benefactor

    The statistic is stored server side.

    Edit sniped
     
    • Like Like x 1
  8. It's okay man, I feel you too. Desperate for an answer, but honestly people are viewing the page. You will get an answer on a Saturday. Be Patient.
     
  9. I'll take a look at this.
     
  10. ...................... People have told you what to do. What else do you want?
    Code (Text):
    Dark Said: Create a getter in your listener class which returns the time played for a specific player (use the start time for the Player you stored in the Map)
     
  11. I don't know how to do that :S
     
  12. Learn Java.
     
  13. @mansionwill Great, being lazy ain't gona work.. Okay thx, I'll try what I can do ;)