Development Assistance 'Slap' Command

Discussion in 'Plugin Help/Development/Requests' started by ReeceyBoi81, May 26, 2015.

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

    ReeceyBoi81

    Recently, I began to make a slap command but I come across one line of code and I have no idea on how to fix it, I've tried most possible fixes but they just aren't working.

    Code:
                Player target = getServer().getPlayer(args[0]);
    Thanks for the help.
     
  2. Offline

    SuperSniper

    Bukkit.getPlayer(args[0]);
    it might be deprecated but its still usable.
    I use it on my plugins
     
  3. @ReeceyBoi81 What exactly is the issue? Did you check the arguments before doing that? Please show your full code and any errors you are getting.
     
  4. Offline

    ReeceyBoi81

    @bwfcwalshy
    My code is:
    Code:
    public class SlapCommand implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    
            if (commandLabel.equalsIgnoreCase("slap")) ;
            {
                if(args.length == 0);
                {
                    sender.sendMessage(ChatColor.RED + "ERROR - Please enter a Player Name");
                }
                if(args[0] == null);
                {
                    sender.sendMessage(ChatColor.RED + "ERROR - Could not find that player: " + ChatColor.GRAY + args[0]);
                    return true;
                }
                Player target = Bukkit.getServer();Bukkit.getPlayer(args[0]);
    
                target.setHealth(19);
                sender.sendMessage(ChatColor.GREEN + "You slapped " + target.getName());
                if(target.isDead());
                {
                    sender.sendMessage(ChatColor.RED + "You can not slap a dead player!");
                }
                return true;
            }
            return false;
        }
    
     
  5. Offline

    I Al Istannen

    @ReeceyBoi81 So a slap heals a player? Try using player#damage(double amount) instead. And why do you have
    "Player target = Bukkit.getServer();Bukkit.getPlayer(args[0]);"? That bold text should'n to anything, delete it. Also i would check if the player is dead before slapping him (move the last if higher). And only return false if you have a nice Using message, but it seems you send all the error messages before. And, if you have a "{" after an if, you don't need a ";".
     
  6. Offline

    ReeceyBoi81

    So, I did most of that and I had to use the "Player target = Bukkit.getPlayer(args[0]);" twice.
    Current code:
    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class SlapCommand implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    
            if (commandLabel.equalsIgnoreCase("slap")) ;
            {
                if(args.length == 0);
                {
                    sender.sendMessage(ChatColor.RED + "ERROR - Please enter a Player Name");
                }
    
                if(args[0] == null);
                {
                    sender.sendMessage(ChatColor.RED + "ERROR - Could not find that player: " + ChatColor.GRAY + args[0]);
                    return true;
                }
    
                Player target = Bukkit.getPlayer(args[0]);
                if(target.isDead());
                {
                    sender.sendMessage(ChatColor.RED + "You can not slap a dead player!");
                }
    
                Player target = Bukkit.getPlayer(args[0]);
                target.setHealth(19);
                sender.sendMessage(ChatColor.GREEN + "You slapped " + target.getName());
    
                return true;
            }
            return false;
        }
    
    }
    @I Al Istannen
     
  7. Offline

    I Al Istannen

    @ReeceyBoi81 Remove the ";" after the if statements. If you leave it, the if statement will just ignore the brackets.
    Code:
    if(args[0] == null)
    args[0] can't be null. Either it is there (size > 0) or it isn't there. The player target can be null however, if the player isn't found. Change your "args[0] == null" to "target == null", then this will work. And, either use player.damage(1) or
    Code:
    player.setHealth(player.getHealth() - 1);
    With your current code, you slap a player with 2 hearts. Health gets set to 19 ==> Player nearly fully healed. And you can leave out your second "Player target =", I wonder why that even was allowed, you register the variable twice. You don't need to.

    I would recommend learning some basic java before starting to make plugins.
     
Thread Status:
Not open for further replies.

Share This Page