Set Health for Players (Newbee Plug-In Programmer)

Discussion in 'Plugin Development' started by Dark-Panther, Mar 29, 2011.

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

    Dark-Panther

    Hey Guys,
    I just started to programm plugins and reached my limits...
    (I know, it isn't cool, but I'm really new...)

    I want to make a plugin to set the health of players.
    So, I'm this far:

    Code:
    package DarkPantherX.KillThem.DarkPantherX.KillThem;
    
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.plugin.PluginLoader;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.Event;
    
    public class KillThem extends JavaPlugin{
        private static final Logger log = Logger.getLogger("Minecraft");
     
    public void onDisable() {
        System.out.println( pdfFile.getName() + "is disabled!" );
    }
     
    public void onEnable() {
    
        System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + "is enabled!" );
    
    }
    
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        if(sender instanceof Player) {
            Player player = (Player)sender;
                String NameofCommand = cmd.getName().toLowerCase();
    
                    if(NameofCommand.equalsIgnoreCase("sethealth")) {
     
                    }
    
        }
    Now I want to connect the Console input from the command with a player and with a number of health.

    If the command looks like this: /sethealth <playername> <numberbetween0and20>

    I know it has somthing to do with "sethealth()" and "getName()"
    Can you help me? All I know I've learnd by studying other source codes...
     
  2. Offline

    blackvoid

    Just a tip. You can hold ctrl and press the player type and you will see all player functions.
    If your using ecplise ^^

    Code:
    @Override
    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
        String commandName = command.getName().toLowerCase();
        String[] trimmedArgs = args;
    
        if(commandName.equals("health")){
            HealthPlayer(sender, trimmedArgs);
        }
        return false;
    }
    
    public static int clamp(float value, int min, int max) {
        return Math.min(Math.max(Math.round(value), min), max);
    }
    
    private boolean HealthPlayer(CommandSender sender, String[] args){
        boolean auth = false;
        Player player = null;
        String user = "console";
        if (sender instanceof Player){
            player = (Player)sender;
            if (Permissions.Security.permission(player, "health")) auth=true;
            user = player.getName();
        }else{
            auth = true;
        }
        if (auth) {
            if (args.length > 0) {
                String p = args[0];
                Player victim = this.getServer().getPlayer(p);
    
                long Health = 20;
                if(args.length >= 2){
                    Health = clamp(Math.round(Long.parseLong(args[1])),1,20);
                }
                if(victim != null){
                    victim.setHealth((int) Health);
                    log.log(Level.INFO, "[Health] " + user + " healthed player " + victim.getDisplayName() + ".");
                    this.getServer().broadcastMessage("§f[Health] " + "§e" + victim.getDisplayName() + " §fwas healed by §e" + user + "§f!");
                    return true;
                }
            }
            return false;
        }
        return false;
    }
     
Thread Status:
Not open for further replies.

Share This Page