Solved Having trouble with /heal

Discussion in 'Plugin Development' started by random_username, Nov 9, 2013.

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

    random_username

    Hello, I just started using multiple classes, so I think this may be the cause, but i'm not sure. Each time I use /heal, I'm getting an error. This is what the console says when I use /heal: http://pastebin.com/9Y3qgA5e
    This is my main:
    Code:java
    1. package me.kait18.playercommands;
    2.  
    3. import me.kait18.playercommands.commands.clear;
    4. import me.kait18.playercommands.commands.feed;
    5. import me.kait18.playercommands.commands.fly;
    6. import me.kait18.playercommands.commands.gm;
    7. import me.kait18.playercommands.commands.hat;
    8. import me.kait18.playercommands.commands.heal;
    9. import me.kait18.playercommands.commands.invsee;
    10. import me.kait18.playercommands.commands.kill;
    11. import me.kait18.playercommands.commands.killall;
    12. import me.kait18.playercommands.commands.pc;
    13. import me.kait18.playercommands.commands.ptime;
    14. import me.kait18.playercommands.commands.pweather;
    15. import me.kait18.playercommands.commands.speed;
    16. import me.kait18.playercommands.commands.starve;
    17. import me.kait18.playercommands.commands.suicide;
    18. import me.kait18.playercommands.commands.tp;
    19. import me.kait18.playercommands.commands.vanish;
    20. import me.kait18.playercommands.listeners.events;
    21.  
    22. import org.bukkit.Bukkit;
    23. import org.bukkit.plugin.PluginDescriptionFile;
    24. import org.bukkit.plugin.PluginManager;
    25. import org.bukkit.plugin.java.JavaPlugin;
    26.  
    27. public class Main extends JavaPlugin{
    28. public void onEnable() {
    29. PluginManager pm = Bukkit.getServer().getPluginManager();
    30. pm.registerEvents(new events(), this);
    31. getCommand("clear").setExecutor(new clear());
    32. getCommand("feed").setExecutor(new feed());
    33. getCommand("fly").setExecutor(new fly());
    34. getCommand("gamemode").setExecutor(new gm());
    35. getCommand("hat").setExecutor(new hat());
    36. getCommand("heal").setExecutor(new heal());
    37. getCommand("invsee").setExecutor(new invsee());
    38. getCommand("kill").setExecutor(new kill());
    39. getCommand("killall").setExecutor(new killall());
    40. getCommand("playercommands").setExecutor(new pc());
    41. getCommand("playertime").setExecutor(new ptime());
    42. getCommand("playerweather").setExecutor(new pweather());
    43. getCommand("speed").setExecutor(new speed());
    44. getCommand("starve").setExecutor(new starve());
    45. getCommand("suicide").setExecutor(new suicide());
    46. getCommand("teleport").setExecutor(new tp());
    47. getCommand("vanish").setExecutor(new vanish());
    48. PluginDescriptionFile pdfFile = this.getDescription();
    49. this.getLogger().info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been Enabled.");
    50. getConfig().options().copyDefaults(true);
    51. this.saveDefaultConfig();
    52. }
    53.  
    54. @Override
    55. public void onDisable() {
    56. PluginDescriptionFile pdfFile = this.getDescription();
    57. this.getLogger().info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been Disabled.");
    58. }
    59.  
    60.  
    61.  
    62. }
    63.  

    And this is my heal class:
    Code:java
    1. package me.kait18.playercommands.commands;
    2.  
    3. import me.kait18.playercommands.Main;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10.  
    11. public class heal extends Main implements CommandExecutor{
    12.  
    13. @Override
    14. public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    15. String noperm = getConfig().getString("NoPermission").replace("&", "§");
    16. String prefix = getConfig().getString("Prefix").replace("&", "§");
    17. if(cmd.getName().equalsIgnoreCase("heal")){
    18. if(args.length == 0){
    19. if(Sender instanceof Player){
    20. if(Sender.hasPermission("pcommands.heal")){
    21. Player player = (Player) Sender;
    22. player.setHealth(20);
    23. Sender.sendMessage(prefix + "§3Your health has been restored.");
    24. }else{
    25. Sender.sendMessage(prefix + noperm);
    26. }
    27. }
    28. }if(args.length == 1){
    29. if(Sender.hasPermission("pcommands.heal.others")){
    30. Player target = Bukkit.getPlayer(args[0]);
    31. if(target == null){
    32. Sender.sendMessage(prefix + "§cPlayer not found.");
    33. }else{
    34. target.setHealth(20);
    35. target.sendMessage(prefix + "§3Your health has been restored.");
    36. Sender.sendMessage(prefix + "§3" + args[0] + "'s health has been restored.");
    37. }
    38. }else{
    39. Sender.sendMessage(prefix + noperm);
    40. }
    41. }
    42. }
    43. return false;
    44. }
    45. }
    46.  

    Any idea on how to solve it?
     
  2. Offline

    Deleted user

    random_username
    Check the string in the config at line 15. Thats the problem
     
  3. Offline

    random_username

    the string is the same as in there though. It is:
    Code:
    NoPermission: '&3You do not have permission to do this!'
    What can I do about it?

    Any Ideas on what could I do to solve this?

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

    NinjaWAffles

    Your problem is most likely that you're trying to extend the main class. Don't extend the main class, put it into a variable and a constructor. ie:
    Code:Java
    1.  
    2. private Main main;
    3.  
    4. public heal(Main main)
    5. {
    6. this.main = main;
    7. }
    8.  

    then from there, you can use this.main.getConfig().getString("").
     
  5. Offline

    random_username

    It wont work, still wont let me do getConfig(), any other way to be able to do this? Current code now:
    Code:java
    1. package me.kait18.playercommands.commands;
    2.  
    3. import me.kait18.playercommands.Main;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10.  
    11. public class heal implements CommandExecutor{
    12.  
    13. private Main main;
    14.  
    15. public heal(Main main)
    16. {
    17. this.main = main;
    18. }
    19.  
    20.  
    21. @Override
    22. public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    23. String noperm = getConfig().getString("NoPermission").replace("&", "§");
    24. String prefix = getConfig().getString("Prefix").replace("&", "§");
    25. if(cmd.getName().equalsIgnoreCase("heal")){
    26. if(args.length == 0){
    27. if(Sender instanceof Player){
    28. if(Sender.hasPermission("pcommands.heal")){
    29. Player player = (Player) Sender;
    30. player.setHealth(20);
    31. Sender.sendMessage(prefix + "§3Your health has been restored.");
    32. }else{
    33. Sender.sendMessage(prefix + noperm);
    34. }
    35. }
    36. }if(args.length == 1){
    37. if(Sender.hasPermission("pcommands.heal.others")){
    38. Player target = Bukkit.getPlayer(args[0]);
    39. if(target == null){
    40. Sender.sendMessage(prefix + "§cPlayer not found.");
    41. }else{
    42. target.setHealth(20);
    43. target.sendMessage(prefix + "§3Your health has been restored.");
    44. Sender.sendMessage(prefix + "§3" + args[0] + "'s health has been restored.");
    45. }
    46. }else{
    47. Sender.sendMessage(prefix + noperm);
    48. }
    49. }
    50. }
    51. return false;
    52. }
    53. }
    54.  

    Btw, thanks for the reply :)
     
  6. Offline

    LegoPal92

  7. Offline

    random_username

    That works, but now my main says that "The constructor heal() is undefined", in line 36. my Main:
    Code:java
    1. package me.kait18.playercommands;
    2.  
    3. import me.kait18.playercommands.commands.clear;
    4. import me.kait18.playercommands.commands.feed;
    5. import me.kait18.playercommands.commands.fly;
    6. import me.kait18.playercommands.commands.gm;
    7. import me.kait18.playercommands.commands.hat;
    8. import me.kait18.playercommands.commands.heal;
    9. import me.kait18.playercommands.commands.invsee;
    10. import me.kait18.playercommands.commands.kill;
    11. import me.kait18.playercommands.commands.killall;
    12. import me.kait18.playercommands.commands.pc;
    13. import me.kait18.playercommands.commands.ptime;
    14. import me.kait18.playercommands.commands.pweather;
    15. import me.kait18.playercommands.commands.speed;
    16. import me.kait18.playercommands.commands.starve;
    17. import me.kait18.playercommands.commands.suicide;
    18. import me.kait18.playercommands.commands.tp;
    19. import me.kait18.playercommands.commands.vanish;
    20. import me.kait18.playercommands.listeners.events;
    21.  
    22. import org.bukkit.Bukkit;
    23. import org.bukkit.plugin.PluginDescriptionFile;
    24. import org.bukkit.plugin.PluginManager;
    25. import org.bukkit.plugin.java.JavaPlugin;
    26.  
    27. public class Main extends JavaPlugin{
    28. public void onEnable() {
    29. PluginManager pm = Bukkit.getServer().getPluginManager();
    30. pm.registerEvents(new events(), this);
    31. getCommand("clear").setExecutor(new clear());
    32. getCommand("feed").setExecutor(new feed());
    33. getCommand("fly").setExecutor(new fly());
    34. getCommand("gamemode").setExecutor(new gm());
    35. getCommand("hat").setExecutor(new hat());
    36. getCommand("heal").setExecutor(new heal());
    37. getCommand("invsee").setExecutor(new invsee());
    38. getCommand("kill").setExecutor(new kill());
    39. getCommand("killall").setExecutor(new killall());
    40. getCommand("playercommands").setExecutor(new pc());
    41. getCommand("playertime").setExecutor(new ptime());
    42. getCommand("playerweather").setExecutor(new pweather());
    43. getCommand("speed").setExecutor(new speed());
    44. getCommand("starve").setExecutor(new starve());
    45. getCommand("suicide").setExecutor(new suicide());
    46. getCommand("teleport").setExecutor(new tp());
    47. getCommand("vanish").setExecutor(new vanish());
    48. PluginDescriptionFile pdfFile = this.getDescription();
    49. this.getLogger().info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been Enabled.");
    50. getConfig().options().copyDefaults(true);
    51. this.saveDefaultConfig();
    52. }
    53.  
    54. @Override
    55. public void onDisable() {
    56. PluginDescriptionFile pdfFile = this.getDescription();
    57. this.getLogger().info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been Disabled.");
    58. }
    59.  
    60.  
    61.  
    62. }
    63.  

    Btw, thanks for helping ;)
     
  8. Offline

    NinjaWAffles

    You just want to change
    Code:Java
    1. getCommand("heal").setExecutor(new heal());

    to
    Code:Java
    1. getCommand("heal").setExecutor(new heal(this));


    Also, by the way you're grasping information and this thread, it doesn't seem you know much about Java. I'd suggest you learn a little bit more about Java then get into the Bukkit API. It's a lot easier that way.
     
  9. Offline

    random_username

    Okay, thanks for the help! :)
     
  10. Offline

    NinjaWAffles

    You're welcome. If there is no further discussion on this topic, would be so kind as to mark this as "SOLVED" so people don't read all of this and realize it's already finished? Thank you! :D
     
Thread Status:
Not open for further replies.

Share This Page