[Need Help] Heal Plugin

Discussion in 'Plugin Development' started by Subroowka, Aug 11, 2017 at 1:51 PM.

  1. Subroowka

    Subroowka New Member

    Friday
    11
    0
    1
    Can one of you explain to me how to make a Nukkit Heal plugin?

    A player should be able to heal themselves ( /heal) or other players ( /heal [Player] ) !

    Thanks for help :)
     
  2. Subroowka

    Subroowka New Member

    Friday
    11
    0
    1
    #3 Subroowka, Aug 12, 2017 at 2:31 PM
    Last edited: Aug 13, 2017 at 2:29 PM
  3. Eater

    Eater New Member

    Monday
    18
    1
    3
    It's very simple. You could do something like this:

    Code:
    if(player.getHealth() + heal_amount <= 20){
       player.setHealth(player.getHealth() + heal_amount);
    }else {
       player.setHealth(20);
    }
    This is just a snippet. I assume you already know how to set up a plugin with commands.
     
  4. Subroowka

    Subroowka New Member

    Friday
    11
    0
    1
    Sry, but I don't know
     
  5. Eater

    Eater New Member

    Monday
    18
    1
    3
    Maybe you should learn how to make a basic hello world plugin first.
     
  6. Subroowka

    Subroowka New Member

    Friday
    11
    0
    1
    So, of course, I'm not , I can Java, but can you tell me how to do Nukkit Commands?
     
  7. Eater

    Eater New Member

    Monday
    18
    1
    3
    Try this:

    Code:
    public class Healer extends PluginBase{
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
          if(cmd.getName().equals("heal")){
             Player p = (Player) sender;
             if(p.getHealth() < 20){
                p.setHealth(p.getHealth() + 3);
             }else{
                p.setHealth(20);
             }
          }
       }
    }
     
  8. Subroowka

    Subroowka New Member

    Friday
    11
    0
    1
    #9 Subroowka, Aug 14, 2017 at 7:25 PM
    Last edited: Aug 14, 2017 at 8:06 PM
    T
    Thank you a lot , can you please explain to me how I can heal other players?
     
  9. Eater

    Eater New Member

    Monday
    18
    1
    3
    You can do this to heal other players:

    Code:
    public class Healer extends PluginBase{
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
          if(cmd.getName().equals("heal")){
             String playername = args[0]; //0 is the first argument, then 1, then 2, and so on.
             Player p = getServer().getPlayer(playername);
           
             if(p.getHealth() < 20){
                p.setHealth(p.getHealth() + 3);
             }else {
                p.setHealth(20);
             }
          }
       }
    }
    For example: /heal <your username>
    However, typing /heal with no arguments will not work.
     
  10. Subroowka

    Subroowka New Member

    Friday
    11
    0
    1
    Thanks a lot for your Help
     
  11. Eater

    Eater New Member

    Monday
    18
    1
    3
    Oh, I forgot one thing. You need to return true at the end of onCommand. In the last line of onCommand, add "return true;".
     
  12. Subroowka

    Subroowka New Member

    Friday
    11
    0
    1
    ok Thanks a lot
     
  13. Eater

    Eater New Member

    Monday
    18
    1
    3
    Here, why don't you use my healer plugin . It works just the same. https://github.com/PixelPlugins/iores/raw/master/healer.jar

    Source:

    Code:
    package healer;
    
    import cn.nukkit.Player;
    import cn.nukkit.command.Command;
    import cn.nukkit.command.CommandSender;
    import cn.nukkit.plugin.PluginBase;
    
    /**
     * Created by Walker on 8/14/2017.
     */
    public class Healer extends PluginBase{
        @Override
        public void onEnable(){
            getLogger().info("Healer plugin has been loaded.");
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equals("heal")){
                Player p = null;
    
                if(args.length > 0){
                    p = getServer().getPlayer(args[0]);
                }else {
                    p = (Player) sender;
                }
    
                if(p.getHealth() + 3 < 20){
                    p.setHealth(p.getHealth() + 3);
                }else {
                    p.setHealth(20);
                }
    
                p.sendMessage("You were healed!");
            }
    
            return true;
        }
    }
    
    And here's plugin.yml:

    Code:
    #name, main, version and api are required
    name: Healer
    main: healer.Healer
    #remember version and api is string, don't write it like this: 1.0.0, or there will be an exception
    version: "1.0.0"
    api: ["1.0.0"]
    
    load: POSTWORLD
    author: Nukkit Project
    description: Heals players
    website: http://worldofecks.ddns.net
    commands:
      example:
        description: Heal command
        usage: "/heal"
        permission: exampleplugin.command.example
    permissions:
      exampleplugin.command.example:
        description: "Allows the user to run the example command"
        default: true
     
  14. Subroowka

    Subroowka New Member

    Friday
    11
    0
    1
    I do not want to use it, I want to learn nukkit, thank you :)