Plugin Help Issue with checking idle time

Discussion in 'Plugin Help/Development/Requests' started by BizarrePlatinum, Feb 21, 2017.

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

    BizarrePlatinum

    I have created an idle checker that is supposed to remove penalties from players that haven't "rested" (I am creating a plugin to add mechanics to minecraft), however it doesn't appear to be working as intended. Any insight would be great.

    Assume everything being called exists (it does).

    Runnable Class (open)

    Code:
    public class RestingTime implements Runnable{
    
        final Server server;
      
        private HashMap<Player, Location> lastLocation = new HashMap<Player, Location>();
        private HashMap<Player, Long> restingTime = new HashMap<Player, Long>();
      
        public RestingTime(Server server) {
            this.server = server;
        }
      
        public void run() {
            for(Player p : server.getOnlinePlayers()) {
                long currentTime = System.currentTimeMillis();
                Location loc = p.getLocation();
                if(lastLocation.containsKey(p)) {
                    Location lastLoc = lastLocation.get(p);
                    //Tests whether the player has moved since the last check
                    if(!(lastLoc.getX() == loc.getX() && lastLoc.getY() == loc.getY() && lastLoc.getZ() == loc.getZ())) {
                        lastLocation.put(p, loc);
                        restingTime.put(p, currentTime);
                    }
                } else {
                    lastLocation.put(p, loc);
                    restingTime.put(p, currentTime);
                }
                //If the player has rested for 60 seconds, all penalties should be removed
                if(getRestingTime(p) >= 60) {
                    BetterMechanics2.distanceTraveled.put(p, (double) 0);
                    BetterMechanics2.tiredness.put(p, 0);
                    p.removePotionEffect(PotionEffectType.SLOW);
                    p.removePotionEffect(PotionEffectType.HUNGER);
                }
            }
          
        }
    
        //Returns a players resting time in seconds
        public int getRestingTime(Player p) {
            Long lastActive = restingTime.get(p);
            if(lastActive == null) {
                return -1;
            }
            return (int)(System.currentTimeMillis() - lastActive) / 1000;
        }
    


    Main Class (open)

    Code:
    public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            taskId = getServer().getScheduler().scheduleSyncDelayedTask(this, rest, 0L);
        }
    
     
  2. Offline

    dNiym

    Have you added any debug code to make sure your methods / runnable is running?

    Likewise you may want to toss in some system.out's to verify that your math / conversions are working as desired.

    I have gotten away from trying to convert ms to seconds back and forth and just use ms.

    Since one second is 1000ms, you could set your resting time to the time which they will have rested for 60 sec and check vs that.

    I.e. When they stop moving and it flags them as resting, do a
    RestingVariable = system.currentTimeMs()+60000;

    Then when it loops around to check them again, if they have moved clear their rest timer variable, if they haven't do
    If(System.currentTimeMs() >= RestingVariable)
    //do whatever


    Sent from my iPhone using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page