Solved Getting a dropped thrown item to go in a straight line

Discussion in 'Plugin Development' started by Blackwing_Forged, Sep 1, 2017.

Thread Status:
Not open for further replies.
  1. I have an item that when someone right clicks it drops the item then shoots it forward, i just can't make it go straight ive looked on every forum about this and nothing seemed to help, this is the closest ive gotten
    Code:
                    Item shuriken = p.getWorld().dropItem(p.getEyeLocation(), itemThrown);
                    shuriken.setVelocity(p.getEyeLocation().getDirection().multiply(2));
                   
                    BukkitRunnable br = new BukkitRunnable()
                    {
                      public void run()
                      {
                          if(shuriken.getLocation().getBlock().getType().isSolid())
                          {
                              cancel();
                          }
                         
                          for (Entity entity : shuriken.getNearbyEntities(1.0D, 1.0D, 1.0D))
                            {
                                if (!entity.equals(p) && entity instanceof LivingEntity)
                                {
                                    LivingEntity hit = (LivingEntity) entity;
                                    hit.damage(damage);
                                    cancel();
                                }
                            }
                      }
                    };
                    br.runTaskTimer(pl, 1L, 1L);
                    shuriken.remove();
     
  2. Offline

    leduyquang753

    You can get the item's vector and mutiply it when a player drops it.
     
  3. Offline

    Horsey

    Get the direction the player is looking at, multiply it and set the velocity of the item to it.
     
  4. @Horsey @leduyquang753
    Isn't that what im doing here?

    Item shuriken = p.getWorld().dropItem(p.getEyeLocation(), itemThrown);
    shuriken.setVelocity(p.getEyeLocation().getDirection().multiply(2));
     
  5. Online

    timtower Administrator Administrator Moderator

  6. Offline

    Horsey

  7. @Horsey
    It doesn't even go 1 block then dissapears

    Full Code:

    Code:
    @EventHandler
        public void onThrowShuriken(PlayerInteractEvent e)
        {
            Player p = e.getPlayer();
            ItemStack itemThrown = p.getInventory().getItemInMainHand();
            double damage = pl.getConfig().getDouble("Shuriken.damage");
           
            if(pl.getConfig().get("Shuriken.item").equals(itemThrown.getType().toString()))
            {
                if(itemThrown.getAmount() > 1)
                {
                    itemThrown.setAmount(itemThrown.getAmount() - 1);
                   
                }
                else
                {
                    p.getInventory().remove(itemThrown);
                }
               
                Item shuriken = p.getWorld().dropItem(p.getEyeLocation(), itemThrown);
               
                BukkitRunnable br = new BukkitRunnable()
                {
                  public void run()
                  {
                      shuriken.setVelocity(p.getEyeLocation().getDirection().multiply(2));
                     
                      if(shuriken.getLocation().getBlock().getType().isSolid())
                      {
                          cancel();
                      }
                     
                      for (Entity entity : shuriken.getNearbyEntities(1.0D, 1.0D, 1.0D))
                        {
                            if (!entity.equals(p) && entity instanceof LivingEntity)
                            {
                                LivingEntity hit = (LivingEntity) entity;
                                hit.damage(damage);
                                cancel();
                            }
                        }
                  }
                };
                br.runTaskTimer(pl, 1L, 1L);
                shuriken.remove();
            }
        }
     
  8. Offline

    LRK

    This code worked for me:
    Code:
     
    ItemStack modified = item.clone();
    modified.setAmount(1);
    Item thrownItem = p.getWorld().dropItem(p.getLocation(), modified);
    
    thrownItem.setVelocity(p.getEyeLocation().add(0, 1, 0).getDirection().multiply(2));
    
    You can remove that #add(0,1,0) if you want
     
  9. I changed it so its this and still doesn't work, it just goes in my legs then disappears
    Code:
    if(e.getAction().equals(Action.RIGHT_CLICK_AIR))
            {
                if(pl.getConfig().get("Shuriken.item").equals(itemThrown.getType().toString()))
                {
                    if(itemThrown.getAmount() > 1)
                    {
                        itemThrown.setAmount(itemThrown.getAmount() - 1);
                       
                    }
                    else
                    {
                        p.getInventory().remove(itemThrown);
                    }
                   
                    ItemStack modified = itemThrown.clone();
                    modified.setAmount(1);
                    Item thrownItem = p.getWorld().dropItem(p.getLocation(), modified);
    
                   
                   
                    BukkitRunnable br = new BukkitRunnable()
                    {
                      public void run()
                      {
                          thrownItem.setVelocity(p.getEyeLocation().getDirection().multiply(2));
                          if(thrownItem.getLocation().getBlock().getType().isSolid())
                          {
                              cancel();
                          }
                         
                          for (Entity entity : thrownItem.getNearbyEntities(1.0D, 1.0D, 1.0D))
                            {
                                if (!entity.equals(p) && entity instanceof LivingEntity)
                                {
                                    LivingEntity hit = (LivingEntity) entity;
                                    hit.damage(damage);
                                    cancel();
                                }
                            }
                      }
                    };
                    br.runTaskTimer(pl, 1L, 1L);
                    thrownItem.remove();
                }
            }
     
  10. Offline

    LRK

    Are you using 1.9+?
     
  11. Offline

    LRK

    Ok i tested a bit and solved your problem:
    The problem was that the Bukkit Scheduler is Async. That means that the thrownItem#remove Method is called immediately after the item is dropped so I modified your code and ended up with this:
    Code:
    // Interact Event....
    
    BukkitRunnable br = new BukkitRunnable() {
    public void run() {
    thrownItem.setVelocity(p.getEyeLocation().getDirection().multiply(2)); // Set velocity
    
    if (thrownItem.getLocation().getBlock().getType().isSolid()) { // Here you could check something like isOnGround too
    thrownItem.remove();
    cancel();
    }
    for (Entity entity : thrownItem.getNearbyEntities(1.0D, 1.0D, 1.0D)) {
    if (!entity.equals(p) && entity instanceof LivingEntity) {
    LivingEntity hit = (LivingEntity) entity;
    hit.damage(1);
    thrownItem.remove();
    cancel();
    }
    }
    }
    };
    br.runTaskTimer(this, 1L, 1L);
    
    Maybe you can do something like thrownItem#isOnGround and if so remove the item.
     
  12. Thanks ill test it in a bit and tell you if it works or not

    Edit: Thank you that worked
     
    Last edited: Sep 4, 2017
Thread Status:
Not open for further replies.

Share This Page