Spawning mobs

Discussion in 'Plugin Development' started by bkleinman1, Sep 8, 2013.

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

    bkleinman1

    How would i make a plugin so that you could do a command such as '/stickspawn zombie' and then when i right clicked with a stick, it would spawn a zombie on the location of the click.

    Note: I dont want it just to be able to spawn zombies, i want the user to decide what mob by doing the /stickspawn command.

    Thanks.
     
  2. Offline

    sgavster

    this is untested. this is what I came up with:
    Code:java
    1. package me.sgavster.TestPlugin;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.World;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.EntityType;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.player.PlayerInteractEvent;
    16. import org.bukkit.inventory.ItemStack;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class SpawnMob extends JavaPlugin implements Listener
    20. {
    21. public static SpawnMob plugin;
    22.  
    23. @Override
    24. public void onEnable()
    25. {
    26.  
    27. }
    28.  
    29. @Override
    30. public void onDisable()
    31. {
    32.  
    33. }
    34.  
    35. public ArrayList<String> z = new ArrayList<String>();
    36.  
    37. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    38. {
    39. if (cmd.getName().equalsIgnoreCase("stickspawn"))
    40. {
    41. if (args.length == 0)
    42. {
    43. sender.sendMessage("Specify a mob.");
    44. }
    45. else if (sender instanceof Player)
    46. {
    47. Player p = (Player)sender;
    48. if(args.length == 1)
    49. {
    50. if(args[0].equalsIgnoreCase("zombie"))
    51. {
    52. p.getInventory().addItem(new ItemStack(Material.STICK));
    53. p.sendMessage("You can now spawn zombies with a stick.");
    54. z.add(p.getName());
    55. }
    56. }
    57. }
    58. }
    59. return false;
    60. }
    61.  
    62. @SuppressWarnings("deprecation")
    63. @EventHandler
    64. public void onInteract(PlayerInteractEvent e)
    65. {
    66. Player p = e.getPlayer();
    67. Location loc = p.getLocation();
    68. World w = p.getWorld();
    69. if(p.getItemInHand().getType() == Material.STICK)
    70. {
    71. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
    72. {
    73. if(z.contains(p.getName()))
    74. {
    75. w.spawnCreature(loc, EntityType.ZOMBIE);
    76. }
    77. }
    78. }
    79. }
    80. }
    81.  

    probably not the most efficient way.
     
  3. Offline

    xTrollxDudex

  4. Offline

    bkleinman1

    sgavster
    I tried it, it doesnt seem to work. im not really sure why as the console isnt giving off any errors.

    xTrollxDudex
    yes but i need it to only spawn if the player has done the command
     
  5. Offline

    xTrollxDudex

  6. Offline

    bkleinman1

    @xTrollxDudex
    but i need it to only spawn the mob when someone right clicks with a stick and has a mob set with the command
     
  7. Offline

    bobacadodl

    You never registered events
     
  8. Offline

    Chinwe

    If you want each player to have a unique entitytype that they spawn, you'll need a Map that stores <String, EntityType> - playername and entitytype. You can get the EntityType using EntityType.valueOf(args[0]) - then listen to PlayerInteractEvent, and if the player is holding a stick, and the Map contains their name, spawn their EntityType at their target block :)
     
    bkleinman1 likes this.
  9. Offline

    bkleinman1

    Chinwe Thanks, i will try it now.

    @Chinwe how do i use the getTargetBlock?

    EDIT: I have found out how, but how would i get the value of the EntityType? would it be somthing like map.get(EntityType);?

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

    Chinwe

    bkleinman1
    map.get(player.getName()) will return the value (EntityType) for that player :)
     
  11. Offline

    bkleinman1

    @Chinwe what would the last line of code be, i tried both:
    Code:java
    1. world.spawnEntity(player.getTargetBlock(null, 50), map.get(player.getName()));

    Code:java
    1. world.spawn(player.getTargetBlock(null, 50), map.get(player.getName()));

    and it gave me an error on spawn/spawnEntity

    PS: i noticed your on 99 likes, if you help me il get you to 100 :p
     
  12. Offline

    Chinwe

    Oh ho ho, shiet just got serious :)

    getTargetBlock() returns a Block, so you need to use .getLocation(), as .spawnEntity() takes a Location and EntityType ;)

    *waits anxiously*
     
  13. Offline

    bkleinman1

    sorry for the long wait, i was afk, hmm im still getting an error, could you give me the code you would use please?

    Chinwe and i see you already got ur 100 :)

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

    Chinwe

    101 in fact :cool:

    This is what you should have:
    Code:
    world.spawnEntity(player.getTargetBlock(null, 50).getLocation(), map.get(player.getName()));
    That should work assuming that your map is <String,EntityType> :>
     
  15. Offline

    bkleinman1

    Chinwe Still getting an error, here is my code:
    Code:java
    1. public class main extends JavaPlugin {
    2. Logger log = Logger.getLogger("Minecraft");
    3.  
    4. public HashMap <String, EntityType> map = new HashMap <String, EntityType>();
    5.  
    6. @Override
    7. public void onEnable() {
    8. log.info("Stick Spawner has been enabled");
    9. }
    10.  
    11. @Override
    12. public void onDisable() {
    13. log.info("Stick Spawner has been dissabled");
    14. }
    15.  
    16. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    17. Player player = (Player) sender;
    18. if (commandLabel.equalsIgnoreCase("stickspawn")) {
    19. if (args.length == 0) {
    20. player.sendMessage(ChatColor.RED + "[StickSpawner]" + ChatColor.GRAY + "Not enough args: /stickspawn [mob]");
    21. }
    22. if (args.length == 1) {
    23. map.put(player.getName(), EntityType.valueOf(args[0]));
    24.  
    25. }
    26. }return false;
    27. }
    28.  
    29. @EventHandler
    30. public void onInteract(PlayerInteractEvent e) {
    31. Player player = e.getPlayer();
    32. World world = player.getWorld();
    33. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    34. if (player.getItemInHand().getType() == Material.STICK) {
    35. if (map.containsKey(player.getName())) {
    36. world.spawnEntity(player.getTargetBlock(null, 50).getLocation(), map.get(player.getName()));
    37. }
    38. }
    39. }
    40. }
    41. }


    have set my hashmap wrong or something?
     
  16. Offline

    sgavster

    bobacadodl On the onEnable() { do Bukkit.getPluginManager().registerEvents(this, this);
     
  17. Offline

    Chinwe

    What is the error, is it a NullPointerException or IllegalArgumentException? If so, it is because EntityType.valueOf() will throw an exception if an exact match is not found (ie "zombie" != EntityType.ZOMBIE) -> you should try using EntityType.fromName(String), or even make a method to match it, such as:
    Code:
        public static EntityType getEntityType(String s)
        {
            for (EntityType e : EntityType.values())
                if (e.getName().startsWith(s) return e;         
        }
    And register the listener like sgavster said ;)
     
  18. Offline

    bkleinman1

    Chinwe whoa that last reply lost me lol, could you try and explain it in a simpler way, as i tried to add the code you said, and i came up with errors. This is what it says about the error on 'spawnEntitys':
    The method spawnEntity(Location, EntityType) in the type World is not applicable for the arguments (Location, BeanDescriptor.EntityType)
     
  19. Offline

    Chinwe

    bkleinman1
    You imported the wrong EntityType, remove it from your imports list and import the org.bukkit one instead :)

    To get the specific EntityType from the input:
    EntityType.valueOf(string) is very picky, so if you for example pass in "zombie", it will error, because it is also case sensitive :(
    You may need to create your own method like what I posted above, though a player could specify "enderdragon" and spawn them everywhere - create one like this:
    Code:
        public static EntityType getEntityType(String s)
        {
            if (s.equalsIgnoreCase("zombie"))
                return EntityType.ZOMBIE;
            else if (s.equalsIgnoreCase("cow"))
                return EntityType.COW;
            else if (s.equalsIgnoreCase("pig"))
                return EntityType.PIG;
                // ... etc
            else
                return null;
        }
    And then, in your onCommand():
    Code:
    EntityType chosen = getEntityType(args[0]);
            if (chosen == null)
            {
                player.sendMessage("You entered an invalid mob type!");
                return true;
            }
     
            // add to map and continue
     
    bkleinman1 likes this.
  20. Offline

    bkleinman1

    Chinwe Im such an idiot xD Thank you so much, if i could give you 100 likes i would :p
     
  21. Offline

    bobacadodl

    You can also just do EntityType.fromName(String s); if you're lazy ;)

    Or, for better matching, do
    Code:
    public EntityType getClosest(String s){
            EntityType closest=EntityType.PIG;
            int closestDistance = Integer.MAX_VALUE;
            for(EntityType type:EntityType.values()){
                int distance = StringUtils.getLevenshteinDistance(s, type.getName());
                if(distance<closestDistance){
                    closestDistance=distance;
                    closest = type;
                }
            }
            return closest;
        }
    That will give you the closest Entity to whatever they typed in ;) so even if they mis-spell it, it will work.
     
    Chinwe likes this.
  22. Offline

    bkleinman1

    is this just for pig?
     
  23. Offline

    Chinwe

  24. Offline

    woaosodo

    How would you give a mob a name or a special characteristic? I want it so that when I spawn mobs they're classed differently and I can have a command that does something to all of them or something. AKA, how do you select the dye color of a sheep? I have it so that when you right click it spawns a sheep but it must be an EntityType, not an Entity so I don't know how to set custom characteristics to it.
     
Thread Status:
Not open for further replies.

Share This Page