Heal others

Discussion in 'Plugin Development' started by DanLT, Aug 27, 2015.

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

    DanLT

    How to heal others, because this code isn't working.

    Code:
    }else if(args.length == 1){
                        Player targetPlayer = Bukkit.getServer().getPlayer(args[0]);
                        if(player.getServer().getPlayer(args[0] !=null)){
                            targetPlayer.setHealth(20.0);
     
    Last edited: Aug 28, 2015
    BlackVoidMike likes this.
  2. Offline

    Xerox262

    player.getServer().getPlayer(args[0] !=null)
    Should be
    targetPlayer != null. Try this, you should show the rest of your code though incase there's something else (atleast a few lines up and down if you don't want to show all your code)

    P.S. This should be in plugin development not requests
     
  3. Online

    timtower Moderator Moderator

    Moved to plugin development.
     
  4. Offline

    DanLT

    Thank you !
     
  5. Offline

    au2001

    @DanLT Some great functions you could also add to your heal command:
    • Reset his food and exhaustion
    • Clear his bad potion effects
    • Extinguish him (if he's on fire)
    • Reset his remaining air
    And if this thread is solved, mark it as filled :)
     
  6. Offline

    The_BloodHound

    Writing some code just to help a little. If this is solved, mark the thread as solved. Btw, the potion effect thing may not work because I didn't test it xP
    Code:
    if (cmd.getName().equalsIgnoreCase("heal")) {
    
                Player player = (Player) sender;
    
                Player target = player.getServer().getPlayer(args[0]);
    
                if (args.length < 0) {
    
    
    
                } else {
    
                    if (target.isOnline() || !target.equals(null)) {
    
                        target.setHealth(20);
    
                        target.setFoodLevel(20);
    
                        Collection<PotionEffect> activePotionEffects = target.getActivePotionEffects();
    
                        if(!activePotionEffects.isEmpty()){
    
                            target.removePotionEffect((PotionEffectType) activePotionEffects);
    
                        }
    
                    }
    
                }
    
            }
    
     
  7. Offline

    mrgreen33gamer

    I suppose this is a way to heal others?

    Code:java
    1.  
    2. if(lbl.equalsIgnoreCase("heal")){
    3. if(sender instanceof Player){
    4. Player player = (Player) sender;
    5. if(args.length == 0){
    6. player.setHealth(20.0D);
    7. player.setFoodLevel(20);
    8. player.sendMessage(ChatColor.GREEN + "You healed yourself!");
    9. }else if(args.length == 1){
    10. Player target = Bukkit.getPlayer(args[0]);
    11. if(!target.isOnline()){
    12. player.sendMessage(ChatColor.RED + "That player is not online.");
    13. return true;
    14. }
    15. target.setHealth(20.0D);
    16. target.setFoodLevel(20);
    17. player.sendMessage(ChatColor.GREEN + "You've successfully healed: " + target.getName());
    18. }else{
    19. player.sendMessage(ChatColor.YELLOW + "Type it like this: /heal or /heal (player)");
    20. }
    21. }else{
    22. sender.sendMessage("Only players can heal others");
    23. }
    24. }
    25.  
     
  8. Offline

    au2001

    @mrgreen33gamer Console and command blocks should be able to heal players (but not themselves).

    @The_BloodHound No, you're potion thing won't work, you can't cast a Collection<PotionEffect> to a PotionEffectType. You have to loop through the collection like this:
    Code:
    for (PotionEffect potion : target.getActivePotionEffects())
        target.removePotionEffect(potion.getType());
    And humm... !target.equals(null)
    That's the dumbest code ever...
    If target was null, "target.equals(null)" would throw a NPE.
    Use (target != null) instead.
     
  9. Offline

    The_BloodHound

    @The_BloodHound No, you're potion thing won't work, you can't cast a Collection<PotionEffect> to a PotionEffectType. You have to loop through the collection like this:
    Code:
    for (PotionEffect potion : target.getActivePotionEffects())
        target.removePotionEffect(potion.getType());
    That's what I thought xD
     
  10. Offline

    xMrPoi

    Oh jeez that snippet of code is kind of a mess :S
     
  11. Offline

    The_BloodHound

    I was about to go to bed when I did that xD
     
Thread Status:
Not open for further replies.

Share This Page