Bat gun?

Discussion in 'Plugin Development' started by Gamesareme, Jul 19, 2014.

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

    Gamesareme

    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.
     
    Last edited by a moderator: Jun 9, 2016
  2. Offline

    _LB

    You either wait for EntityAPI to never be released, or constantly alter the bat's velocity/position.
     
  3. Offline

    Gamesareme

    Ok, I have never changes an objects velocity before. Where should I go to find how to do it?
     
  4. Offline

    _LB

  5. Offline

    Gamesareme

    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
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. if(player.getItemInHand().getType().equals(Material.RED_ROSE)){
    5. Location loc = player.getLocation();
    6. player.getWorld().spawn(loc, Bat.class);
    7. }
    8. }

    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.
     
    Last edited by a moderator: Jun 9, 2016
  6. Offline

    dsouzamatt

    Gamesareme To get the bat when you spawn it you should be able to do this:
    Code:java
    1. Bat bat = (Bat) player.getWorld().spawn(loc, Bat.class);
     
  7. Offline

    Gamesareme

    How would you call this? Or do I just replace what I have with that?
     
  8. Offline

    Necrodoom

    Spawn method returns the entity. Use it.
     
  9. Offline

    Gamesareme

    What :confused: ? are you saying place this after my spawn code, and it will become the bat I just spawned?
     
  10. Offline

    _LB

    No, he's saying that you need to store the bat you spawn somewhere and then reference it later as you update its velocity.
     
  11. Offline

    Gamesareme

    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?
     
  12. Offline

    _LB

    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?
     
  13. Offline

    Gamesareme

    Here. I have not been able to save the, that is why I have been asking how.
     
  14. Offline

    Necrodoom

    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.
     
    TwerkinCraft and Garris0n like this.
  15. Offline

    Flamedek

    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.
     
  16. Offline

    Gamesareme

    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.
     
    Last edited by a moderator: Jun 9, 2016
  17. Offline

    Garris0n

    Are you seeing anything wrong here?
     
  18. Offline

    artish1

    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).
     
  19. Offline

    Drew1080

    I'm beginning to think this statement you made is a lie, while reading through some of the posts in this thread.
     
    _LB likes this.
  20. Offline

    Gamesareme

    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
     
  21. Offline

    _LB

    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.
     
  22. Offline

    Drew1080

    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.
     
  23. Offline

    Gamesareme

    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. ;)
     
  24. Offline

    Drew1080

  25. Offline

    Gamesareme

    Thank, that was what I needed. I can understand now what you were saying above.
     
Thread Status:
Not open for further replies.

Share This Page