Solved Usage of boolean returns at CommandExecutor

Discussion in 'Plugin Development' started by ElCreeperHD, Mar 8, 2016.

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

    ElCreeperHD

    Hello, I am making a plugin, and I have the same error I sometimes have. The command sends its usage when executing it. I tried adding returns at the end of every command check, but it is not working. I made this mistake 10 times, so I want to know how this works (I saw other threads, but they did not help me).
    Here is the code (/cursemc says its usage when executing it and it is not working):

    Code:
    package cursemc;
    
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Color;
    import org.bukkit.FireworkEffect;
    import org.bukkit.FireworkEffect.Type;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Firework;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.FireworkMeta;
    import org.bukkit.inventory.meta.ItemMeta;
    
    
    
    
    
    public class CommandListener implements CommandExecutor{
      
        private Main plugin;
    
        public CommandListener(Main plugin)
        {
          this.plugin = plugin;
        }
           private Bounty bounty;
           public CommandListener(Bounty bounty) {
             this.bounty = bounty;
           }
        public Logger log = Bukkit.getLogger();
        String prefix = ChatColor.WHITE + "[" + ChatColor.AQUA + "CurseMC" + ChatColor.WHITE + "]";
      
    
      
    
        public boolean onCommand(CommandSender cmds, Command cmd, String string, String[] args) {
            if(cmd.getName().equalsIgnoreCase("cursefireworks"))    {
    
                if (args.length == 0)
                {   
                    Player p = (Player)cmds;   
                    cmds.sendMessage(prefix + ChatColor.GREEN + "" + ChatColor.BOLD + " Fireworks fired!");
                  
                     fireworks(p);
         
              
                }else if (args.length == 1){  
                   Player p = Bukkit.getPlayer(args[0]);
                   if(p != null){  
                       cmds.sendMessage(prefix + ChatColor.GREEN + "" + ChatColor.BOLD + " Fireworks fired at the location of " + p.getName() + "!");
                       fireworks(p); 
                   }else{  
                       cmds.sendMessage(prefix + ChatColor.RED + "" + ChatColor.BOLD + " That player is not online!");
                   }
                  
                  
                }//close args length 1
                else if(args.length >= 2){  
                    cmds.sendMessage(prefix + ChatColor.RED + " Oops, I think this command is not valid!");
                    cmds.sendMessage(prefix + ChatColor.AQUA + " -Mr_Matwin3");   
                  
                  
                  
                }//close not valid (more or equal 2) arg length
             
              
             return true; 
              
            }
            else if(cmd.getName().equalsIgnoreCase("curse"))    {
    
                if (args.length == 0)
                {   
                //    Player p = (Player)cmds;   
                    cmds.sendMessage(ChatColor.GREEN + "++++++++++++++++++++++++++++++");
                    cmds.sendMessage(ChatColor.GREEN + "+" + "       " + ChatColor.YELLOW + "CurseMC V 0.5.2      " + ChatColor.GREEN +  "+");
                    cmds.sendMessage(ChatColor.GREEN + "+" + "     " + ChatColor.YELLOW + "Developed by Mr_Matwin3   " + ChatColor.GREEN +  "+");
                    cmds.sendMessage(ChatColor.GREEN + "+" + "     " + ChatColor.RED + "/curse help   " + ChatColor.GREEN +  "+");
                    cmds.sendMessage(ChatColor.GREEN + "+++++++++++++++++++++++++++++");
                }
                else if (args.length == 1){
                 if(args[0].equalsIgnoreCase("skills"))    {
                    cmds.sendMessage(ChatColor.RED + "Use /curse skills Item Skill Level");
                    }//close skills
                }//close args length 1
                else if(args.length == 4){  
            
                    if(cmds instanceof Player){
                     
                    Player p = (Player)cmds;   
          if(cmds.hasPermission("curse.skill.give"))    {
                    cmds.sendMessage(prefix + ChatColor.RED + " You just got an item with skills!");
                    ItemStack item = new ItemStack(Material.valueOf(args[1]), 1);
                    ItemMeta meta = item.getItemMeta();
                    List<String> lorelist = new ArrayList<String>();
                    lorelist.add(ChatColor.GOLD + "Skill:" + getColor(args[2]) + args[2] + ChatColor.DARK_GREEN + "[" + args[3]+ "]");//This is the first line of lore
                   meta.setLore(lorelist);
                    item.setItemMeta(meta);
                    p.getInventory().addItem(item);
                  
                  
    }else{
         cmds.sendMessage(prefix + ChatColor.RED + " Sorry, you need permissions!");
    }
        
                    }//end player check
                }
                else if(args.length >= 5){  
    
                    cmds.sendMessage(prefix + ChatColor.RED + " Oops, I think this command is not valid!");
                    cmds.sendMessage(prefix + ChatColor.AQUA + " -Mr_Matwin3");
                  
                  
                  
                }//close not valid (more or equal 5) arg length
             
              
              
                return true;
            }//end cmd
          
               return false;
        }//end of onCommand
    
        public static void fireworks(Player p){
            //Spawn the Firework
            Firework fw = (Firework) p.getWorld().spawnEntity(p.getLocation(), EntityType.FIREWORK);
            FireworkMeta fwm = fw.getFireworkMeta();
    
            //random
            Random ra = new Random();
    
            //sets type
            int rt = ra.nextInt(5) + 1;
            Type type = Type.BALL;
            if (rt == 1) type = Type.BALL;
            if (rt == 2) type = Type.BALL_LARGE;
            if (rt == 3) type = Type.BURST;
            if (rt == 4) type = Type.CREEPER;
            if (rt == 5) type = Type.STAR;
    
            //colors
            int r = ra.nextInt(256);
            int b = ra.nextInt(256);
            int g = ra.nextInt(256);
            Color c1 = Color.fromRGB(r, g, b);
    
            r = ra.nextInt(256);
            b = ra.nextInt(256);
            g = ra.nextInt(256);
            Color c2 = Color.fromRGB(r, g, b);
    
    
            //effect
            FireworkEffect effect = FireworkEffect.builder().flicker(ra.nextBoolean()).withColor(c1).withFade(c2).with(type).trail(ra.nextBoolean()).build();
    
            //applied effects
            fwm.addEffect(effect);
    
            //random power!
            int rp = ra.nextInt(2) + 1;
            fwm.setPower(rp);
    
            //set it
            fw.setFireworkMeta(fwm);   
          
          
        }
        ChatColor getColor(String skill)    {
              if(skill.equalsIgnoreCase("Beheading")){
                  return ChatColor.GREEN;
                        
                      }else if(skill.equalsIgnoreCase("Freeze"))    {
                          return ChatColor.AQUA;
                      }else if(skill.equalsIgnoreCase("Vampiric Regeneration"))    {
                          return ChatColor.DARK_RED;
                          }
                      else if(skill.equalsIgnoreCase("Venomous Strike"))    {
                          return ChatColor.DARK_GREEN;
                          }
                      else if(skill.equalsIgnoreCase("Vanish"))    {
                          return ChatColor.DARK_GRAY;
                          }
                      else if(skill.equalsIgnoreCase("Fireworks"))    {
                          return ChatColor.LIGHT_PURPLE;
                          }
                      else if(skill.equalsIgnoreCase("Lightning"))    {
                          return ChatColor.YELLOW;
                          }
            return ChatColor.WHITE;
        }
    }
    
    EDIT: Thanks! I just forgot to register the command at Main class
     
    Last edited: Mar 9, 2016
  2. Offline

    HoeMC

    It sends usage when you return false. Please fix your indentation too; it's incredibly hard to read that code.
     
    Last edited: Mar 8, 2016
  3. Offline

    Zombie_Striker

    Please follow package naming conventions; Please use either a domain (E.g. If you own "google.com", use "com.google") or your reverse email address (e.g. com.gmail.example).

    This will always leave either "plugin" null, or "bounty" null. I think what you're thinking of is
    Code:
    public CommandListener(Main plugin, [I]Bounty bounty[/I]){
    1. It does not look like it is ever used.
    2. You don't need it to be public, not do you need to create a field.
    DON'T BLIND CAST! This was in the first command. Always rememeber to check if a sender is a Player before casting.
    Just some suggestions:
    1. Grab the "version" from the plugin.yml instead of having to edit this piece of text every time there's a new update
    2. Grab the author from the plugin.yml
    3. Grab the commands from the plugin.yml

    This^

    And This^
     
  4. Offline

    ElCreeperHD

    @HoeMC But I returned true at all the commands? And the problem is only with /cursemc.

    That code is working.

    I know, but the players know they should not use console to execute that, I will add the check.

    I considered the other suggestions.
     
  5. Offline

    HoeMC

    You haven't handled the command "cursemc" in that class. Is it in another class and registered in your main class & plugin.yml?

    The code is working for now but if you need to access non-static fields/methods from bounty you won't be able to.
     
    Last edited: Mar 8, 2016
Thread Status:
Not open for further replies.

Share This Page