Solved whats wrong with this code?

Discussion in 'Plugin Development' started by Randomguy, Aug 25, 2015.

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

    Randomguy

    Code:
    package com.Zach.FireArrows;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.TNTPrimed;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityShootBowEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.Vector;
    
    public class FireArrows extends JavaPlugin implements Listener
    {
        public static boolean enabled = true;
       
        public void onEnable()
        {
            getServer().getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler
        public void arrowChecker(EntityShootBowEvent e)
        {
            if(enabled) //TODO eventually add config that allows option of mobs with tnt... Also add in config option to broadcast if its turned on and by who
            {
                if(e.getEntity() instanceof Player) //the entity is the shooter
                {
                    Vector vec = e.getProjectile().getVelocity(); //TODO eventually add effect that adds flaming path before tnt
                    Location arrowloc = e.getProjectile().getLocation();
                    e.getProjectile().getWorld().spawn(arrowloc, TNTPrimed.class).setVelocity(vec);;
                    e.setCancelled(true);
                }
            }
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {   
            if(commandLabel.equalsIgnoreCase("FireArrow"))
            {
                if(args.length == 0)
                {
                    if(args[0].equalsIgnoreCase("On"))
                    {
                        if(sender.hasPermission("Firebow.change") || sender.isOp())
                        {
                            enabled = true;
                            sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "FireArrows is active!!");
                            return true;
                        }
                        else
                            sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "YOU DO NOT HAVE PERMISSION FOR THIS COMMAND!!");
                    }
                    else if(args[0].equalsIgnoreCase("Off"))
                    {
                        if(sender.hasPermission("Firebow.change") || sender.isOp())
                        {
                            enabled = false;
                            sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "FireArrows is not active!!");
                            return true;
                        }
                        else
                            sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "YOU DO NOT HAVE PERMISSION FOR THIS COMMAND!!");
                    }
               
                    else if(args[0].equalsIgnoreCase("Help"))
                    {
                        if(sender.hasPermission("Firebow.change") || sender.isOp())
                        {
                            if(enabled)
                                sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "FireArrows is active!!");
                            else
                                sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "FireArrows is not active!!");
                           
                            sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "FireArrows, when first installed, starts on. \nTurning it on multiple times in a row doesn't turn it on and off.");
                            sender.sendMessage(ChatColor.YELLOW + "FireArrows turns arrows into TNT!!\nCommands:\nfirearrows on: Enables FireArrows.\nfirearrows off: Disables FireArrows.\nfirearrows help: FireArrows Help Page (The one your on right now).\nPermissions: Firebow.change, Firebow.help");
                            return true;
                        }
                        else
                        {
                            sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "YOU DO NOT HAVE PERMISSION FOR THIS COMMAND!!");
                        }
                    }
                    else
                    {
                        sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Unknown: " + args[0]);
                        return false;
                    }
                }
                else
                {
                    sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too much/little args. Try /FireArrow help.");
                    return false;
                }
                return true;
            }
            return false;
        }
    }
    Honestly, not sure whats wrong. I try /firearrow help or any other commands it says "Unknown Command".
    plugin.yml:
    Code:
    name: FireArrows
    author: ZachAttack1170
    version: 1.0
    main: com.Zach.FireArrows.FireArrows
    commands:
        FireArrow On:
            description: Turns on FireArrows!
            usage: /FireArrow on
        FireArrow Off:
            description: Turns off FireArrows!
            usage: /FireArrow off
        FireArrow Help:
            description: Help!
            usage: /FireArrow Help
        
     
  2. Offline

    teej107

    @Randomguy commands in the plugin.yml don't and shouldn't have arguments.
     
  3. Offline

    Zombie_Striker

    @Randomguy
    The commands part of the config is asking for Commands, not Commands AND arguments. Remove the on, off, and help and use the args[] for arguments.
     
  4. Offline

    SkyleTyler1337

    @Randomguy

    that right there tells your checking for no arguments.
    Code:
    if(args.length == 0)
                {
    try this
    Code:
    if(args.length == 1){
       // stuff
    }
     
    Last edited: Aug 25, 2015
    Randomguy likes this.
  5. Offline

    Randomguy

    args is an array... args starts at 0, right? so wouldn't you check for if(args.length == 0) because java starts counting at 0?
     
  6. Offline

    mine-care

    @Randomguy nope, the length is the ammount of elements in ther array, and o is the index so if the legth is 1 it has one element that can be obtained from arg[0]
     
    Randomguy likes this.
  7. Offline

    Randomguy

    ok... thx for the explanation...
     
  8. Offline

    mine-care

    @Randomguy Np :- )
    Also dont forget to tahg me, i wont see your response elsehow.
    (click the 'tahg user' button on the bottom right of the post)
     
  9. Offline

    Randomguy

    @mine-care ok... but it still doesn't work... It still says unknown command when I enter any commands. New code:
    Code:
    package com.Zach.FireArrows;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.TNTPrimed;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityShootBowEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.Vector;
    
    public class FireArrows extends JavaPlugin implements Listener
    {
        public static boolean enabled = true;
       
        public void onEnable()
        {
            getServer().getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler
        public void arrowChecker(EntityShootBowEvent e)
        {
            if(enabled) //TODO eventually add config that allows option of mobs with tnt... Also add in config option to broadcast if its turned on and by who
            {
                if(e.getEntity() instanceof Player) //the entity is the shooter
                {
                    Vector vec = e.getProjectile().getVelocity(); //TODO eventually add effect that adds flaming path before tnt
                    Location arrowloc = e.getProjectile().getLocation();
                    e.getProjectile().getWorld().spawn(arrowloc, TNTPrimed.class).setVelocity(vec);;
                    e.setCancelled(true);
                }
            }
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {   
            if(commandLabel.equalsIgnoreCase("FireArrow"))
            {
                if(args.length == 1) //this means if there is 1 element in the array... starts counting at 1
                {
                    if(args[0].equalsIgnoreCase("On"))
                    {
                        if(sender.hasPermission("Firebow.change") || sender.isOp())
                        {
                            enabled = true;
                            sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "FireArrows is active!!");
                            return true;
                        }
                        else
                            sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "YOU DO NOT HAVE PERMISSION FOR THIS COMMAND!!");
                    }
                    else if(args[0].equalsIgnoreCase("Off"))
                    {
                        if(sender.hasPermission("Firebow.change") || sender.isOp())
                        {
                            enabled = false;
                            sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "FireArrows is not active!!");
                            return true;
                        }
                        else
                            sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "YOU DO NOT HAVE PERMISSION FOR THIS COMMAND!!");
                    }
               
                    else if(args[0].equalsIgnoreCase("Help"))
                    {
                        if(sender.hasPermission("Firebow.change") || sender.isOp())
                        {
                            if(enabled)
                                sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "FireArrows is active!!");
                            else
                                sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "FireArrows is not active!!");
                           
                            sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "FireArrows, when first installed, starts on. \nTurning it on multiple times in a row doesn't turn it on and off.");
                            sender.sendMessage(ChatColor.YELLOW + "FireArrows turns arrows into TNT!!\nCommands:\nfirearrows on: Enables FireArrows.\nfirearrows off: Disables FireArrows.\nfirearrows help: FireArrows Help Page (The one your on right now).\nPermissions: Firebow.change, Firebow.help");
                            return true;
                        }
                        else
                        {
                            sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "YOU DO NOT HAVE PERMISSION FOR THIS COMMAND!!");
                        }
                    }
                    else
                    {
                        sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Unknown: " + args[0]);
                        return false;
                    }
                }
                else
                {
                    sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Too much/little args. Try /FireArrow help.");
                    return false;
                }
                return true;
            }
            return false;
        }
    }
     
  10. Offline

    mine-care

    @Randomguy that means commands arent added properly in plugin.yml
    As @teej107 said, you cant include spaces in the registration of commands.
    Also it is not 3 different commands, it is 1 command with 3 different parameters so:
    Code:
    commands:
       firearrow:
          lalalalalalala...
    
     
  11. Offline

    Randomguy

    @mine-care Then should I do this under commands?
    Code:
    commands:
        firearrow:
            help: helps the user
            //other
    or do i need to just do firearm:
    or:
    Code:
    commands:
        firearrow:
            description: firearrow on: turns it on. firearrow off: does this... etc.
            usage: /firearrow on, /firearrow off
    I just ended up changing the code like
    Code:
    commands:
        firearrows:
            description: do /firearrows help for help
            usage: /firearrows help
    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Aug 26, 2015
  12. Offline

    mine-care

    @Randomguy yep the last change you made is right.
     
Thread Status:
Not open for further replies.

Share This Page