Why can i not give someone creeper eggs with a Command?

Discussion in 'Spigot Plugin Development' started by Astrobabby, Jun 2, 2018.

  1. I just get an Error if i use this:
    if (cmd.getName().equalsIgnoreCase("CreeperEgg")) {

    ItemStack MonsterEgg = new ItemStack(Material.MONSTER_EGG, 1,(short) 50);

    ItemMeta MonsterEggmeta = MonsterEgg.getItemMeta();

    MonsterEggmeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1, true);

    MonsterEggmeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);

    MonsterEggmeta.setDisplayName("§eMonsterEgg");

    MonsterEgg.setItemMeta(MonsterEggmeta);

    p.getInventory().addItem(MonsterEgg);

    }

    If i use this i would not get it end just get the error message.
    Can someone help??
     
  2. And the error is?..........................
     
  3. what does the console say?
     
  4. I'm going to assume you didn't register the command properly.

    Here I create the command class.
    Code (Java):

    public class CreeperEgg implements CommandExecutor {
        Main plugin;
        public CreeperEgg(Main instance) {
            this.plugin = instance;
        }

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("creeperegg")) {
                ItemStack me = new ItemStack(Material.MONSTER_EGG, 1, (byte) 50);
                ItemMeta mem = me.getItemMeta();
                mem.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1, true);
                mem.addItemFlags(ItemFlag.HIDE_ENCHANTS);
                mem.setDisplayName(ChatColor.YELLOW + "MonsterEgg");
                me.setItemMeta(mem);
                p.getInventory().addItem(me);
            }
            return true;
        }
    }
     
    In the main class I create a method called registerCommands() and call it in my onEnable() function.
    Code (Java):

        public void registerCommands() {
            getCommand("creeperegg").setExecutor(new CreeperEgg(this));
        }
     
    In my plugin.yml I register the command.
    Code (yml (Unknown Language)):

    commands:
        creeperegg:
            description: Spawn a creeper egg.
            usage: /creeperegg
     
    If you follow these steps /creeperegg should give the player whatever itemstack is within the class.
    I recommend that you check if the sender is an instance of player and return false if not. This will prevent any errors from popping up in the console if the sender isn't a player.
     
  5. Maybe you have to use SpawnEggMeta instead of ItemMeta to set EntityType to creeper.
     
    • Agree Agree x 1
  6. You're doing Command#getName(). To access what the user typed in as a command, refer to the string "label".

    In addition, the way to change what type of mob the egg spawns is by altering the itemstack's durability. The durability acts like the subtype of an item. Setting a wool block's durability to 2, for example, makes the block turn orange. For a creeper egg, set the item's durability to 50.