Help with commands.

Discussion in 'Spigot Plugin Development' started by bradley13, May 30, 2016.

  1. Hey there, I am trying to create a command, /complexcrate <username>, I have copied and pasted from another command for a lower crate but this one isn't working and I don't know why.

    Code for the command:
    Code (Text):

            if(cmd.getName().equalsIgnoreCase("ComplexCrate")){

                Log.info("100");

                Player player = (Player) sender;

                if(!player.hasPermission("spartancraft.complexcrate")){

                    Log.info("100");

                    player.sendMessage(ChatColor.RED + "You do not have permission for this crate!");

                }else{

                    Log.info("100");

                    if(args.length == 0){

                        player.sendMessage(ChatColor.RED + "Please specify a player to give the crate to!");

                    }else{

                        Log.info("100");

                    Player target = Bukkit.getServer().getPlayer(args [0]);

                    ItemStack complex = new ItemStack(Material.CHEST, 1);

                    ItemMeta complexMeta = complex.getItemMeta();

                    complexMeta.setDisplayName(ChatColor.GREEN + "Complex supply chest " + ChatColor.GRAY + "(right click to open)");

                    ArrayList<String> lore2 = new ArrayList<String>();

                    lore2.add(ChatColor.RED + "The most complex of all supplies");

                    complexMeta.setLore(lore2);

                    complex.setItemMeta(complexMeta);

                 

                    if(target.getInventory().contains(complex)){

                        Log.info("100");

                        if(target.getInventory().firstEmpty()==-1){

                            target.sendMessage(ChatColor.RED + "Inventory is full you cannot give you that item.");

                        }else{

                            Log.info("100");

                            target.getInventory().setItem(target.getInventory().firstEmpty(), complex);

                            target.sendMessage(ChatColor.GREEN + "You have recieved a" + ChatColor.WHITE + " 'Complex supply chest'");

                        }

                    }else{

                        Log.info("100");

                    target.getInventory().addItem(complex);

                    target.sendMessage(ChatColor.GREEN + "You have recieved a" + ChatColor.WHITE + " 'Complex supply chest'");

                    }

                }

            }

            }
    My main class:
    Code (Text):

    Plain pCrate = new Plain();
    getCommand("ComplexCrate").setExecutor(pCrate);

    My plugin.yml:
    Code (Text):

       ComplexCrate:

            usage: /<command>

            aliases: [cc, complex]

            permission: spartancraft.complexcrate

            description: Complex Crate.
     
    I know it is probably a stupid mistake but I cannot see it. If someone else can then please let me know! Thanks.
     
  2. None, all it does is return the command I put in so if I type /complexcrate it sends me a message "/complexcrate"
     
  3. I tested myself. It works on my server:

    Code (Text):
    name: Test
    version: 1.0
    author: MultiworldLP
    main: de.multi.test.main.Main

    commands:
      ComplexCrate:
    Main

    Code (Text):
    package de.multi.test.main;

    import me.multi.test.commands.Test;

    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {

        public void onEnable() {
            getCommand("ComplexCrate").setExecutor(new Test());
        }
    }
     
    Test

    Code (Text):
    package me.multi.test.commands;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.libs.jline.internal.Log;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class Test implements CommandExecutor {

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String arg2, String[] args) {
           
             if(cmd.getName().equalsIgnoreCase("ComplexCrate")){

                    Log.info("100");

                    Player player = (Player) sender;

                    if(!player.hasPermission("spartancraft.complexcrate")){

                        Log.info("100");

                        player.sendMessage(ChatColor.RED + "You do not have permission for this crate!");

                    }else{

                        Log.info("100");

                        if(args.length == 0){

                            player.sendMessage(ChatColor.RED + "Please specify a player to give the crate to!");

                        }else{

                            Log.info("100");

                        Player target = Bukkit.getServer().getPlayer(args [0]);

                        ItemStack complex = new ItemStack(Material.CHEST, 1);

                        ItemMeta complexMeta = complex.getItemMeta();

                        complexMeta.setDisplayName(ChatColor.GREEN + "Complex supply chest " + ChatColor.GRAY + "(right click to open)");

                        ArrayList<String> lore2 = new ArrayList<String>();

                        lore2.add(ChatColor.RED + "The most complex of all supplies");

                        complexMeta.setLore(lore2);

                        complex.setItemMeta(complexMeta);

                     

                        if(target.getInventory().contains(complex)){

                            Log.info("100");

                            if(target.getInventory().firstEmpty()==-1){

                                target.sendMessage(ChatColor.RED + "Inventory is full you cannot give you that item.");

                            }else{

                                Log.info("100");

                                target.getInventory().setItem(target.getInventory().firstEmpty(), complex);

                                target.sendMessage(ChatColor.GREEN + "You have recieved a" + ChatColor.WHITE + " 'Complex supply chest'");

                            }

                        }else{

                            Log.info("100");

                        target.getInventory().addItem(complex);

                        target.sendMessage(ChatColor.GREEN + "You have recieved a" + ChatColor.WHITE + " 'Complex supply chest'");

                        }

                    }

                }

                }
            return false;
        }
    }
     
     
  4. hmmmm, strange, I will see if it will work in a different class.
     
  5. Different classes work too ;) Write pls the same commandline, like me in plugin.yml. Without usage etc. and test it than!
     
  6. Ok, I found the problem, the whole of that project was just bugged. I copied and pasted it to another project and it works. I don't know what the hell went on in that project. Thanks a lot for all your help as well!
     
  7. Well, I noticed your command thingy doesn't have a return statement... it should "return true" to get rid of the message.
     
  8. rewrote your code, { } use them carefully.