Setting Creature Velocity

Discussion in 'Plugin Development' started by yewtree8, Jun 12, 2014.

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

    yewtree8

    So I am trying to make it so that on an event it spawns a mob and sets its velocity, at the moment is spawns the squid but does not set its velocity, I have tried changin it to entitytype and creaturetype but nothing works, any pointers?

    Code:java
    1. private ItemStack setMeta(ItemStack material, String name, String lore) {
    2. ItemStack i = new ItemStack(material);
    3. ItemMeta im = i.getItemMeta();
    4. im.setDisplayName(name);
    5. im.setLore(Arrays.asList(lore));
    6. i.setItemMeta(im);
    7. return i;
    8.  
    9. }
    10.  
    11. private ItemStack BatBlaster = setMeta(new ItemStack(Material.IRON_BARDING), (ChatColor.GOLD
    12. + "Squid Blaster!"), (ChatColor.DARK_GREEN + "Right Click To Fire A Squid!"));
    13.  
    14.  
    15. private List<Player> NoBat = new ArrayList<Player>();
    16.  
    17.  
    18.  
    19. @EventHandler
    20. public void onPlayerSquidtLaunch(PlayerInteractEvent event) {
    21.  
    22. Player player = event.getPlayer();
    23. Mob squid = Mob.SQUID;
    24.  
    25.  
    26. if (squid == null)
    27. {
    28. return;
    29. }
    30.  
    31. if(!(player.getItemInHand().equals(BatBlaster)) && (event.getAction() == Action.RIGHT_CLICK_AIR)) return;
    32. if(player.getItemInHand().equals(BatBlaster) && (event.getAction() == Action.RIGHT_CLICK_AIR)) {
    33. if(NoBat.contains(player)) {
    34. player.sendMessage(ChatColor.RED + "You Must Wait 10 Seconds Before Using The Bat Blaster.");
    35. return;
    36. }
    37.  
    38.  
    39. player.getLocation().getWorld().spawn(player.getLocation(), Squid.class).setVelocity(player.getLocation().getDirection().multiply(2));
    40.  
    41.  
    42.  
    43. }
    44.  
    45.  
    46.  
    47. }
     
  2. Try instead of
    player.getLocation().getDirection()
    player.getEyeLocation().getDirection()
     
  3. Offline

    yewtree8

    Bram0101

    Nope it doesn't work :( I've tried loads
     
  4. Maybe try to spawn it, and wait a tick and then set his velocity
     
  5. Offline

    yewtree8

    Bram0101

    How would I do that? make a delayed task? And I don't think I can seperate the line, it needs to be all on one line ¬_¬
     
  6. This is an example, I don't know a 100% if it works.
    <syntax=java>
    final Squid squid = player.getLocation().getWorld().spawn(player.getLocation(), Squid.class);
    Bukkit.getServer().getScheduler();
    scheduler.scheduleSyncDelayedTask(this, new Runnable() {
    @Override
    public void run() {
    squid.setVelocity(player.getEyeLocation().getDirection().multiply(2));
    }
    }, 20L);
    </syntax>
     
  7. Offline

    xTigerRebornx

    yewtree8 Look at these two lines of code then think about it for a second.
    Code:
    if(!(player.getItemInHand().equals(BatBlaster)) && (event.getAction() == Action.RIGHT_CLICK_AIR)) return;
    if(player.getItemInHand().equals(BatBlaster) && (event.getAction() == Action.RIGHT_CLICK_AIR)) {
     
  8. Offline

    yewtree8

    xTigerRebornx

    I feel like noob D: what is wrong?....

    EDIT: It is called "bat blaster" because it was originally for bats, but I changed it to squids, and if i wanted to change it I would have to go through all 12 class files and that is bleh :(

    @Bram0101

    It doesn't work, event schedulers don't work for me ¬_¬

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
    Official_Spendrew likes this.
  9. Offline

    GeorgeeeHD

    yewtree8 well this line:

    Code:java
    1. if(!(player.getItemInHand().equals(BatBlaster)) && (event.getAction() == Action.RIGHT_CLICK_AIR)) return;


    returns out of the method if the item in the players hand is not a bat blaster and if they right click air. Just remove the check for the action.
     
  10. Offline

    xTigerRebornx

    yewtree8 I read those lines wrong, disregard that comment.
    The lines are still redundant (since you are just checking for the same thing twice)
    But, you shouldn't use ItemStack#equals() for comparison
    http://jd.bukkit.org/rb/apidocs/src-html/org/bukkit/inventory/ItemStack.html#line.302
    It goes through all of the checks shown there, including damage and amount. You'd be better of using ItemStack#isSimilar(), or checking if the ItemMeta of the two stacks are equal
     
Thread Status:
Not open for further replies.

Share This Page