Throwing Item as projectile - Damage

Discussion in 'Plugin Development' started by FlaveDrake, Jan 20, 2018.

Thread Status:
Not open for further replies.
  1. Hello,

    I want to make throwing like "knives" :

    So i just made this code

    Code:java
    1.  
    2. @EventHandler
    3. public void onInteract(PlayerInteractEvent e) {
    4. Player p = e.getPlayer();
    5. ItemStack i = p.getItemInHand();
    6.  
    7. if (i.getType() == Material.IRON_SWORD && i != null) {
    8. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    9. p.getInventory().remove(i);
    10. Item dropped = p.getWorld().dropItem(p.getLocation(), i);
    11. if (p.isSneaking()) {
    12. dropped.setVelocity(p.getLocation().getDirection().normalize().multiply(0.75));
    13. } else if (p.isSprinting()) {
    14. dropped.setVelocity(p.getLocation().getDirection().normalize().multiply(1.75));
    15. } else {
    16. dropped.setVelocity(p.getLocation().getDirection().normalize().multiply(1.25));
    17. }
    18. }
    19. } else {
    20. return;
    21. }
    22. }
    23.  


    This works properly, and now to my question:


    How I can i do Damage on hitting an Entity, if i've thrown the "knife".
     
  2. Offline

    Zombie_Striker

    @FlaveDrake
    1. Create a BukkitRunnable. Make it so it runs every couple of ticks.
    2. Inside the task, check if there are any nearby entities within a 1 block radius.
    3. If so, damage them and remove the dropped item
    4. If not, check if the item is on the ground, and if so, cancel the task.
     
Thread Status:
Not open for further replies.

Share This Page