Solved I did everything wrong i guess

Discussion in 'Plugin Development' started by WHQ, Oct 20, 2015.

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

    WHQ

    Hey,

    I've been coding for the last couple of hours and i made a little plugin wich is definitely not finished yet, lots of stuff should be added. When i enabled my server, no errors pop up but when i try to run commands nothing really works. Im feeling like i did everything wrong, i guess i did..

    Code:
    package me.Wannes001.LeagueOfMinecraft;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Material;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.util.Vector;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class LOM_Commands extends JavaPlugin implements Listener {
       
        public List<String> champions = this.getConfig().getStringList("champions");
        private List<String> ashe_champion = new ArrayList<String>();
       
        public void onEnable(){
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
            saveDefaultConfig();
        }
       
        ItemStack q = new ItemStack(Material.WOOL, 1, (short) 11);
        ItemMeta q_lore = q.getItemMeta();
        ItemStack w = new ItemStack(Material.WOOL, 1, (short) 5);
        ItemMeta w_lore = w.getItemMeta();
        ItemStack e = new ItemStack(Material.WOOL, 1, (short) 14);
        ItemMeta e_lore = e.getItemMeta();
        ItemStack ashe = new ItemStack(Material.BOW);
        ItemMeta ashe_lore = e.getItemMeta();
        float accuracy = 0.2F;
       
        @EventHandler
        public void AsheAbilities(final PlayerInteractEvent e) {
            Player p = e.getPlayer();
           
            if(!(ashe_champion.contains(p.getName()))) return;
            if(!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
            if(e.getItem().getType().equals(q));
           
            Snowball ashesnow_q = e.getPlayer().launchProjectile(Snowball.class);
            ashesnow_q.getWorld().playEffect(e.getPlayer().getLocation(), Effect.FIREWORKS_SPARK, 10);
            p.sendMessage("test for q");
           
            if((e.getItem().getType().equals(w)));
           
            Snowball ashesnow_w = e.getPlayer().launchProjectile(Snowball.class);
            Snowball ashesnow_w2 = e.getPlayer().launchProjectile(Snowball.class);
            Snowball ashesnow_w3 = e.getPlayer().launchProjectile(Snowball.class);
            Snowball ashesnow_w4 = e.getPlayer().launchProjectile(Snowball.class);
            Snowball ashesnow_w5 = e.getPlayer().launchProjectile(Snowball.class);
            Vector ashevector1 = e.getPlayer().getLocation().getDirection();
            Vector ashevector2 = e.getPlayer().getLocation().getDirection();
            Vector ashevector3 = e.getPlayer().getLocation().getDirection();
            Vector ashevector4 = e.getPlayer().getLocation().getDirection();
            Vector ashevector5 = e.getPlayer().getLocation().getDirection();
            ashevector1.add(new Vector(Math.random() * accuracy - accuracy,Math.random() * accuracy - accuracy,Math.random() * accuracy - accuracy));
            ashevector2.add(new Vector(Math.random() * accuracy - accuracy,Math.random() * accuracy - accuracy,Math.random() * accuracy - accuracy));
            ashevector3.add(new Vector(Math.random() * accuracy - accuracy,Math.random() * accuracy - accuracy,Math.random() * accuracy - accuracy));
            ashevector4.add(new Vector(Math.random() * accuracy - accuracy,Math.random() * accuracy - accuracy,Math.random() * accuracy - accuracy));
            ashevector5.add(new Vector(Math.random() * accuracy - accuracy,Math.random() * accuracy - accuracy,Math.random() * accuracy - accuracy));
            ashesnow_w.setVelocity(ashevector1);
            ashesnow_w2.setVelocity(ashevector2);
            ashesnow_w3.setVelocity(ashevector3);
            ashesnow_w4.setVelocity(ashevector4);
            ashesnow_w5.setVelocity(ashevector5);
            ashesnow_w.getWorld().playEffect(e.getPlayer().getLocation(), Effect.FIREWORKS_SPARK, 10);
            ashesnow_w2.getWorld().playEffect(e.getPlayer().getLocation(), Effect.FIREWORKS_SPARK, 10);
            ashesnow_w3.getWorld().playEffect(e.getPlayer().getLocation(), Effect.FIREWORKS_SPARK, 10);
            ashesnow_w4.getWorld().playEffect(e.getPlayer().getLocation(), Effect.FIREWORKS_SPARK, 10);
            ashesnow_w5.getWorld().playEffect(e.getPlayer().getLocation(), Effect.FIREWORKS_SPARK, 10);
            p.sendMessage("test for w");
           
            if((e.getItem().getType().equals(e)));
           
            Snowball ashesnow_e = e.getPlayer().launchProjectile(Snowball.class);
            ashesnow_e.getWorld().playEffect(e.getPlayer().getLocation(), Effect.FIREWORKS_SPARK, 10);
            p.sendMessage("Scouting the area for you!!!");
        }
       
         @EventHandler
         public void Asheprojectile(ProjectileHitEvent e) {
         Projectile ashep = e.getEntity();
         if(!(ashep instanceof Snowball)) return;
         Snowball s = (Snowball) ashep;
    
         for(Entity en : s.getNearbyEntities(10, 30, 10)) {
         if(en instanceof Player) {
         Player pl = (Player) en;
         if(!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 60, 0));
         }
         }
         }
    
        
        public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args){
            Player p = (Player) sender;
           
            if(cmd.getName().equalsIgnoreCase("lom") || cmd.getName().equalsIgnoreCase("leagueofminecraft")){
                p.sendMessage(ChatColor.DARK_BLUE + "<[" + ChatColor.GOLD + "League of Minecraft" + ChatColor.DARK_BLUE + "]>");
                p.sendMessage("version: 0.2 (beta)");
                p.sendMessage("made by WHQ");
            }
    
                else if(args.length == 2){
                    if(args[0].equalsIgnoreCase("championlist") ||args[0].equalsIgnoreCase("champlist")){
                        p.sendMessage(ChatColor.DARK_BLUE + "<[" + ChatColor.GOLD + "Available Champions" + ChatColor.DARK_BLUE + "]>");
                       
                        for (String champs : champions)
                            p.sendMessage(champs);
                }
                }
    
    
                    if(args[0].equalsIgnoreCase("championselect") || args[0].equalsIgnoreCase("champselect")){
                        p.sendMessage(ChatColor.DARK_RED + "Wrong usage! try: /lom championselect <champion>");
                    }
                       
                        else if(args[1].equalsIgnoreCase("Ashe")){
                            ashe_champion.add(p.getName());
                           
                            ashe_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Ashe's bow"));
                            ashe.setItemMeta(ashe_lore);
                            p.getInventory().addItem(ashe);
                           
                            q_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Ranger's Focus", ChatColor.GOLD + "For 5 seconds, you can shoot snowballs that will slow the enemy", ChatColor.GRAY + "Cooldown: 12 seconds"));
                            q.setItemMeta(q_lore);
                            p.getInventory().addItem(q);
                           
                            w_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Volley", ChatColor.GOLD + "When activated, 5 snowballs will be shot into different directions", ChatColor.GRAY + "Cooldown: 15 seconds"));
                            q.setItemMeta(w_lore);
                            p.getInventory().addItem(w);
                           
                            e_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Hawkshot", ChatColor.GOLD + "Use a snowball to scout the area, if the snowball lands near enemy players then you will see their location (radius = 20 blocks)", ChatColor.GRAY + "Cooldown: 20 seconds"));
                            q.setItemMeta(q_lore);
                            p.getInventory().addItem(q);
                        }
                    return true;
        }
    }
    
    
    Plugin.yml
    Code:
    name: LeagueofMinecraft
    main: me.Wannes001.LeagueOfMinecraft.LOM_Commands
    version: 0.2
    description: >
                 LeagueOfMinecraft v0.2(beta)
    commands:
      LOM:
        description: leagueofminecraft basic command
    
    Crash log:
    Code:
    [18:23:06 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'lom'
    in plugin LeagueofMinecraft v0.2
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServe
    r.java:641) ~[craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:1162) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
    :997) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :45) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :1) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
    3) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_31]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukki
    t.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
    15) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    74) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    54) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :557) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at me.Wannes001.LeagueOfMinecraft.LOM_Commands.onCommand(LOM_Commands.ja
    va:137) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Spigot-5f38d38-12698ea]
            ... 15 more
    
    Thanks in advance
     
  2. Offline

    FabeGabeMC

    check if sender is an instance of a player, first.
    Don't use uppercases whilst registering commands. Also, you forgot to register the alias ('leagueofminecraft')
    Source
     
  3. Offline

    mcdorli

    First.: package names should be all lowercase
    Second: You cast the sender to a player, before checking, that it is actually a player
    Third:This is an ArrayOutOfBoundsException, You didn't check, if args[1] even exists on line 137, you should learn, how to read stacktraces
    4.: You always return true, no matter what.
    5.: There is a lot of useless if's in the code, at least open them, and put the correct stuff inside it, or delete them, but in this form, they don't do anything. Learn java before bukkit.
     
    Last edited: Oct 20, 2015
  4. Offline

    WHQ

    @mcdorli

    Okay,
    - i fixed the package name
    - i now have added: if (sender instanceof Player){

    but i don't understand the args[1] part, what should i do with that? (im sorry if i sound stupid now)
     
  5. Offline

    mcdorli

    You placed the "if (args[1] == ...)" section outside the "if (args.length == 2 )" statement. Check, where your brackets end.
     
    WHQ likes this.
  6. Offline

    WHQ

    @mcdorli
    Oh damn you are right,
    Thanks mate! i removed the 2nd bracket on line 130 and then placed it at the end of the codes, no errors are showing so im gonna test it now :)

    @mcdorli

    Okay, errors don't pop up anymore so thats a big plus, thanks,
    But now i can only do /lom, all commands with multiple arguments dont work, i think i screwed it up somewhere with the args[] or brackets again.
    Code:
        
    public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args){
            if(sender instanceof Player) {
            Player p = (Player) sender;
           
            if(cmd.getName().equalsIgnoreCase("lom") || cmd.getName().equalsIgnoreCase("leagueofminecraft")){
                p.sendMessage(ChatColor.DARK_BLUE + "<[" + ChatColor.GOLD + "League of Minecraft" + ChatColor.DARK_BLUE + "]>");
                p.sendMessage("version: 0.2 (beta)");
                p.sendMessage("made by Wannes001");
            }
    
    
                else if(args.length == 2){
                    if(args[0].equalsIgnoreCase("championlist") ||args[0].equalsIgnoreCase("champlist")){
                        p.sendMessage(ChatColor.DARK_BLUE + "<[" + ChatColor.GOLD + "Available Champions" + ChatColor.DARK_BLUE + "]>");
                       
                        for (String champs : champions)
                            p.sendMessage(champs);
                }
    
    
    
                    if(args[0].equalsIgnoreCase("championselect") || args[0].equalsIgnoreCase("champselect")){
                        p.sendMessage(ChatColor.DARK_RED + "Wrong usage! try: /lom championselect <champion>");
                    }
                       
                        else if(args[1].equalsIgnoreCase("Ashe")){
                            ashe_champion.add(p.getName());
                           
                            ashe_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Ashe's bow"));
                            ashe.setItemMeta(ashe_lore);
                            p.getInventory().addItem(ashe);
                           
                            q_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Ranger's Focus", ChatColor.GOLD + "For 5 seconds, you can shoot snowballs that will slow the enemy", ChatColor.GRAY + "Cooldown: 12 seconds"));
                            q.setItemMeta(q_lore);
                            p.getInventory().addItem(q);
                           
                            w_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Volley", ChatColor.GOLD + "When activated, 5 snowballs will be shot into different directions", ChatColor.GRAY + "Cooldown: 15 seconds"));
                            q.setItemMeta(w_lore);
                            p.getInventory().addItem(w);
                           
                            e_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Hawkshot", ChatColor.GOLD + "Use a snowball to scout the area, if the snowball lands near enemy players then you will see their location (radius = 20 blocks)", ChatColor.GRAY + "Cooldown: 20 seconds"));
                            q.setItemMeta(q_lore);
                            p.getInventory().addItem(q);
                        }
                }
            }
            return true;
        }
    }
    
    And the plugin.yml
    Code:
    name: LeagueOfMinecraft
    main: me.wannes001.LeagueOfMinecraft.LOM_Commands
    version: 0.2
    description: >
                 LeagueOfMinecraft v0.2(beta)
    commands:
      lom:
        aliases: [leagueofminecraft]
        description: the basic command of leagueofminecraft
    
    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Oct 20, 2015
  7. Offline

    mcdorli

    Yes, I see the error, you do
    if (cmd.getName().equalsIgnoreCase("lom")) {
    ....
    } else if (args.length == 2) {
    ...
    }
    This means, that if the command name is "lom", then do something, but if it isn't, and the args.length equals to 2, then do somwthing else.
     
  8. Offline

    WHQ

    @mcdorli

    i did what you said, still the same, with all comands i get the message that i normally only should get when doing /lom
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args){
            if(sender instanceof Player) {
            Player p = (Player) sender;
           
            if(cmd.getName().equalsIgnoreCase("lom")){
                p.sendMessage(ChatColor.DARK_BLUE + "<[" + ChatColor.GOLD + "League of Minecraft" + ChatColor.DARK_BLUE + "]>");
                p.sendMessage("version: 0.2 (beta)");
                p.sendMessage("made by Wannes001");
           
            } else if(args.length == 2){
                    if(args[0].equalsIgnoreCase("championlist") ||args[0].equalsIgnoreCase("champlist")){
                        p.sendMessage(ChatColor.DARK_BLUE + "<[" + ChatColor.GOLD + "Available Champions" + ChatColor.DARK_BLUE + "]>");
                       
                        for (String champs : champions)
                            p.sendMessage(champs);
                    }
    
    
    
                    if(args[0].equalsIgnoreCase("championselect") || args[0].equalsIgnoreCase("champselect")){
                        p.sendMessage(ChatColor.DARK_RED + "Wrong usage! try: /lom championselect <champion>");
                    }
                       
                        else if(args[1].equalsIgnoreCase("Ashe")){
                            ashe_champion.add(p.getName());
                           
                            ashe_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Ashe's bow"));
                            ashe.setItemMeta(ashe_lore);
                            p.getInventory().addItem(ashe);
                           
                            q_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Ranger's Focus", ChatColor.GOLD + "For 5 seconds, you can shoot snowballs that will slow the enemy", ChatColor.GRAY + "Cooldown: 12 seconds"));
                            q.setItemMeta(q_lore);
                            p.getInventory().addItem(q);
                           
                            w_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Volley", ChatColor.GOLD + "When activated, 5 snowballs will be shot into different directions", ChatColor.GRAY + "Cooldown: 15 seconds"));
                            q.setItemMeta(w_lore);
                            p.getInventory().addItem(w);
                           
                            e_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Hawkshot", ChatColor.GOLD + "Use a snowball to scout the area, if the snowball lands near enemy players then you will see their location (radius = 20 blocks)", ChatColor.GRAY + "Cooldown: 20 seconds"));
                            q.setItemMeta(q_lore);
                            p.getInventory().addItem(q);
                        }
            }
            }
            return true;
        }
    }
    
     
  9. @WHQ Please fix that plugin description.
     
  10. Offline

    mythbusterma

  11. @mythbusterma I don't see the reason of having a > then going to a new line for the description.
     
    WHQ likes this.
  12. Offline

    elian1203

    You didn't fix it, you're still checking for argument length outside of the command
    if(cmd.label.equalsIgnoreCase("lom")
    {
    if(args.length == 1)
    {
    ...
    }
    }
    Sorry for code looking ugly, just wrote it in here.
     
    WHQ likes this.
  13. Offline

    WHQ

    @elian1203 Any help is welcome,
    i still can't figure it out though? I have tried to reposition several brackets but i can't get my head around it, if anyone could tell me what brackets should be different theni would be very thankfull :oops:
    Code:
         public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args){
                if(sender instanceof Player) {
                    Player p = (Player) sender;
                  
                    if(cmd.getName().equalsIgnoreCase("lom")){
                        p.sendMessage(ChatColor.DARK_BLUE + "<[" + ChatColor.GOLD + "League of Minecraft" + ChatColor.DARK_BLUE + "]>");
                        p.sendMessage("version: 0.2 (beta)");
                        p.sendMessage("made by Wannes001");
                      
                  
                        }else if(args.length == 2){
                            if(args[0].equalsIgnoreCase("championlist") ||args[0].equalsIgnoreCase("champlist")){
                                p.sendMessage(ChatColor.DARK_BLUE + "<[" + ChatColor.GOLD + "Available Champions" + ChatColor.DARK_BLUE + "]>");
                              
                                for (String champs : champions)
                                    p.sendMessage(champs);
                        }
                          
                            if(args[0].equalsIgnoreCase("championselect") || args[0].equalsIgnoreCase("champselect")){
                                p.sendMessage(ChatColor.DARK_RED + "Wrong usage! try: /lom championselect <champion>");
                                }
                          
                            else if(args[1].equalsIgnoreCase("Ashe")){
                                ashe_champion.add(p.getName());
                              
                                ashe_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Ashe's bow"));
                                ashe.setItemMeta(ashe_lore);
                                p.getInventory().addItem(ashe);
                              
                                q_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Ranger's Focus", ChatColor.GOLD + "For 5 seconds, you can shoot snowballs that will slow the enemy", ChatColor.GRAY + "Cooldown: 12 seconds"));
                                q.setItemMeta(q_lore);
                                p.getInventory().addItem(q);
                              
                                w_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Volley", ChatColor.GOLD + "When activated, 5 snowballs will be shot into different directions", ChatColor.GRAY + "Cooldown: 15 seconds"));
                                q.setItemMeta(w_lore);
                                p.getInventory().addItem(w);
                              
                                e_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Hawkshot", ChatColor.GOLD + "Use a snowball to scout the area, if the snowball lands near enemy players then you will see their location (radius = 20 blocks)", ChatColor.GRAY + "Cooldown: 20 seconds"));
                                q.setItemMeta(q_lore);
                                p.getInventory().addItem(q);
                            }
                        }
                }
                return true;
         }
    }
    
     
  14. Offline

    mcdorli

    Do you even changed, what I said was wrong?

    this place:

    Is wrong, you do
    Code:
    if (cmd.getName().equalsIgnoreCase("lom")) {
        ....
    } else if (args.length == 2) {
        ....
    }
    
    Do you see, why it is wrong?

    Don't use loops and statements without brackets, it makes you confused.
     
  15. Offline

    Scimiguy

    @WHQ
    You still haven't changed what you've been told to change twice now.

    @mcdorli
    If you look closely.. you can see that his "Fixing" of that problem was him putting some more whitespace before the
    } else if
    So that it doesn't line up. -.-
     
  16. Offline

    mcdorli

    @WHQ, press Shift+Ctrl+F, and check the code again for problems.
    @Scimiguy Wait, you're totally right... I'm amazed by these new programming techniches (I dont' remember how to write technic in plural, please send help!).
     
    Last edited: Oct 20, 2015
  17. This is what your code does

    If the command is Lom, send 3 messages
    Else if the args are equal to 2

    You say if the command is Blah otherwise if the args are this. Th argument code goes in the command check, not against the command check.
     
    WHQ and mcdorli like this.
  18. Offline

    elian1203

    I wrote an outline for you, because you don't seem to get it. I recommend you put your brackets like I do because it's easier to detect where certain things begin and end., but it's preference.
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args)
        {
            if(!(sender instanceof Player))
                return false;
            if(label.equalsIgnoreCase("lom"))
            {
                if(args.length == 0)
                {
                    //code
                } else if(args.length == 1)
                {
                    //code
                }
            }
        }
     
    WHQ likes this.
  19. Offline

    WHQ

    @elian1203 Ah okay, i edited my code with your instructions, would this work :) ?

    Code:
         public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args)
         {
             if(!(sender instanceof Player))
                 return false;
            
             Player p = (Player) sender;
            
             if(label.equalsIgnoreCase("lom"))
             {
                 if(args.length == 0)
                 {
                    
                     if(args[0].equalsIgnoreCase("championlist")){
                            p.sendMessage(ChatColor.DARK_BLUE + "<[" + ChatColor.GOLD + "Available Champions" + ChatColor.DARK_BLUE + "]>");
                           
                            for (String champs : champions)
                                p.sendMessage(champs);
                     }
                    
                     if(args[0].equalsIgnoreCase("championselect")){
                         p.sendMessage(ChatColor.DARK_RED + "Wrong usage! try /lom championselect <champion>");
                     }
                    
                 }else if(args.length == 1)
                
                 {
                     if(args[1].equalsIgnoreCase("Ashe")){
                            ashe_champion.add(p.getName());
                           
                            ashe_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Ashe's bow"));
                            ashe.setItemMeta(ashe_lore);
                            p.getInventory().addItem(ashe);
                           
                            q_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Ranger's Focus", ChatColor.GOLD + "For 5 seconds, you can shoot snowballs that will slow the enemy", ChatColor.GRAY + "Cooldown: 12 seconds"));
                            q.setItemMeta(q_lore);
                            p.getInventory().addItem(q);
                           
                            w_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Volley", ChatColor.GOLD + "When activated, 5 snowballs will be shot into different directions", ChatColor.GRAY + "Cooldown: 15 seconds"));
                            q.setItemMeta(w_lore);
                            p.getInventory().addItem(w);
                           
                            e_lore.setLore(Arrays.asList(ChatColor.DARK_BLUE + "Hawkshot", ChatColor.GOLD + "Use a snowball to scout the area, if the snowball lands near enemy players then you will see their location (radius = 20 blocks)", ChatColor.GRAY + "Cooldown: 20 seconds"));
                            q.setItemMeta(q_lore);
                            p.getInventory().addItem(q);
                     }
                 }
             }
            return true;
         }
    }
    
     
  20. Offline

    mcdorli

    Probably.

    And press Ctrl+Shift+F, it makes your life easier.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
    WHQ likes this.
  21. Offline

    boomboompower

    @WHQ
    Try it out, looks good to me.

    If it does work, please mark this thread as solved, thx -boom

    @mcdorli
    Use the edit button instead of double posting
     
  22. Offline

    WHQ

    @mcdorli @elian1203 Thanks for your help, in the beginning i misunderstood you and i didn't fix the right thing, i apologize, im gonna test my code soon and i think it will work this time!

    Sadly, every command gives me errors now
    Code:
    [12:39:40 INFO]: Wannes001 issued server command: /lom championselect
    [12:39:40 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'lom
    in plugin LeagueOfMinecraft v0.2
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cr
    ftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:1
    1) ~[craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServ
    r.java:641) ~[craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerCo
    nection.java:1162) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.jav
    :997) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.jav
    :45) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.jav
    :1) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:
    3) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    ?:1.8.0_31]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukk
    t.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:
    15) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:
    74) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:
    54) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.jav
    :557) [craftbukkit.jar:git-Spigot-5f38d38-12698ea]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at me.Wannes001.LeagueOfMinecraft.LOM_Commands.onCommand(LOM_Commands.j
    va:140) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cr
    ftbukkit.jar:git-Spigot-5f38d38-12698ea]
            ... 15 more
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  23. Offline

    boomboompower

    @WHQ Whats line 140?
     
  24. Offline

    WHQ

  25. Offline

    mcdorli

    What's on line 140?

    Then there's probably still messed io with your if...else ifs

    You should rewrite the whole code, but make the if-elses first,and just then add the real code

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  26. Offline

    boomboompower

    @mcdorli
    Please stop double posting. Use the edit button
    [​IMG]
     
  27. Offline

    mcdorli

    Sorry, these forum post are really fast-paced, and I tought, that there is like 3 messages after my first one.
     
  28. Offline

    WHQ

    @mcdorli @boomboompower
    I fixed it, the plugin is working so far :)
    the problem wasn't the if's and else if's

    I changed the
    Code:
    if(args.length == 0)
    to
    Code:
    if(args.length == 1)
    And the
    Code:
    if(args.length == 1)
    to
    Code:
    if(args.length == 2)
    That was the problem, thread is solved :D
     
  29. Offline

    elian1203

    You made it so that
    if(args.length == 0)
    {
    if(args[0])....
    }
    That is incorrect. If the args length are zero, that means they just did /lom. So if the args.length would be equal to 1 you would use args[0]. It's weird, but zero is used as a number too, not with args.length though.

    Oh woops, didn't notice your reply, congrats.
     
Thread Status:
Not open for further replies.

Share This Page