Solved Why is .getPlayerTime a global method?

Discussion in 'Spigot Plugin Development' started by Joedon, Jun 17, 2016.

  1. Hi there I am using the following code to get a player's time and divide it into seconds. It works perfectly when there's only 1 player online. When another joins, that player takes the first player's current time and continues to add onto it, therefore making them have the same player time. Any ideas?

    Code (Text):
        public static void playerMinTime(Player s){
            if(!s.isOnline()){
                return;
            }
            String uuid = s.getUniqueId().toString();
            Bukkit.broadcastMessage(s + " | " + uuid + " | " + s.getPlayerTime());
            Data.timeonlinemin.put(uuid, s.getPlayerTime() / (20 * 60));
            UserConfigs cm = new UserConfigs(plugin, s);
            FileConfiguration f = cm.getConfig();
                f.set("playertimemin", Data.timeonlinemin.get(uuid));
                cm.reload();
                cm.saveConfig();
                Bukkit.broadcastMessage(uuid + " | " + Data.timeonlinemin.get(uuid));
                return;
        }
    I am running this method for each player through an onJoin event every 60 seconds
     
  2. Sounds like you're probably setting the same value for every player rather than saving it per-UUID. Can you show the 'Data' class please?
     
  3. This is the hashmap in the data class:
    Code (Text):
    public static Map<String, Long> timeonlinemin = new HashMap<String, Long>();
    Since I'm saving it with a UUID wouldn't a string be fine here?
     
  4. One option would be to use the Scoreboard API
     
  5. I just want a simple format to display it in ticks, seconds, or minutes. I will not use an API as I am sure there is a better way to do this
     
    • Like Like x 1
  6. Okay, so shouldn't this return the player's online time?:
    Code (Text):
        public static void playerMinTime(Player s){
            if(!s.isOnline()){
                return;
            }
            Bukkit.broadcastMessage("" + s.getStatistic(Statistic.PLAY_ONE_TICK));
    }
    It is still counting globally
     
  7. Nevermind I just simply used a manual counter and it's working great. Thanks for the ideas though!
     
  8. Glad you got it, wanted to show what I use for this:
    Code (Text):
        public String getHoursPlayed(Player player) {
            int ticks = player.getStatistic(Statistic.PLAY_ONE_TICK);
            int minutes = ticks / 20 / 60;
            int hours = minutes / 60;
         
            if(hours < 1) {
                return "0." + minutes;
            }
         
            return String.valueOf(hours);

        }
    It's actively used by many players (100+ players), appears to be working fine.
     
    • Like Like x 1