1.15.2 Played time cast problem

Discussion in 'Spigot Plugin Development' started by riobusor66, Feb 1, 2020.

  1. Hi , I would like the playing time of each player to be saved in a yml file and display it in String format, if I set the variables in Long it tells me that I cannot cast them in Int and if I put them in Int he tells me I can't cast them long.

    the RunBukkit class called every 5 seconds:
    Code (Java):
    public class PlayedTime extends BukkitRunnable {

        private FileConfiguration config;
        private Main main;
       
        public PlayedTime(FileConfiguration config, Main main) {
            this.config = config;
            this.main = main;
        }
       
        @Override
        public void run() {
            for(Player p : Bukkit.getOnlinePlayers()) {
                if(config.get("played-time." + p.getUniqueId()) == null) {
                    config.set("played-time." + p.getUniqueId(), 0);
                    main.saveConfig();
                }
                int time = (int) config.get("played-time." + p.getUniqueId());
                time += 5;
                config.set("played-time." + p.getUniqueId(), time);
            }
           
            main.saveConfig();
        }

    }
    I use this to get the time:
    Code (Text):
    long time = (long) config.get("played-time." + player.getUniqueId());
                    playerHeadLore.add("§4Temps de jeu: §f" + convertToDateFormat(time));
    The convert to string method:
    Code (Text):
    private String convertToDateFormat(long time) {
            long seconds = time;
            long minutes = 0;
            long hours = 0;
            long days = 0;
           
            if(seconds > 60) {
                minutes = (long) seconds / 60;
                seconds = (long) seconds % 60;
               
                if(minutes > 60) {
                    hours = (long) minutes / 60;
                    minutes = (long) minutes % 60;
                   
                    if (hours > 24) {
                        days = (long) hours / 24;
                        hours = hours % 60;
                    }
                }
            }
           
            return Long.toString(days) + " Jours " + Long.toString(hours) + " Heures " + Long.toString(minutes) + " Minutes " + Long.toString(seconds) + " Secondes";
        }
     
  2. You can always cast an int to long and most of the time a long to int, where exactly do you have problems with that and what exactly is your problem?
    Also, this:
    Code (Java):
    if(config.get("played-time." + p.getUniqueId()) == null) {
                    config.set("played-time." + p.getUniqueId(), 0);
                    main.saveConfig();
                }
                int time = (int) config.get("played-time." + p.getUniqueId());
    can be simplified to this:
    Code (Text):
    int time = config.get("played_time" + p.getUniqueId(), 0);
    Also, there is a util-class called DateTimeFormatter that does exactly your conversion
     
  3. Use players statistics.
     
  4. Try to use:
    Code (Java):
    int time = (int) config.getDouble(//.....);
     
  5. There’s a thing called config.getInt btw
     
  6. I have create my own FileConfiguration file and It's solved the problem
     
    • Like Like x 1