Check if a player is running

Discussion in 'Spigot Plugin Development' started by xUbalubex, May 31, 2018.

  1. Okay so I am making a Stamina and Thirst, and I want to check if the player is running, if so, then decrease the stamina. I would also like the same with jumping

    Code (Java):
    package sm.uba;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin
    {

        boolean ThirstEnabled;
        boolean StaminaEnabled;
       
        boolean MessagesEnabled;
       
        int ThirstTime;
       
        int StaminaRemove;
        int ThirstRemove;
       
        int Thirst;
        int Stamina;
       
        int ThirstDamage;
       

       
        public void onEnable(final Player p)
        {
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
           
            ThirstDamage = this.getConfig().getInt("ThirstDamage");
            ThirstTime = this.getConfig().getInt("ThirstTime");
           
            //Thirst Bar
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run()
                {
                    if(ThirstEnabled = false) {
                        return;
                    } else {
                        Thirst = Thirst - ThirstRemove;
                    }
                   
                    if(Thirst <= 0)
                    {
                        Thirst = 0;
                       
                        double Health = p.getHealth();
                       
                        Health = Health - ThirstDamage;
                    }
                }
            }, 20, ThirstTime);
        }
       
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            //---------Stat Messages---------
            if(cmd.getName().equalsIgnoreCase("Messages")) {
                Player p = (Player) sender;
                if(args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Error! Please define a state!");
                    p.sendMessage(ChatColor.GREEN + "/messages on : /messages off");
                    return true;
                }
                if(args[0].equalsIgnoreCase("on"))
                {
                    if(MessagesEnabled = true) {
                        p.sendMessage(ChatColor.RED + "This feature is already enabled!");
                        return true;
                    } else {
                    MessagesEnabled = true;
                    p.sendMessage(ChatColor.GREEN + "Status Messages Enabled!");
                    return true;
                    }
                }
               
                if(args[0].equalsIgnoreCase("off"))
                {
                    if(MessagesEnabled = false) {
                        p.sendMessage(ChatColor.RED + "This feature is already disabled!");
                        return true;
                    } else {
                    MessagesEnabled = false;
                    p.sendMessage(ChatColor.RED + "Status Messages Disabled!");
                    return true;
                    }
                }
            }
            //---------Thirst---------
            if(cmd.getName().equalsIgnoreCase("Thirst"))
            {
                Player p = (Player) sender;
                if(args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Error! Please define a state!");
                    p.sendMessage(ChatColor.GREEN + "/thirst enabled : /thirst disabled");
                    return true;
                }
                if(args[0].equalsIgnoreCase("enabled"))
                {
                    if(ThirstEnabled = true) {
                        p.sendMessage(ChatColor.RED + "This feature is already enabled!");
                        return true;
                    } else {
                    ThirstEnabled = true;
                    p.sendMessage(ChatColor.GREEN + "Thirst Enabled!");
                    return true;
                    }
                }
               
                if(args[0].equalsIgnoreCase("disabled"))
                {
                    if(ThirstEnabled = false) {
                        p.sendMessage(ChatColor.RED + "This feature is already disabled!");
                        return true;
                    } else {
                    ThirstEnabled = false;
                    p.sendMessage(ChatColor.RED + "Thirst Disabled!");
                    return true;
                    }
                }
            }
            //---------Stamina---------
            if(cmd.getName().equalsIgnoreCase("Stamina"))
            {
                Player p = (Player) sender;
                if(args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Error! Please define a state!");
                    p.sendMessage(ChatColor.GREEN + "/stamina enabled : /stamina disabled");
                    return true;
                }
                if(args[0].equalsIgnoreCase("enabled"))
                {
                    if(StaminaEnabled = true) {
                        p.sendMessage(ChatColor.RED + "This feature is already enabled!");
                        return true;
                    } else {
                        StaminaEnabled = true;
                    p.sendMessage(ChatColor.GREEN + "Stamina Enabled!");
                    return true;
                    }
                }
               
                if(args[0].equalsIgnoreCase("disabled"))
                {
                    if(StaminaEnabled = false) {
                        p.sendMessage(ChatColor.RED + "This feature is already disabled!");
                        return true;
                    } else {
                    StaminaEnabled = false;
                    p.sendMessage(ChatColor.RED + "Stamina Disabled!");
                    return true;
                    }
                }
            }
            //---------Status---------
            if(cmd.getName().equalsIgnoreCase("Stats"))
            {
                Player p = (Player) sender;
                double hp = p.getHealth();
               
                if(this.getConfig().getBoolean("StatsSymbols") == false) {
                   
                    p.sendMessage(ChatColor.GREEN + "Stats");
                    p.sendMessage(ChatColor.RED + "Health " + hp);
                    p.sendMessage(ChatColor.GOLD + "Stamina " + Stamina);
                    p.sendMessage(ChatColor.AQUA + "Thirs " + Thirst);
                   
                    return true;
                   
                } else if(this.getConfig().getBoolean("StatsSymbols") == true) {
                   
                    p.sendMessage(ChatColor.GREEN + "Stats");
                    p.sendMessage(ChatColor.RED + "❤ " + hp);
                    p.sendMessage(ChatColor.GOLD + "⚡ " + Stamina);
                    p.sendMessage(ChatColor.AQUA + "☕ " + Thirst);
                   
                    return true;
                }
               

            }
            return false;
           
        }
       
        public void Die(PlayerDeathEvent e) {
           
            Stamina = 100;
            Thirst = 100;
        }
       
        public void Stamina(/*Help Needed Here*/)
        {
           
        }
               
    }
     
    Also, if you catch any errors, it would be nice to report them! I am experimenting with Integers and Booleans.
     
  2. Player#isSprinting(); is the method you want.
     
  3. Is that all I need?

    That doesn't seem like it is all
     
  4. Could also just use Player#isOnGround(); bit hacky and will also take effect if the player's falling but ech.
    There's also probably a packet solution for jumping.
     
  5. Sorry I do not understand. So I put this in and I get an error the blank field Jump has not been initialized. I think I followed your answer too literally. And also, how do I check if they actually jump?
     
  6. Player#getStatistic(Statistic.JUMP);

    Edit: You check by seeing if that's larger than it was before.
     
  7. Wait sorry I am pretty new to coding. So Player#isOnGround(); is a method if I am correct. But what would I do in the method? I haven't really used methods that much.
     
    • Funny Funny x 1
  8. Highly suggest reading up on what these things actually are. Player#isOnGround(); is a method in the Player class, which returns a boolean (true/false) value of whether or not they are standing on the ground or not. So "boolean grounded = player.isOnGround();" would make grounded either true or false, depending on whether whoever "player" is, is on the ground or not. Or- "if(player.isOnGround){/* code here */}" will only execute if the player is on the ground.
     
  9. If you're using Paper you could use the PlayerJumpEvent. This event doesn't exist in CraftBukkit or Spigot, though.

    This is an event that Paper added to the server at the exact spot the jump stat is incremented. That way you wont have to keep checking the jump stat count on a repeating task to detect jumps.

    Also, for clarification: Yes, Player#isSprinting() can be called at any time to check if the player is running or not. But if you are wanting to detect the precise moment the player starts/ends running then you will want to use the PlayerToggleSprintEvent.
     
    #10 BillyGalbreath, May 31, 2018
    Last edited: May 31, 2018
  10. Okay so I was looking, and I found a little easier way. I made a loop. and I did
    if(p.isSprinting()) {
    Stamina = Stamina = StaminaRemove;