Healing Plugin

Discussion in 'Plugin Development' started by nicholasntp, Oct 24, 2011.

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

    nicholasntp

    Alright. In the code below, I have had trouble. I can't get it to heal the player specified in the args. SetHealth is underlined with the error message:
    Code:
     The method setHealth(int) is undefined for the type String 
    This is the code for the plugin's main class.


    (SEE POST BELOW)

    Unformatted Code thats not messed up: (but still has the problem)

    Code:java
    1.  
    2.  
    3. package me.nicholasntp.iHeal;
    4.  
    5. import java.io.BufferedReader;
    6. import java.io.IOException;
    7. import java.io.InputStream;
    8. import java.io.InputStreamReader;
    9. import java.util.Scanner;
    10. import java.util.logging.Logger;
    11.  
    12. import org.bukkit.Bukkit;
    13. import org.bukkit.Location;
    14. import org.bukkit.command.Command;
    15. import org.bukkit.command.CommandSender;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.event.Event;
    18. import org.bukkit.plugin.PluginDescriptionFile;
    19. import org.bukkit.plugin.PluginManager;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21.  
    22.  
    23. public class iHeal extends JavaPlugin{
    24. public static iHeal plugin;
    25. public final Logger logger = Logger.getLogger("Minecraft");
    26. public final iHealPlayerListener playerListener = new iHealPlayerListener(this);
    27. public void onDisable() {
    28. PluginDescriptionFile pdffile = this.getDescription();
    29. this.logger.info(pdffile.getName() + "by nicholasntp is now disabled.");
    30. }
    31. public void onEnable() {
    32. PluginManager pm = getServer().getPluginManager();
    33. pm.registerEvent(Event.Type.PLAYER_CHAT, this.playerListener, Event.Priority.Normal, this);
    34. PluginDescriptionFile pdffile = this.getDescription();
    35. this.logger.info(pdffile.getName() + " version " + pdffile.getVersion() + "by nicholasntp is enabled.");
    36. }
    37. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    38. Player player = null;
    39. if (sender instanceof Player) {
    40. player = (Player) sender;
    41. }
    42.  
    43. if (cmd.getName().equalsIgnoreCase("iheal")) {
    44. if (args[0] == "help") {
    45. sender.sendMessage("Usage: /iheal [Player]");
    46. }
    47. String playerToHeal = args[0];
    48. boolean online = plugin.getServer().getPlayer(playerToHeal) != null;
    49. if (online && sender.hasPermission("iheal.heal")) {
    50. sender.sendMessage("§1[iHeal] §cHealing §e" + playerToHeal + "§c.");
    51. playerToHeal.setHealth(20);
    52. } else {
    53. sender.sendMessage("§1[iHeal] §cPlayer §e" + playerToHeal + "§c is not found, or you don't have the permission to heal.");
    54. }
    55. }
    56.  
    57.  
    58.  
    59. return true;
    60.  
    61.  
    62. }
    63. }
    64.  
    65. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  2. You are trying to set the health of playerToHeal which is a String, not a Player.
     
  3. yes, change it to plugin.getServer().getPlayer(playerToHeal).setHealth(20);
     
    r3Fuze likes this.
  4. Offline

    nicholasntp

    Oh.. Okay... I'll try it.

    It works. Thanks so much, @r3Fuze and @tips48 . I have other questions though. Ill have to ask tomorrow (console errors when running command).

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  5. Offline

    ArcheCane

    Did you configurate the commands in the plugin.yml properly? No tabs, etc.
     
  6. Offline

    nicholasntp

    Yeah. Yml syntax good. I'll get my new main class for u when I get to my computer.

    Here is the main class now. Its edited.
    Code:java
    1.  
    Code:java
    1.  
    2. package me.nicholasntp.iHeal;
    3.  
    4. import java.util.logging.Logger;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.Event;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13.  
    14. public class iHeal extends JavaPlugin{
    15. public static iHeal plugin;
    16. public final Logger logger = Logger.getLogger("Minecraft");
    17. public final iHealPlayerListener playerListener = new iHealPlayerListener(this);
    18. public void onDisable() {
    19. PluginDescriptionFile pdffile = this.getDescription();
    20. this.logger.info(pdffile.getName() + "by nicholasntp is now disabled.");
    21. }
    22. public void onEnable() {
    23. PluginManager pm = getServer().getPluginManager();
    24. pm.registerEvent(Event.Type.PLAYER_CHAT, this.playerListener, Event.Priority.Normal, this);
    25. PluginDescriptionFile pdffile = this.getDescription();
    26. this.logger.info(pdffile.getName() + " version " + pdffile.getVersion() + "by nicholasntp is enabled.");
    27. }
    28. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    29. Player player = null;
    30. if (sender instanceof Player) {
    31. player = (Player) sender;
    32. }
    33.  
    34. if (cmd.getName().equalsIgnoreCase("iheal")) {
    35. if (args[0] == "help") {
    36. sender.sendMessage("Usage: /iheal [Player]");
    37. }
    38. String playerToHeal = args[0];
    39. boolean online = plugin.getServer().getPlayer(playerToHeal) != null;
    40. if (online) {
    41. if (sender.hasPermission("iheal.heal")) {
    42. sender.sendMessage("§1[iHeal] §cHealing §e" + playerToHeal + "§c.");
    43. plugin.getServer().getPlayer(playerToHeal).setHealth(20);
    44. plugin.getServer().getPlayer(playerToHeal).sendMessage("§1[iHeal] §cYou have been healed by §e" + sender + "§c.");
    45. }
    46. else {
    47. sender.sendMessage("§1[iHeal] §cYou do not have the required permission §e'iheal.heal'§c.");
    48. }
    49. } else {
    50. sender.sendMessage("§1[iHeal] §cPlayer §e" + playerToHeal + "§c is not found.");
    51. }
    52. }
    53. return true;
    54. }
    55. }
    56.  
    57. [COLOR=rgb(0, 0, 0)][/COLOR]


    And here is the console message I get when trying to run: /iheal nicholasntp

    Code:
    [/COLOR]
    2011-10-24 23:25:59 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'iheal' in plugin iHeal v1.0
    	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:41)
    	at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:163)
    	at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:353)
    	at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:756)
    	at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:721)
    	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:714)
    	at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:33)
    	at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
    	at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
    	at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:464)
    	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:374)
    	at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
    Caused by: java.lang.NullPointerException
    	at me.nicholasntp.iHeal.iHeal.onCommand(iHeal.java:50)
    	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:39)
    	... 12 more
    [COLOR=rgb(0, 0, 0)]
    [/CODE][/COLOR]

    I hate color tags. Hate. Them. Sorry if its confusing. :eek:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  7. Offline

    Father Of Time

    Honestly, when I have a null point exception that I can't seem to find I simply comment out everything, and I mean everything inside the function throwing the exception, then I start by un-commenting a single variable with a console print of its value after its assigned. I run the plugin and if the variable prints what I expect it to I move on by uncommenting the next section of code.

    This may be time consuming and frustrating, but I've never had a null exception that I couldn't track down using this method. I only suggest this because nothing immediately stands out to me as "wrong"...

    Well, I mean I see some issues, like:

    Code:
    #
    Player player = null;
    #
    if (sender instanceof Player) {
    #
    player = (Player) sender;
    #
    }
    You don't have all the code that is dependent on the player variable nested inside this if statement, so if somehow a non player entity used a command (don't think this is possible, but still) it would never assign the player value, but then later in the oncommand function that none assigned player variable will be called.

    I would advise taking a few minutes to look at all of the variable you declare, then reviewing the oncommand function to see what code is dependent on what variables, and which could possible be left null under certain conditions.
     
  8. Offline

    nicholasntp

    Oh My. Alien Language. lol. I think I know what your saying. The console is telling me a variable is null. So it can't run the function. Can you help me look through the code to see what might be wrong?

    EDIT: And also, can you explain whats wrong in the Code snip you put there?
     
  9. Offline

    bergerkiller

    Fixed the coding for a bit to make it more 'logical'.
    Since you re-use the player object returned, there is no need to call it 3-4 times. You can simply do this:
    Code:
    if (cmd.getName().equalsIgnoreCase("iheal")) {
        if (args[0].equalsIgnoreCase("help")) {
            sender.sendMessage("Usage: /iheal [Player]");
        }
        String playerToHeal = args[0];
        Player toHeal = plugin.getServer().getPlayer(playerToHeal);
        String healedBy = "console";
        if (sender instanceof Player) by = ((Player) sender).getName();
        if (toHeal != null) {
            if (sender.hasPermission("iheal.heal")) {
                sender.sendMessage("§1[iHeal] §cHealing §e" + playerToHeal + "§c.");
                toHeal.setHealth(20);
                toHeal.sendMessage("§1[iHeal] §cYou have been healed by §e" + healedBy + "§c.");
            }  else {
                sender.sendMessage("§1[iHeal] §cYou do not have the required permission §e'iheal.heal'§c.");
            }
        } else {
            sender.sendMessage("§1[iHeal] §cPlayer §e" + playerToHeal + "§c is not found.");
        }
    }
    return true;
    I have done the following changes:
     
    nicholasntp likes this.
  10. Offline

    Don Redhorse

    hm... one thing is the part with Player player = null; and than the if instance of player.

    if the sender is not an instance of player, player will stay null

    also if line 50 in the dump is the line 50 in you code above you try to print playertoheal which could be null and playertoheal is a string... do you require a Player?

    also the if / else code it kind weird... if the playertoheal is not online the player will get the message (in theory) that the player isn't online... but you don't check the permissions.

    I think you should rethink the flow of the code, and to a System.out.print(variables) to try to figure out which data you are handling at which point.
     
  11. Offline

    nicholasntp

    Wow. Thank you. That makes more sense. Ill check if I still get errors.

    Okay. Makes sense. I totally get what you are saying. Lemme check to see if I still get errors from what @bergerkiller did.

    if (sender instanceof Player) by = ((Player) sender).getName();
    What is the "by"? Its getting java errors.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  12. Offline

    bergerkiller

    @nicholasntp optimizing it's not a difficult step in programming, but it is an important one. I am actually kinda addicted to optimizing. Whenever I see this:
    Code:
    if (event.getBlock().getType() == Material.WOOD || event.getBlock().getType() == Material.STONE) {
        //code
    
    }
    I almost instantly scream out optimize!
    Code:
    Block b = event.getBlock();
    Material type = b.getType();
    if (type == Material.WOOD || type == Material.STONE) {
        //code
    }
    Guess it's a left-over from coding TrainCarts...had to optimize everything there too :/

    Also, by should be healedBy, I was writing that code in notepad xd
     
  13. Offline

    Don Redhorse

    I hope so... :)

    btw @bergerkiller s code still has some of these issues... like he checks if the args[0] is heal and displays a message but than he goes further and looks for the player to heal which is args[0] again

    but his code looks nice otherwise...
     
  14. Offline

    nicholasntp

    ???

    That looks much nicer with optimizing. And ok. :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  15. Offline

    bergerkiller

    @Don Redhorse it also still lacks the code to check the args length, it could easily be zero. (/iheal)
    That should be declared all above :)

    And I left it like that, could be there is a player called help lol
     
  16. Offline

    nicholasntp

    ugh.. still console error....

    Code:
    
    10:57:58 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'iheal' in plugin iHeal v1.0
    	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:41)
    	at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:163)
    	at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:353)
    	at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:756)
    	at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:721)
    	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:714)
    	at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:33)
    	at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
    	at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
    	at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:464)
    	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:374)
    	at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
    Caused by: java.lang.NullPointerException
    	at me.nicholasntp.iHeal.iHeal.onCommand(iHeal.java:40)
    	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:39)
    	... 12 more
    
    And ingame...

    Code:
    An internal error occurred while attempting to perform this command
    
    if I type /iheal help, then it shows the usage, then gets the internal error occurred error

    EDIT: So that means its trying to also get the player "help" after it shows usage. So I need to END the command process after it shows usage.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  17. Offline

    bergerkiller

    @nicholasntp could you post the full code you have right now? So I can stack-trace the line where the error is...

    And, not really, because then the command doesn't even work xd

    EDIT

    Spotted it. Your 'plugin' variable is null. Either initialize it in onEnable (plugin = this) or use 'Bukkit' instead.
    This is null:
    Code:
    public static iHeal plugin;
    Needs to be set in onEnable:
    Code:
    plugin = this;
    And then this part works as intended:
    Code:
    Player toHeal = plugin.getServer().getPlayer(playerToHeal);
     
  18. Offline

    nicholasntp

    Ohhhh... I forgot to specify plugin (in the onEnable part). XD Im an idiot.
     
  19. Offline

    enjikaka

    If you are intrested; this is how I made my /heal /kill plugin :) It haz a bug (error in consle) when trying to kill or heal offline player xD But anyways :p

    Code:
    package enji.lep.life;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import enji.lep.Msg;
    
    /* Created by enji */
    
    public class Life extends JavaPlugin {
        Logger log = Logger.getLogger("Minecraft");
    
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            log.info("Lightweight Essential Plugin - " + pdfFile.getName() + " Edition " + pdfFile.getVersion() + " enabled.");
        }
    
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            log.info("Lightweight Essential Plugin - " + pdfFile.getName() + " Edition " + pdfFile.getVersion() + " disabled.");
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args) {
            Player player = (Player)sender;
            if (sender instanceof Player) {
                if(command.equalsIgnoreCase("heal")) {
                    if (player.hasPermission("lep.life.heal") || player.hasPermission("lep.all") || player.isOp()) {
                        if (args.length == 1) {
                            Player player2 = Bukkit.getServer().getPlayer(args[0]);
                            if (player2 instanceof OfflinePlayer) {
                                player.sendMessage(ChatColor.RED + player2.getName() + " is not online!");
                            }
                            player2.setHealth(20);
                            player.sendMessage(ChatColor.GREEN + "You gave " + ChatColor.YELLOW + player2.getName() + ChatColor.GREEN + " some extra lives!");
                            player2.sendMessage(ChatColor.YELLOW + player.getName() + ChatColor.GREEN + " gave you your lives back!");
                        }
                        else {
                            player.setHealth(20);
                            player.sendMessage(ChatColor.GREEN + "You are strong once again!");
                        }
                    }
                    else {
                        player.sendMessage(Msg.np);
                        log.info(player.getName() + Msg.tti + command);
                    }
                }
                if(command.equalsIgnoreCase("kill")) {
                    if (args.length == 1) {
                        if (player.hasPermission("lep.life.kill.other") || player.hasPermission("lep.all") || player.isOp()) {
                            Player player2 = Bukkit.getServer().getPlayer(args[0]);
                            if (player2 instanceof OfflinePlayer) {
                                player.sendMessage(ChatColor.RED + player2.getName() + " is not online!");
                            }
                            player2.setFallDistance(50);
                            player.sendMessage(ChatColor.GREEN + "You murdered " + ChatColor.YELLOW + player2.getName());
                            player2.sendMessage(ChatColor.RED + player.getName() + " murdered you!");
                        }
                        else {
                            player.sendMessage(Msg.np);
                            log.info(player.getName() + Msg.tti + command);
                        }
                    }
                    else {
                        if (player.hasPermission("lep.life.kill") || player.hasPermission("lep.all") || player.isOp()) {
                            player.setFallDistance(50);
                            player.sendMessage(ChatColor.RED + "You committed suicide!");
                        }
                        else {
                            player.sendMessage(Msg.np);
                            log.info(player.getName() + Msg.tti + command);
                        }
                    }
                }
            }
            else {
                log.info(Msg.oig);
            }
            return false;
        }
    }
     
  20. Offline

    nicholasntp

    Cool and thanks @enjikaka

    Plugin tested. Releasing soon! :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  21. include a if(sender instanceof Player) to fix the NPE
     
  22. Offline

    Abrupt

    that's not the only thing. he is also repeatedly searching for the associated player object for whatever reason.
     
  23. Offline

    enjikaka

    I have that already?
     
  24. then it shouldn't give a NPE. What is it? :p
     
  25. Offline

    nicholasntp

    I will actually be posting iHeal tomorrow after school. I'm not sick anymore, so I get to go to school. Yay. ;) :/

    It will also have a page on bukkit dev. Even though I dislike bukkit dev sooo much.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  26. Offline

    enjikaka

    Shouldn't this:

    PHP:
    if (player2 instanceof OfflinePlayer) {
                                
    player.sendMessage(ChatColor.RED player2.getName() + " is not online!");
             }
    Stop that?
     
  27. Offline

    ArcheCane

    Aren't there heaps of Healing plugins? :p
     
  28. Offline

    nicholasntp

    Yes, this is just practice.

    instead of if (player2 instanceof OfflinePlayer) {

    use if (plugin.getServer().getPlayer(player2) == false); I think....

    Im totally wrong. Forget what I said. xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  29. Offline

    enjikaka

    Haha lmao
     
    Walker Crouse likes this.
  30. Offline

    nicholasntp

    :p

    Essentials and CommandBook are about to get a rival on 11/11/11

    Not that I have anything against CommandBook.... Its Essentials that I dislike...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
Thread Status:
Not open for further replies.

Share This Page