I am rather good at programming now, so I wanted to do something different. I want to make a bat gun. I am having a problem though. I have no clue as to how to spawn a bat and then get it to fly for a little time in the direction you are looking. I am not looking for someone to write the code, but it would be great if they could at least point me in the right direction. Well? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Why, you can find out how at your friendly neighborhood jd.bukkit.org! http://jd.bukkit.org/beta/apidocs/org/bukkit/entity/Entity.html#setVelocity(org.bukkit.util.Vector)
ha ha, of course. Sorry for a pointless question I am having a problem where I can not get the bat I spawned, so that I can not change it velocity. This is my code(simple ) Code:java @EventHandler public void onPlayerInteract(PlayerInteractEvent event){ Player player = event.getPlayer(); if(player.getItemInHand().getType().equals(Material.RED_ROSE)){ Location loc = player.getLocation(); player.getWorld().spawn(loc, Bat.class); } } What am I missing that lets me get the bat just spawned? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Gamesareme To get the bat when you spawn it you should be able to do this: Code:java Bat bat = (Bat) player.getWorld().spawn(loc, Bat.class);
No, he's saying that you need to store the bat you spawn somewhere and then reference it later as you update its velocity.
I am really confused. Code: Bat bat = (Bat) player.getWorld().spawn(loc, Bat.class); this calls spawn in it, I would have thought you would call 'bat' or something like that to spawn it. How can a save the bat I spawned, if I do not know how to spawn it using this code?
You say you already spawned the bat, where did you store it? Can you show the code where you claim to have already done it?
Gamesareme again, the spawn method returns the entity that was spawned, just like the oncommand method returns a boolean. If you still dont understand it, see Java tutorials about how methods work.
Gamesareme You have given the answer yourself already.. That spawn method that you already used, also returns the bat it just spawned. So if you just declare that line you used as a variable, the new bat will spawn + it will be saved. So just use the code above with the cast to the Bat object and tadaa, bat is now your object you can alter, and it represents the bat that just spawned.
Ok, now I understand. Thanks for the answer. Ok now I have another problem. I am able to shoot the bats fine, but I am trying to add them to a hash map so I can delete, after a few seconds. This is what I have. Code: HashMap<Player, Bat> getbat = new HashMap<Player, Bat>(); I try and something to this by using Code: getbat.put(player.getName(), bat.getEntityId()); but this throws up an error where I say bat.getEntityId(). What am I doing wrong? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Gamesareme Your putting in a String object into the key of the HashMap, (player.getName()) when they key is supposed to be a Player object, same goes for your value of the HashMap (Bat object). Also many people have reverted to using UUID's to put into HashMap's, HashSet's, and Lists. Instead of the Player object itself, as it can cause memory leaks (as i have heard).
I'm beginning to think this statement you made is a lie, while reading through some of the posts in this thread.
Ouch that hurts. No I am not ling here. I am rather good at coding, but I have never used these statements before. That is why I said
You can't be "rather good at coding" if you "have never used these statements before". This is one of those times were people will tell you to learn some more Java before proceeding - it will help your understanding a lot and make it much less frustrating.
Gamesareme I'm sorry if I sounded harsh but if you're rather good at programming like you claim you wouldn't ask what you were doing wrong when adding and key and value to a HashMap using the wrong objects. Your IDE should have even been able to suggest to you what you did wrong. Eg above the key is a Player object and the value is the Bat object yet you pass a string and a integer. That's not new "statements" like you claim it's just basic java. Also you should not be storing Player objects like artish1 stated as it can cause memory leaks.
I have never needed these before, because I used the config to save the things I needed. I have made several plugins already with them all veering in difficulty. So I know what I am doing. But I have only used the config, that is why I am not sure about HashMaps. And yes I have gone to the bukkit page on them, but came up with nothing useful as to what I am doing wrong.
Gamesareme For what you are doing wrong both me and artish1 have told you what you need to fix. If your still not to sure about HashMaps: http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html http://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html