Solved Percentage strange behaviour.

Discussion in 'Plugin Development' started by GeekyCompz, Jul 7, 2014.

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

    GeekyCompz

    Hey, this is my code and for some reason, when there's 2 people on the server and one sleeps it says 0.0% and not 50.0%. If there is one person on the server and he sleeps it says there is 0.0% sleeping. Strange maths right there. Also how can I change it to 0 decimal places. So only 33% or 50%?
    Here's my code:
    Code:
    @EventHandler
        public void onEnterBed(PlayerBedEnterEvent e){
     
            int sleeping = 0;
            int total = plugin.getServer().getOnlinePlayers().length;
            Player p = e.getPlayer();
           
            for (Player player : plugin.getServer().getOnlinePlayers()) {
                if (player.isSleeping()) sleeping++;
            }
           
            float percentage = (float)((sleeping*100)/total);
           
            plugin.getServer().broadcastMessage("§6[§eSleep§6] §a§l" + p.getName() + " §ais now sleeping. §3(" + percentage + "%)");
           
            if (percentage >= 50) {
                plugin.getServer().broadcastMessage("§6[§eSleep§6] §7The sun rises from the horizon. A new day is here.");
                for (World world : plugin.getServer().getWorlds()) {
                    if (world.getEnvironment() == Environment.NORMAL) {
                        world.setTime((long)0);
                    }
                }
            }
           
        }
       
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onLeaveBed(PlayerBedLeaveEvent e) {
            Player p = e.getPlayer();
           
            if (!(p.getWorld().getTime() < (long)200)) {
               
                int sleeping = 0;
                int total = plugin.getServer().getOnlinePlayers().length;
               
                for (Player player : plugin.getServer().getOnlinePlayers()) {
                    if (player.isSleeping()) sleeping++;
                }
               
                float percentage = (float)((sleeping*100)/total);
               
               
                plugin.getServer().broadcastMessage("§6[§eSleep§6] §a§l" + p.getName() + " §ais no longer sleeping. §3(" + percentage + "%)");
            }
        }
    Anyone?

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

    mythbusterma

    It has to do with the way Java does arithmetic, I have had this same problem before. It has to do with

    "float percentage = (float)((sleeping*100)/total);"

    You should first cast sleeping and total to floats also.
     
  3. Offline

    GeekyCompz

    mythbusterma It still doesn't work.

    Code:
    float sleeping = 0;
            float total = plugin.getServer().getOnlinePlayers().length;
            Player p = e.getPlayer();
           
            for (Player player : plugin.getServer().getOnlinePlayers()) {
                if (player.isSleeping()) sleeping++;
            }
           
            float percentage = (float)((sleeping*100)/total);
     
  4. Offline

    Waffles87

    I tried this out and it looks like the isSleeping() method isn't returning true for players in bed; maybe it's referring to something else? The math is working, it's just that it always thinks no one is in bed, so it's giving you 0 every time. I guess it's possible that isSleeping() is broken, but the API describes it as "Returns whether this player is slumbering" so maybe "slumbering" has a different meaning. If so, you'll need to keep track of sleeping players manually, probably most easily with an ArrayList; add the player to it when they get in bed, remove them when they get out.

    As for the decimal point, that's there because you did some integer math then cast it into a float. If you want no decimal you can either do float math and round to int, or just do integer math and be a little bit inaccurate because integer division truncates any decimals.

    float math, rounded to int (I think it's Math.round()):
    int percentage = Math.round((float)sleeping / (float)total * 100);

    inaccurate int math:
    int percentage = (sleeping * 100) / total;
     
  5. Offline

    GeekyCompz

    Waffles87 It seems that the isSleeping() is working. As I stated that if there is two players in bed, it says there are 50% in bed. Even though there is only 2 people on the server should it should be 100%.
     
  6. Offline

    Waffles87


    The way I read your first post was that it said 0.0% and not 50% when there were two people total and one sleeping. That was a bit misleading- are you sure you are seeing 50% in that case?
     
  7. Offline

    GeekyCompz

    Waffles87 mythbusterma I have got it to work. Basically the sleeping players had to start at 1 and not 0. It was giving it false information as to 1 less player sleeping every time.

    Code:
    float sleeping = 1;
            float total = plugin.getServer().getOnlinePlayers().length;
            Player p = e.getPlayer();
           
            for (Player player : plugin.getServer().getOnlinePlayers()) {
                if (player.isSleeping()) sleeping++;
            }
           
            int percentage = Math.round((float)sleeping / (float)total * 100);
     
  8. Offline

    Waffles87

    Ok cool :)
     
Thread Status:
Not open for further replies.

Share This Page