args[0] == 0 error?

Discussion in 'Spigot Plugin Development' started by ben40k, Jul 5, 2015.

  1. Hi, I am trying to make a simple plugin but I have hit some snags!
    Heres the code:
    Code (Text):
    package com.smash.core;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
        public void onEnable(){
            getLogger().info("Enabled!");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
           
            if(cmd.getName().equalsIgnoreCase("sc")) {
          #     if(args[0] == 0){
                    sender.sendMessage(ChatColor.GOLD + "" + ChatColor.STRIKETHROUGH + "----------------------------------------");
                    sender.sendMessage(ChatColor.BLUE + "Made by" + ChatColor.YELLOW + " Smashlols");
                    sender.sendMessage(ChatColor.BLUE + "Version:" + ChatColor.YELLOW + " 3.0");
                    sender.sendMessage(ChatColor.BLUE + "Do /help" + ChatColor.YELLOW + "For a list of commands!");
                    sender.sendMessage(ChatColor.GOLD + "" + ChatColor.STRIKETHROUGH + "----------------------------------------");
                }else if(args[0].equalsIgnoreCase("restart")){
                    Bukkit.broadcastMessage("Restart Iminent!");
                    //Bukkit.shutdown();
                }else if (args[0].equalsIgnoreCase("stop")){
                    Bukkit.broadcastMessage("Stopping!");
                }else if (args[0].equalsIgnoreCase("custom")){
                    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', args[1] ));
         #       }else if ( args[1] == 0 ) {
                        sender.sendMessage(ChatColor.RED + "Please type in a message!");
                    }
                }else if (args[0].equalsIgnoreCase("help")){
                    sender.sendMessage(ChatColor.GOLD + "" + ChatColor.STRIKETHROUGH + "----------------------------------------");
                    sender.sendMessage(ChatColor.BLUE + "/sc restart" + ChatColor.GRAY + " : " + ChatColor.GREEN + "Broadcast a message!");
                    sender.sendMessage(ChatColor.BLUE + "/sc stop" + ChatColor.GRAY + " : " + ChatColor.GREEN + "Broadcast a message!");
                    sender.sendMessage(ChatColor.BLUE + "/sc custom" + ChatColor.GRAY + " : " + ChatColor.GREEN + "Broadcast a custom message!");
                    sender.sendMessage(ChatColor.GOLD + "" + ChatColor.STRIKETHROUGH + "----------------------------------------");
                }
           
           
            return false;
       
     
    #}
     
    # = Errors in the big red circle!
    Console:
    Code (Text):
    [21:02:26 ERROR]: Could not load 'plugins/Smashcore4.0.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.Error: Unresolved compilation problems:
        Incompatible operand types String and int
        Incompatible operand types String and int
        Syntax error, insert "}" to complete ClassBody

        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugins(CraftServer.java:291) [Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.reload(CraftServer.java:744) [Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at org.bukkit.Bukkit.reload(Bukkit.java:534) [Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) [Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchServerCommand(CraftServer.java:632) [Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at net.minecraft.server.v1_8_R1.DedicatedServer.aM(DedicatedServer.java:353) [Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:317) [Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45]
    Caused by: java.lang.Error: Unresolved compilation problems:
        Incompatible operand types String and int
        Incompatible operand types String and int
        Syntax error, insert "}" to complete ClassBody

        at com.smash.core.Main.<init>(Main.java:16) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_45]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_45]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_45]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[?:1.8.0_45]
        at java.lang.Class.newInstance(Class.java:442) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[Spigot187.jar:git-Spigot-c3c767f-33d5de3]
        ... 14 more
     
     
  2. Surly errors in your IDE will tell you the problem ;)

    anyways, do equalsIgnoreCase instead of == btw
     
    #2 NaJated, Jul 5, 2015
    Last edited: Jul 5, 2015
    • Agree Agree x 3
  3. foncused

    Moderator Patron

    Don't bump threads you created 6 minutes ago.

    == is not the same comparison as .equals() or .equalsIgnoreCase().
     
    • Agree Agree x 2
  4. You should not try to run code that doesn't compile correctly. Fix any compilation errors before trying to run the plugin.
     
    • Agree Agree x 1
  5. You cant compare apples to oranges.

    args is a string, 0 is an integer.

    See this for more information.
     
  6. Read the stacktrace, it tells you that you're missing a "}" on the specific line. at com.smash.core.Main.<init>(Main.java:16) ~[?:?] That means, line 16 of your Main class.

    EDIT: args[0] is a string value, so do:

    if(args[0] == "0")
    {
    code here
    }
     
  7. o? I dont get it could you show me a example
     
  8. nvm page just refreshed
     
  9. If I helped you out, please remember to leave me a rating, along with any others!
     
  10. I dont under stand xD
     
  11. Im not asking you to code it for me but could you modify snipits from my code?
     
  12. Because args[0] requires a STRING value, you need to make it a string, your original argument was an INTEGER, which means that it's a different data type. args[0] == "0" will work since anything inside of " " is a string.

    EDIT: Sure, I'll re-write some of it.
     
  13. So is this correct?
    Code (Text):
    package com.smash.core;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
        public void onEnable(){
            getLogger().info("Enabled!");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
           
            if(cmd.getName().equalsIgnoreCase("sc")) {
                if(args[0] == "0"){
                    sender.sendMessage(ChatColor.GOLD + "" + ChatColor.STRIKETHROUGH + "----------------------------------------");
                    sender.sendMessage(ChatColor.BLUE + "Made by" + ChatColor.YELLOW + " Smashlols");
                    sender.sendMessage(ChatColor.BLUE + "Version:" + ChatColor.YELLOW + " 3.0");
                    sender.sendMessage(ChatColor.BLUE + "Do /help" + ChatColor.YELLOW + "For a list of commands!");
                    sender.sendMessage(ChatColor.GOLD + "" + ChatColor.STRIKETHROUGH + "----------------------------------------");
                }else if(args[0].equalsIgnoreCase("restart")){
                    Bukkit.broadcastMessage("Restart Iminent!");
                    //Bukkit.shutdown();
                }else if (args[0].equalsIgnoreCase("stop")){
                    Bukkit.broadcastMessage("Stopping!");
                }else if (args[0].equalsIgnoreCase("custom")){
                    Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', args[1] ));
                }else if ( args[1] == "0" ) {
                        sender.sendMessage(ChatColor.RED + "Please type in a message!");
                    }
                }else if (args[0].equalsIgnoreCase("help")){
                    sender.sendMessage(ChatColor.GOLD + "" + ChatColor.STRIKETHROUGH + "----------------------------------------");
                    sender.sendMessage(ChatColor.BLUE + "/sc restart" + ChatColor.GRAY + " : " + ChatColor.GREEN + "Broadcast a message!");
                    sender.sendMessage(ChatColor.BLUE + "/sc stop" + ChatColor.GRAY + " : " + ChatColor.GREEN + "Broadcast a message!");
                    sender.sendMessage(ChatColor.BLUE + "/sc custom" + ChatColor.GRAY + " : " + ChatColor.GREEN + "Broadcast a custom message!");
                    sender.sendMessage(ChatColor.GOLD + "" + ChatColor.STRIKETHROUGH + "----------------------------------------");
                }
           
           
            return false;
       
     
      }
    }
     
     
  14. Wait a minute, are you trying to say if the arguments are equal to null? Or if the player has typed nothing with your command?
     
  15. For example: /sc 0 or do you want to check if they have typed /sc by itself?

    EDIT: args[0] == "0" means /sc 0 by the way.
     
  16. I want it like when they type:
    /sc: Itll post a message saying please use /sc help
    and /sc custom without the the message itll say please add a message
     
  17. You have to use equals or equalsIgnoreCase for strings.
     
    • Agree Agree x 1
  18. So Yeah I want it to be like if args = null itll post a message saying do /sc help
     
  19. JustThiemo

    JustThiemo Previously TrinityDEV

    Just use if(args.length == 0)
     
    • Agree Agree x 1
  20. Here's what you'll want: You'll want to REMOVE the check if args[0] == "0" and then do the message if(cmd.getName().equalsIgnoreCase("sc")
    {
    your message code
    }

    It's really not necessary to check for null arguments.

    EDIT: You could use TrinityDev's advice, too as an alternative.