Helpme with this plugin

Discussion in 'Spigot Plugin Development' started by Raymart, Jun 3, 2015.

  1. Code (Text):
    package me.raymart.motd;

    import java.util.logging.Logger;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class CMD_motd
      implements CommandExecutor
    {
      public final Logger logger = Logger.getLogger("Minecraft");
      public MOTD plugin;

      public CMD_motd(MOTD instance)
      {
        plugin = instance;
      }

      public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
      {
        if (!(sender instanceof Player)) {
          sender.sendMessage("Only players can do that");
          return false;
        }
        Player p = (Player)sender;
        String prefix = this.plugin.getConfig().getString("Prefix").replaceAll("&", "§");
        String subcmd = args[0].toLowerCase();
        String word = this.plugin.getConfig().getString("MOTD").replaceAll("&n", "\n").replace("<player>", p.getName());
        String noperm = this.plugin.getConfig().getString("No Permission Message").replaceAll("&", "§");
        String perm = this.plugin.getConfig().getString("Permission Node For All");
       
        if(commandLabel.equalsIgnoreCase("motd")) {
            if(args.length == 0) {
                p.sendMessage(prefix + "/motd help");
                return true;
            }
        }
                 if (subcmd.equalsIgnoreCase("help")) {
                     if ((!p.hasPermission("motd.help")) || (!p.hasPermission(perm))) {
                       p.sendMessage(noperm);
                       return true;
                     }
                     if (args.length != 1) {
                       return false;
                     }
                     p.sendMessage(prefix + "use /motd reload to reload the plugin");
                     p.sendMessage(prefix + "use /motd check to check the motd");
                     return true;
                   }if (subcmd.equalsIgnoreCase("reload")) {
                     if ((!p.hasPermission("motde.reload")) || (!p.hasPermission(perm))) {
                       p.sendMessage(noperm);
                       return true;
                     }
                     if (args.length != 1) {
                       return false;
                     }
                     this.plugin.reloadConfig();
                     p.sendMessage(prefix + "Has been reloaded");
                     return true;
                   }if (subcmd.equalsIgnoreCase("check")) {
                     if ((!p.hasPermission("motde.check")) || (!p.hasPermission(perm))) {
                       p.sendMessage(noperm);
                       return true;
                     }
                     if (args.length != 1) {
                       return false;
                     }
                     p.sendMessage(word);
                     return true;
                   }
        return false;
      }
    }
    why can't I use /motd ingame ?
     
  2. Did you get any stacktrace?
    Did you register the command?
    Did you add it to plugin.yml?
    What if you add a debug message to the start of the function?
     
  3. @SubSide
    Did you get any stacktrace? Where it is ?
    Did you register the command? Yes
    Did you add it to plugin.yml? Yes
    What if you add a debug message to the start of the function? What you mean by that ?
     
  4. Stacktrace is in console.
    debug message System.out.println("test");
     
  5. @SubSide
    stacktrace none
    if I add a debug message
    Code (Text):
    org.bukkit.command.CommandException: Unhandled exception executing command 'motd
    ' in plugin MOTD v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
    9) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServe
    r.java:544) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:932) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.raymart.motd.CMD_motd.onCommand(CMD_motd.java:28) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2992jnks]
            ... 13 more
     
  6. Well, there you go.
    String subcmd = args[0].toLowerCase();
    what if no argument is given?

    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
     
  7. so what code should I use ?
     
  8. @Raymart
    • Have you set the executor of the command?
    • Don't use Logger.getLogger("Minecraft"), use the plugin's Logger object.
    • Don't use replaceAll for String literals (any String that is not a regular expression).
    • Use ChatColor.translateAlternateColorCodes(char, String) to colour Strings, rather than using replace.
    • Check if an array has enough elements before trying to access them (like @SubSide sneakily mentioned).
    • Use else if where applicable.
    • Mind your code format, it's hardly readable.
    • Instead of checking something over and over again, check it once and store the result. (or act instantly, like with your args.length checks).
    • Make your fields private if no other classes should have access. If they need access, create getter methods.
     
    • Agree Agree x 1
  9. since I fix this I already don't need this but thanks anyway
     
  10. Might still be a good idea to fix what he said. ;)
     
    • Agree Agree x 2