Spawn mob with command

Discussion in 'Plugin Development' started by tylerthecreeper1, Apr 28, 2014.

Thread Status:
Not open for further replies.
  1. I'm having trouble with this :/ It shouldn't be this difficult.. Everything works, I have tried using debug messages and everything. It's just the part that spawns the zombie. When I use '/zombie create test', test being the display name, it just says "/zombie" in white in the chat.

    Command portion of CommandExecutor Class:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. if(sender instanceof Player) {
    3. Player p = (Player) sender;
    4. if(p.isOp()) {
    5. if(cmd.getName().equalsIgnoreCase("zombie")) {
    6. if(args.length == 0) {
    7. p.sendMessage(ChatColor.GOLD + "Zombie GUI Commands:");
    8. p.sendMessage(ChatColor.YELLOW + "/zombie create <name> " + ChatColor.GRAY + "Create a zombie with the given name.");
    9. p.sendMessage(ChatColor.YELLOW + "/zombie remove <name> " + ChatColor.GRAY + "Remove a zombie with the given name.");
    10. p.sendMessage(ChatColor.YELLOW + "/zombie removeall " + ChatColor.GRAY + "Removed all zombies in the current world.");
    11. return true;
    12. }
    13. else if(args.length == 1) {
    14. if(args[0].equalsIgnoreCase("create")) {
    15. p.sendMessage(ChatColor.RED + "Error: " + ChatColor.GOLD + "Usage: /zombie [subcmd] [arg]");
    16. return true;
    17. }
    18. else if(args.length == 2) {
    19. if(args[0].equalsIgnoreCase("create")) {
    20. String zname = args[1];
    21. LivingEntity e = (LivingEntity)p.getWorld().spawnEntity(p.getLocation(), EntityType.ZOMBIE);
    22.  
    23. e.setCanPickupItems(false);
    24. e.setCustomName(ChatColor.translateAlternateColorCodes('&', zname));
    25. e.setCustomNameVisible(true);
    26. e.setRemoveWhenFarAway(false);
    27.  
    28. e.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 99999, 6));
    29. e.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 99999, 6));
    30.  
    31. ItemStack bhat = new ItemStack(Material.LEATHER_HELMET);
    32. LeatherArmorMeta bmeta = (LeatherArmorMeta)bhat.getItemMeta();
    33. bmeta.setColor(Color.BLUE);
    34. bhat.setItemMeta(bmeta);
    35. ItemStack bchest = new ItemStack(Material.LEATHER_CHESTPLATE);
    36. LeatherArmorMeta bmeta2 = (LeatherArmorMeta)bchest.getItemMeta();
    37. bmeta2.setColor(Color.BLUE);
    38. bchest.setItemMeta(bmeta2);
    39. ItemStack blegs = new ItemStack(Material.LEATHER_CHESTPLATE);
    40. LeatherArmorMeta bmeta3 = (LeatherArmorMeta)blegs.getItemMeta();
    41. bmeta3.setColor(Color.BLUE);
    42. blegs.setItemMeta(bmeta3);
    43. ItemStack bboots = new ItemStack(Material.LEATHER_CHESTPLATE);
    44. LeatherArmorMeta bmeta4 = (LeatherArmorMeta)bboots.getItemMeta();
    45. bmeta4.setColor(Color.BLUE);
    46. bboots.setItemMeta(bmeta4);
    47.  
    48. e.getEquipment().setHelmet(new ItemStack(bhat));
    49. e.getEquipment().setChestplate(new ItemStack(bchest));
    50. e.getEquipment().setLeggings(new ItemStack(blegs));
    51. e.getEquipment().setBoots(new ItemStack(bboots));
    52.  
    53. String name = e.getCustomName();
    54. p.sendMessage(ChatColor.GREEN + "Zombie created at your location with display name: " + ChatColor.YELLOW + name);
    55. return true;
    56. }
    57. }
    58. }
    59. }
    60. }
    61. else {
    62. p.sendMessage(ChatColor.RED + "You must be OP to use this command!");
    63. return true;
    64. }
    65. }
    66. return false;
    67. }


    onEnable() :
    Code:java
    1. @Override
    2. public void onEnable() {
    3. log("Enabled.");
    4. getCommand("zombie").setExecutor(new CmdCreate());


    Any help will be great. Thanks in advance.
     
  2. Offline

    CraftedShack

    Are you executing the command from the console?
     
  3. CraftedShack Nope.. In game.

    CraftedShack '/zombie create <name>' is suppose to create a zombie at my location with the name "<name>.
    It just returns '/zombie' in chat, like in this screenshot.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    ThunderWaffeMC

    You need to register your command in your main class. You may have it in your plugin.yml but you need to add this to your onEnable:

    Code:java
    1.  
    2. PluginManager manager = getServer().getPluginManager();
    3. manager.registerEvents(new ListenerClass(this), this);
    4.  
     
  5. ThunderWaffeMC It's not an event though, it's in the command executor class.. I'm spawning the zombie with a command, /zombie create <name>. Giving it a custom display name.
     
  6. Offline

    ThunderWaffeMC

    Sorry. I didn't mean to register an event. Register the command*

    Code:java
    1.  
    2. getCommand("mycommand").setExecutor(new CommandClass(this));
    3.  
     
  7. ThunderWaffeMC Yes, I did that :/ Can you add me on skype? My skype is tylerthecreeper1
     
  8. Bingo. You messed up the indentation. I've got it fixed for you here:
    Code:java
    1. publicboolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. if (sender instanceof Player) {
    3. Player p = (Player) sender;
    4. if (p.isOp()) {
    5. if (cmd.getName().equalsIgnoreCase("zombie")) {
    6. if (args.length == 0) {
    7. p.sendMessage(ChatColor.GOLD + "Zombie GUI Commands:");
    8. p.sendMessage(ChatColor.YELLOW + "/zombie create <name> " + ChatColor.GRAY + "Create a zombie with the given name.");
    9. p.sendMessage(ChatColor.YELLOW + "/zombie remove <name> " + ChatColor.GRAY + "Remove a zombie with the given name.");
    10. p.sendMessage(ChatColor.YELLOW + "/zombie removeall " + ChatColor.GRAY + "Removed all zombies in the current world.");
    11. returntrue;
    12. } else if (args.length == 1) {
    13. if (args[0].equalsIgnoreCase("create")) {
    14. p.sendMessage(ChatColor.RED + "Error: " + ChatColor.GOLD + "Usage: /zombie [subcmd] [arg]");
    15. returntrue;
    16. }
    17. } else if (args.length == 2) {
    18. if (args[0].equalsIgnoreCase("create")) {
    19. String zname = args[1];
    20. LivingEntity e = (LivingEntity) p.getWorld().spawnEntity(p.getLocation(), EntityType.ZOMBIE);
    21.  
    22. e.setCanPickupItems(false);
    23. e.setCustomName(ChatColor.translateAlternateColorCodes('&', zname));
    24. e.setCustomNameVisible(true);
    25. e.setRemoveWhenFarAway(false);
    26.  
    27. e.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 99999, 6));
    28. e.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 99999, 6));
    29.  
    30. ItemStack bhat = new ItemStack(Material.LEATHER_HELMET);
    31. LeatherArmorMeta bmeta = (LeatherArmorMeta) bhat.getItemMeta();
    32. bmeta.setColor(Color.BLUE);
    33. bhat.setItemMeta(bmeta);
    34. ItemStack bchest = new ItemStack(Material.LEATHER_CHESTPLATE);
    35. LeatherArmorMeta bmeta2 = (LeatherArmorMeta) bchest.getItemMeta();
    36. bmeta2.setColor(Color.BLUE);
    37. bchest.setItemMeta(bmeta2);
    38. ItemStack blegs = new ItemStack(Material.LEATHER_CHESTPLATE);
    39. LeatherArmorMeta bmeta3 = (LeatherArmorMeta) blegs.getItemMeta();
    40. bmeta3.setColor(Color.BLUE);
    41. blegs.setItemMeta(bmeta3);
    42. ItemStack bboots = new ItemStack(Material.LEATHER_CHESTPLATE);
    43. LeatherArmorMeta bmeta4 = (LeatherArmorMeta) bboots.getItemMeta();
    44. bmeta4.setColor(Color.BLUE);
    45. bboots.setItemMeta(bmeta4);
    46.  
    47. e.getEquipment().setHelmet(new ItemStack(bhat));
    48. e.getEquipment().setChestplate(new ItemStack(bchest));
    49. e.getEquipment().setLeggings(new ItemStack(blegs));
    50. e.getEquipment().setBoots(new ItemStack(bboots));
    51.  
    52. String name = e.getCustomName();
    53. p.sendMessage(ChatColor.GREEN + "Zombie created at your location with display name: " + ChatColor.YELLOW + name);
    54. returntrue;
    55. }
    56. }
    57. }
    58. } else {
    59. p.sendMessage(ChatColor.RED + "You must be OP to use this command!");
    60. returntrue;
    61. }
    62. }
    63. returnfalse;
    64. }

    (There were too many indentation mistakes to point them all out)
     
  9. DJSkepter Thanks a lot, I'll try it and let you know :)

    DJSkepter Works, thanks !

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  10. No problem - indentation isn't a simple topic. In the end, your previous indentation forgot about the isOp part and went straight to return false, where it sends the default command message. Happy coding :D
     
    tylerthecreeper1 likes this.
Thread Status:
Not open for further replies.

Share This Page