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.
this is untested. this is what I came up with: Code:java package me.sgavster.TestPlugin; import java.util.ArrayList; import org.bukkit.Location;import org.bukkit.Material;import org.bukkit.World;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.entity.EntityType;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.block.Action;import org.bukkit.event.player.PlayerInteractEvent;import org.bukkit.inventory.ItemStack;import org.bukkit.plugin.java.JavaPlugin; public class SpawnMob extends JavaPlugin implements Listener{ public static SpawnMob plugin; @Override public void onEnable() { } @Override public void onDisable() { } public ArrayList<String> z = new ArrayList<String>(); public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if (cmd.getName().equalsIgnoreCase("stickspawn")) { if (args.length == 0) { sender.sendMessage("Specify a mob."); } else if (sender instanceof Player) { Player p = (Player)sender; if(args.length == 1) { if(args[0].equalsIgnoreCase("zombie")) { p.getInventory().addItem(new ItemStack(Material.STICK)); p.sendMessage("You can now spawn zombies with a stick."); z.add(p.getName()); } } } } return false; } @SuppressWarnings("deprecation") @EventHandler public void onInteract(PlayerInteractEvent e) { Player p = e.getPlayer(); Location loc = p.getLocation(); World w = p.getWorld(); if(p.getItemInHand().getType() == Material.STICK) { if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) { if(z.contains(p.getName())) { w.spawnCreature(loc, EntityType.ZOMBIE); } } } }} probably not the most efficient way.
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
@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
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
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.
@Chinwe what would the last line of code be, i tried both: Code:java world.spawnEntity(player.getTargetBlock(null, 50), map.get(player.getName())); Code:java 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
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*
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.
101 in fact 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> :>
Chinwe Still getting an error, here is my code: Code:java public class main extends JavaPlugin {Logger log = Logger.getLogger("Minecraft"); public HashMap <String, EntityType> map = new HashMap <String, EntityType>(); @Overridepublic void onEnable() {log.info("Stick Spawner has been enabled");} @Overridepublic void onDisable() {log.info("Stick Spawner has been dissabled");} public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {Player player = (Player) sender;if (commandLabel.equalsIgnoreCase("stickspawn")) {if (args.length == 0) {player.sendMessage(ChatColor.RED + "[StickSpawner]" + ChatColor.GRAY + "Not enough args: /stickspawn [mob]");}if (args.length == 1) {map.put(player.getName(), EntityType.valueOf(args[0])); }}return false;} @EventHandlerpublic void onInteract(PlayerInteractEvent e) {Player player = e.getPlayer();World world = player.getWorld();if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {if (player.getItemInHand().getType() == Material.STICK) {if (map.containsKey(player.getName())) {world.spawnEntity(player.getTargetBlock(null, 50).getLocation(), map.get(player.getName()));}}}}} have set my hashmap wrong or something?
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
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)
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
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.
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.