Solved I have a bug with the damage of throwing shuriken!

Discussion in 'Plugin Development' started by Jiraiz, Aug 25, 2013.

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

    Jiraiz

    I tried to make a throw shuriken, and I did.
    But, there is a problem, the problem is another player does not get damage from a shuriken.

    My listener code:

    Code:java
    1. public class MyPlayerListener implements Listener {
    2.  
    3. public static Test plugin;
    4.  
    5. public MyPlayerListener(Test instance) {
    6. plugin = instance;
    7. }
    8.  
    9. Player shurikenPlayer;
    10.  
    11. @SuppressWarnings("deprecation")
    12. @EventHandler
    13. public void onPlayerInteract(PlayerInteractEvent event) {
    14. Player player = event.getPlayer();
    15. int blockId = player.getItemInHand().getType().getId();
    16. if (blockId == 399 && event.getAction() == Action.RIGHT_CLICK_AIR || blockId == 399 && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    17. final Item item = player.getWorld().dropItem(player.getEyeLocation(), new ItemStack(Material.NETHER_STAR));
    18. item.setVelocity(player.getLocation().getDirection().multiply(1.75D));
    19. item.setPickupDelay(Integer.MAX_VALUE);
    20. player.getInventory().removeItem(new ItemStack(Material.NETHER_STAR, 1));
    21. player.updateInventory();
    22. item.getWorld().playSound(item.getLocation(), Sound.STEP_SNOW, 5, 5);
    23. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    24.  
    25. @Override
    26. public void run() {
    27. item.remove();
    28. item.getWorld().playSound(item.getLocation(), Sound.DIG_GRAVEL, 1, 1);
    29. }
    30. }, 20);
    31. }
    32. }
    33.  
    34. @EventHandler
    35. public void onPlayerMove(PlayerMoveEvent event) {
    36. Player player = event.getPlayer();
    37. for (Entity entity : player.getNearbyEntities(1, 1, 1)) {
    38. if (entity.getType() == EntityType.DROPPED_ITEM) {
    39. if (entity instanceof Item) {
    40. if (((Item) entity).getItemStack().getType() == Material.NETHER_STAR) {
    41. if (player != null) {
    42. shurikenPlayer.damage(4.0D);
    43. }
    44. }
    45. }
    46. }
    47. }
    48. }


    Thanks for who try to help me!
     
  2. Offline

    Conarnar

    this
    Code:java
    1. if (player != null) {
    2. shurikenPlayer.damage(4.0D);
    3. }

    replace it with
    Code:java
    1. player.damage(4.0D);
     
  3. Offline

    Jiraiz

    Conarnar Another player receives damage, but I also get damage when I move and throw.
     
  4. Offline

    Conarnar

    Instead of using PlayerMoveEvent make a BukkitRunnable that keeps track of the shuriken and detects players near it and damage them. Add a delay in the beginning so that You don't get damaged if you throw it.
     
  5. Offline

    Jiraiz

    Conarnar Can you make for me a code example, not that I understood you, sorry.
     
  6. Offline

    Conarnar

    Code:java
    1. new BukkitRunnable() {
    2. public void run {
    3. for (Entity entity: item.getNearbyEntities(1, 1, 1)) {
    4. if (entity.getType() == EntityType.PLAYER) {
    5. item.remove();
    6. ((Player) entity).damage(4.0D);
    7. cancel();
    8. return;
    9. }
    10. }
    11. }
    12. }.runTaskTimer(plugin, 2, 1);
    13.  


    put it after you spawn the item
     
  7. Offline

    Jiraiz

    Conarnar Now when a player throws a shuriken as he moved forward with the throw, he gets damage, the shuriken that hits it a bit if I'm not mistaken, but it gets damage.
    So how do we fix it?

    My code now:

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onPlayerInteract(PlayerInteractEvent event) {
    4. Player player = event.getPlayer();
    5. int blockId = player.getItemInHand().getType().getId();
    6. if (blockId == 399 && event.getAction() == Action.RIGHT_CLICK_AIR || blockId == 399 && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    7. final Item item = player.getWorld().dropItem(player.getEyeLocation(), new ItemStack(Material.NETHER_STAR));
    8. item.setVelocity(player.getLocation().getDirection().multiply(1.75D));
    9. item.setPickupDelay(Integer.MAX_VALUE);
    10. player.getInventory().removeItem(new ItemStack(Material.NETHER_STAR, 1));
    11. player.updateInventory();
    12. item.getWorld().playSound(item.getLocation(), Sound.STEP_SNOW, 5, 5);
    13. new BukkitRunnable() {
    14.  
    15. @Override
    16. public void run() {
    17. for (Entity entity: item.getNearbyEntities(1, 1, 1)) {
    18. if (entity.getType() == EntityType.PLAYER) {
    19. ((Player) entity).damage(4.0D);
    20. cancel();
    21. return;
    22. }
    23. }
    24. }
    25. }.runTaskTimer(plugin, 2, 1);
    26. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    27.  
    28. @Override
    29. public void run() {
    30. item.remove();
    31. item.getWorld().playSound(item.getLocation(), Sound.DIG_GRAVEL, 1, 1);
    32. }
    33. }, 20);
    34. }
    35. }
     
  8. Offline

    ISHLCMinecraft

    Actualy the player will be killed.
    When you damaging the player remove the item drop and in the second runable just check if the item exist
     
  9. Offline

    creatorfromhell

    Try replacing this bit:
    Code:java
    1. for (Entity entity: item.getNearbyEntities(1, 1, 1)) {
    2. if (entity.getType() == EntityType.PLAYER) {
    3. ((Player) entity).damage(4.0D);
    4. cancel();
    5. return;
    6. }
    7. }

    with this:
    Code:java
    1. for (Entity entity: item.getNearbyEntities(1, 1, 1)) {
    2. if (entity.getType() == EntityType.PLAYER) {
    3. if((Player) entity != player) {
    4. ((Player) entity).damage(4.0D);
    5. }
    6. cancel();
    7. return;
    8. }
    9. }
     
  10. Offline

    Jiraiz

  11. Offline

    creatorfromhell

Thread Status:
Not open for further replies.

Share This Page