Command not registering

Discussion in 'Plugin Development' started by Skify, Sep 4, 2015.

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

    Skify

    Hi,
    The command does not work, when I execute it, it says nothing, not even the unknown command message. I added this command in my plugin.yml correctly as well.
    And 1 more thing I need help with, on the place and break event, exclude flint and steel/fire.

    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String label,
    3. String[] args) {
    4. System.out.println("1");
    5. System.out.println("2");
    6. System.out.println("3");
    7. if ((sender instanceof Player)) {
    8. Player p = (Player) sender;
    9. if (cmd.getName().equalsIgnoreCase("togglebuild")) {
    10. if (p.hasPermission("shumaapi.build")) {
    11. if (!getConfig().getBoolean(p.getName() + " build")) {
    12. p.sendMessage(prefix + "You cannot build now.");
    13. getConfig().set(p.getName() + " build",
    14. Boolean.valueOf(true));
    15. saveConfig();
    16. } else {
    17. p.sendMessage(prefix + "You can build now.");
    18. getConfig().set(p.getName() + " build",
    19. Boolean.valueOf(false));
    20. saveConfig();
    21. }
    22. } else {
    23. p.sendMessage(prefix + "You do not have permission!");
    24. }
    25. }
    26. }
    27. return false;
    28. }
    29.  
    30. @EventHandler
    31. public void onLogin(final PlayerLoginEvent event) {
    32. if (event.getPlayer().isOp())
    33. Bukkit.getServer().getScheduler()
    34. .runTaskLater(this, new Runnable() {
    35. public void run() {
    36. event.getPlayer().sendMessage(
    37. prefix + "Build toggle has been disabled.");
    38. build.this.getConfig().set(
    39. event.getPlayer().getName() + " build",
    40. Boolean.valueOf(false));
    41. build.this.saveConfig();
    42. }
    43. }, 40L);
    44. }
    45.  
    46. @EventHandler
    47. public void breakBlockEvent(BlockBreakEvent event) {
    48. if (getConfig().getBoolean(event.getPlayer().getName() + " build"))
    49. event.setCancelled(true);
    50. }
    51.  
    52. @EventHandler
    53. public void placeBlockEvent(BlockPlaceEvent event) {
    54. if (getConfig().getBoolean(event.getPlayer().getName() + " build"))
    55. event.setCancelled(true);
    56. }
    57. }
     
    Last edited: Sep 4, 2015
  2. Offline

    mythbusterma

    @Skify

    Did you set this as the command executor?
     
  3. Offline

    Skify

    @mythbusterma
    Ohh, I just did, but I don't know why I'm getting this error.
    Cannot instantiate the type build

    Code:java
    1.  
    2. getCommand("togglebuild").setExecutor(new build());
     
  4. Offline

    DuaneTheDev_

    @Skify

    You silly goose. You're setting the config wrong.

    Replace:
    Code:
    getConfig().set(p.getName() + " build",
                                    Boolean.valueOf(true));
    With:
    Code:
    getConfig().set(p.getName() + ".build", Boolean.TRUE;
    -And for false do Boolean.FALSE

    You may need to change the following but it should be somewhat similar.
    Code:
    if(cmd.getName().equalsIgnoreCase("togglebuild")) {
               if(p.hasPermission("shumaapi.build")) {
                   if(getConfig().getBoolean(p.getName() + ".build")) {
                       p.sendMessage(prefix + "Building privilege: Enabled");
                       getConfig().set(p.getName() + ".build", Boolean.FALSE);
                       saveConfig();
                   } else {
                       p.sendMessage(prefix + "Building privilege: Disabled");
                       getConfig().set(p.getName() + ".build", Boolean.TRUE);
                       saveConfig();
                   }
               } else {
                   p.sendMessage(prefix + "You do not have permission to use /togglebuild");
               }
    You need to also fix login, place and break events.


    EDIT: I didn't test the code, so let me know if there's a problem.

    EDIT: Fixed Boolean mistake.
     
    Last edited: Sep 5, 2015
  5. Offline

    Skify

    I'm still getting the Cannot instantiate the type build error.
     
  6. Offline

    mythbusterma

    @Skify

    You're gonna wanna fix all that as well. The reason is because either "build" is not a class, or does not have a publicly visible default constructor.
     
  7. Offline

    mine-care

    What the OP had was correct... the only change was the dot ('.') before the section "build", Comparing two booleans to end in a third boolean isnt logical...

    @Skify Please follow Java Naming Convention for classes.
    Can i see how the class build is declared? I mean, the top part where you declare the name, and modifiers
     
    DuaneTheDev_ likes this.
  8. Offline

    DuaneTheDev_

    @Skify

    Inside build class.
    Code:
    public class build implements CommandExecutor, Listener {

    Inside main class, under OnEnable
    Code:
    getServer().getPluginManager().registerEvents(new build(), this);
    this.getCommand("togglebuild").setExecutor(new build());
     
    Last edited: Sep 5, 2015
  9. Offline

    bwfcwalshy Retired Staff

    @DuaneTheDev_ Follow naming conventions, and fix your booleans. true/false are booleans, no need to do Boolean.valueOf(true/false);
     
    DuaneTheDev_ likes this.
  10. Offline

    DuaneTheDev_

    Last edited: Sep 5, 2015
  11. Offline

    Skify

    @DuaneTheDev_

    The 'build' always has an error saying:
    Cannot instantiate the type build

    Code:java
    1.  
    2. getServer().getPluginManager().registerEvents(new build(), this);
    3. this.getCommand("togglebuild").setExecutor(new build());
     
  12. Offline

    DuaneTheDev_

    @Skify

    Do you have a build class?

    If yes, then are you using a abstract class?

    If yes, remove abstract.
     
    Last edited: Sep 5, 2015
  13. Offline

    Skify

    @DuaneTheDev_
    Yes, I have a build.java class. What do you mean abstract class?
     
  14. Offline

    DuaneTheDev_

    @Skify

    Code:
    public abtract class /*classname*/ implements CommandExecutor, Listener{
    }
    If you have abstract, remove it.
     
  15. Offline

    stormneo7

    Wrong Imports?
    Incorrect Constructor?

    It's help if you can post your entire build.java class.
     
  16. Offline

    DuaneTheDev_

    @Skify

    Update us, did it work?

    If yes, then mark this as solved.
     
  17. Offline

    meguy26

    @Skify
    Post all the code, from all the classes, in your plugin. Then we may be able to help. Also, learn java.
     
  18. Offline

    mythbusterma

    I have no idea how this could cause problems.

    (That was sarcasm, this will cause problems)


    @Skify

    I've already told you what you did wrong. Unless you fix it, I don't see why you keep posting saying what is effectively "idk." And for god's sake, capitalise "build."
     
  19. Offline

    Skify

    Code:java
    1.  
    2. public class build implements CommandExecutor, Plugin, Listener { // the build text has an error
    3.  
    4. private static String prefix = "§8[§eBuild§8] §f";
    5.  
    6. @Override
    7. public boolean onCommand(CommandSender sender, Command cmd, String label,
    8. String[] args) {
    9. if ((sender instanceof Player)) {
    10. Player p = (Player) sender;
    11. if(cmd.getName().equalsIgnoreCase(build")) {
    12. if(p.hasPermission("shumaapi.build")) {
    13. if(getConfig().getBoolean(p.getName() + ".build")) {
    14. p.sendMessage(prefix + "Building privilege: Enabled");
    15. getConfig().set(p.getName() + ".build", Boolean.FALSE);
    16. saveConfig();
    17. } else {
    18. p.sendMessage(prefix + "Building privilege: Disabled");
    19. getConfig().set(p.getName() + ".build", Boolean.TRUE);
    20. saveConfig();
    21. }
    22. } else {
    23. p.sendMessage(prefix + "You do not have permission to use /togglebuild");
    24. }
    25. }
    26. }
    27. return false;
    28. }
    29.  
    30. @EventHandler
    31. public void onLogin(final PlayerLoginEvent event) {
    32. if (event.getPlayer().isOp())
    33. Bukkit.getServer().getScheduler()
    34. .runTaskLater(this, new Runnable() {
    35. public void run() {
    36. event.getPlayer().sendMessage(
    37. prefix + "Build toggle has been disabled.");
    38. build.this.getConfig().set(event.getPlayer().getName() + " build", Boolean.valueOf(false));
    39. build.this.saveConfig();
    40. }
    41. }, 40L);
    42. }
    43.  
    44. @EventHandler
    45. public void breakBlockEvent(BlockBreakEvent event) {
    46. if (getConfig().getBoolean(event.getPlayer().getName() + " build"))
    47. event.setCancelled(true);
    48. }
    49.  
    50. @EventHandler
    51. public void placeBlockEvent(BlockPlaceEvent event) {
    52. if (getConfig().getBoolean(event.getPlayer().getName() + " build"))
    53. event.setCancelled(true);
    54. }
    55. }
    56.  
    57.  

    @DuaneTheDev_ @meguy26
    How do I also make it so you can only break fire blocks and place flint and steel?
     
  20. Offline

    SuperSniper

    @Skify
    You forgot the first quote:
    Code:
    if(cmd.getName().equalsIgnoreCase(build")) {
    
    Change it to:
    Code:
    if(cmd.getName().equalsIgnoreCase("build")) {
    
     
  21. Offline

    teej107

    @Skify
    Gee! I wonder why :p
     
  22. Offline

    Skify

    @SuperSniper
    No, that is just copy and paste mistakes, my one is 'togglebuild' not 'build'.

    Code:java
    1.  
    2. public class build implements CommandExecutor, Plugin, Listener {
    3.  
    4.  
    5.  
    6. private static String prefix = "§8[§eBuild§8] §f";
    7.  
    8.  
    9.  
    10. @Override
    11.  
    12. publicbooleanonCommand(CommandSendersender, Commandcmd, Stringlabel,
    13.  
    14. String[] args) {
    15.  
    16. if ((senderinstanceofPlayer)) {
    17.  
    18. Playerp = (Player) sender;
    19.  
    20. if(cmd.getName().equalsIgnoreCase("togglebuild")) {
    21.  
    22. if(p.hasPermission("shumaapi.build")) {
    23.  
    24. if(getConfig().getBoolean(p.getName() + ".build")) {
    25.  
    26. p.sendMessage(prefix + "Building privilege: Enabled");
    27.  
    28. getConfig().set(p.getName() + ".build", Boolean.FALSE);
    29.  
    30. saveConfig();
    31.  
    32. } else {
    33.  
    34. p.sendMessage(prefix + "Building privilege: Disabled");
    35.  
    36. getConfig().set(p.getName() + ".build", Boolean.TRUE);
    37.  
    38. saveConfig();
    39.  
    40. }
    41.  
    42. } else {
    43.  
    44. p.sendMessage(prefix + "You do not have permission to use /togglebuild");
    45.  
    46. }
    47.  
    48. }
    49.  
    50. }
    51.  
    52. returnfalse;
    53.  
    54. }
    55.  
    56.  
    57.  
    58. @EventHandler
    59.  
    60. publicvoidonLogin(finalPlayerLoginEventevent) {
    61.  
    62. if (event.getPlayer().isOp())
    63.  
    64. Bukkit.getServer().getScheduler()
    65.  
    66. .runTaskLater(this, new Runnable() {
    67.  
    68. public void run() {
    69.  
    70. event.getPlayer().sendMessage(
    71.  
    72. prefix + "Build toggle has been disabled.");
    73.  
    74. build.this.getConfig().set(event.getPlayer().getName() + " build", Boolean.valueOf(false));
    75.  
    76. build.this.saveConfig();
    77.  
    78. }
    79.  
    80. }, 40L);
    81.  
    82. }
    83.  
    84.  
    85.  
    86. @EventHandler
    87.  
    88. publicvoidbreakBlockEvent(BlockBreakEventevent) {
    89.  
    90. if (getConfig().getBoolean(event.getPlayer().getName() + " build"))
    91.  
    92. event.setCancelled(true);
    93.  
    94. }
    95.  
    96.  
    97.  
    98. @EventHandler
    99.  
    100. publicvoidplaceBlockEvent(BlockPlaceEventevent) {
    101.  
    102. if (getConfig().getBoolean(event.getPlayer().getName() + " build"))
    103.  
    104. event.setCancelled(true);
    105.  
    106. }
    107.  
    108. }
    109.  
     
  23. Offline

    DuaneTheDev_

    @Skify

    Remove "Plugin" from implements.

    Also, can I see your main class?
     
  24. Offline

    Skify

    @DuaneTheDev_
    If I remove "Plugin", there are many errors the saveConfig, and getConfig.

    Code:java
    1.  
    2. public class build implements CommandExecutor, Listener {
    3.  
    4. private static String prefix = "§8[§eBuild§8] §f";
    5.  
    6. @Override
    7. public boolean onCommand(CommandSender sender, Command cmd, String label,
    8. String[] args) {
    9. if ((sender instanceof Player)) {
    10. Player p = (Player) sender;
    11. if(cmd.getName().equalsIgnoreCase("togglebuild")) {
    12. if(p.hasPermission("shumaapi.build")) {
    13. if(getConfig().getBoolean(p.getName() + ".build")) {
    14. p.sendMessage(prefix + "Building privilege: Enabled");
    15. getConfig().set(p.getName() + ".build", Boolean.FALSE);
    16. saveConfig();
    17. } else {
    18. p.sendMessage(prefix + "Building privilege: Disabled");
    19. getConfig().set(p.getName() + ".build", Boolean.TRUE);
    20. saveConfig();
    21. }
    22. } else {
    23. p.sendMessage(prefix + "You do not have permission to use /togglebuild");
    24. }
    25. }
    26. }
    27. return false;
    28. }
    29.  
    30. @EventHandler
    31. public void onLogin(final PlayerLoginEvent event) {
    32. if (event.getPlayer().isOp())
    33. Bukkit.getServer().getScheduler()
    34. .runTaskLater((Plugin) this, new Runnable() {
    35. public void run() {
    36. event.getPlayer().sendMessage(
    37. prefix + "Build toggle has been disabled.");
    38. build.this.getConfig().set(event.getPlayer().getName() + " build", Boolean.valueOf(false));
    39. build.this.saveConfig();
    40. }
    41. }, 40L);
    42. }
    43.  
    44. @EventHandler
    45. public void breakBlockEvent(BlockBreakEvent event) {
    46. if (getConfig().getBoolean(event.getPlayer().getName() + " build"))
    47. event.setCancelled(true);
    48. }
    49.  
    50. @EventHandler
    51. public void placeBlockEvent(BlockPlaceEvent event) {
    52. if (getConfig().getBoolean(event.getPlayer().getName() + " build"))
    53. event.setCancelled(true);
    54. }
    55. }
    56.  
    57. [syntax=java]
    58. public void registerCommands() {
    59.  
    60. getCommand("togglebuild").setExecutor(newbuild());
    61.  
    62. getServer().getPluginManager().registerEvents(newbuild(), this);
    63.  
    64. this.getCommand("togglebuild").setExecutor(newbuild());[/syntax]
     
  25. Offline

    DuaneTheDev_

    Last edited: Sep 6, 2015
  26. Offline

    Skify

  27. Offline

    SuperSniper

    @Skify
    Can you show us the stacktrace in the console?
     
  28. Offline

    Skify

    @SuperSniper

    Code:
    06.09 16:53:24 [Server] INFO ... 15 more06.09 16:53:24 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:298) ~[spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]06.09 16:53:24 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]06.09 16:53:24 [Server] INFO at sun.reflect.GeneratedMethodAccessor3759.invoke(Unknown Source) ~[?:?]06.09 16:53:24 [Server] INFO at me.shuma.main.commands.build.breakBlockEvent(build.java:72) ~[?:?]06.09 16:53:24 [Server] INFO Caused by: java.lang.NullPointerException06.09 16:53:24 [Server] INFO at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at net.minecraft.server.v1_7_R4.PacketPlayInBlockDig.handle(PacketPlayInBlockDig.java:65) [spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at net.minecraft.server.v1_7_R4.PacketPlayInBlockDig.a(PacketPlayInBlockDig.java:41) [spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:552) [spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at net.minecraft.server.v1_7_R4.PlayerInteractManager.dig(PlayerInteractManager.java:164) [spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at net.minecraft.server.v1_7_R4.PlayerInteractManager.breakBlock(PlayerInteractManager.java:264) [spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:494) [spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:509) [spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.8-old.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]06.09 16:53:24 [Server] INFO org.bukkit.event.EventException
     
  29. Offline

    SuperSniper

    @Skify
    me.shuma.main.commands.build.breakBlockEvent(build.java:72)
    ~[?:?]06.09 16:53:24 [Server] INFO Caused by: java.lang.NullPointerException

    Please show us line 72 of "build"
     
  30. Offline

    Skify

    @SuperSniper
    Code:java
    1.  
    2. if (plugin.getConfig().getBoolean(event.getPlayer().getName() + " build"))
     
Thread Status:
Not open for further replies.

Share This Page