Bow/Arrow Cancel

Discussion in 'Plugin Development' started by CrazymanJR, Sep 18, 2014.

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

    CrazymanJR

    I'm trying to cancel the event when a bow is shot at there teammate (same team). I cancelled it when you pvp them but i can't figure out how to cancel it when your shooting a bow at them.

    My Current Code:
    Code:java
    1. @EventHandler
    2. public void onHit(EntityDamageByEntityEvent event) {
    3. Player player = (Player) event.getEntity();
    4. Player damager = (Player) event.getDamager();
    5.  
    6. if (GameState.isState(GameState.IN_GAME)) {
    7.  
    8. if (damager instanceof Player && player instanceof Player) {
    9. if (President.getPresidents().isPresidentPlaying(player) && President.getSecretService().isSecretServicePlaying(damager)) {
    10. event.setCancelled(true);
    11. damager.sendMessage(ChatColor.RED + "You can not hit your own teammates!");
    12. } else if (President.getSecretService().isSecretServicePlaying(player) && President.getPresidents().isPresidentPlaying(damager)) {
    13. event.setCancelled(true);
    14. damager.sendMessage(ChatColor.RED + "You can not hit your own teammates!");
    15. } else if (President.getTerrorists().isTerroristPlaying(player) && President.getTerrorists().isTerroristPlaying(damager)) {
    16. event.setCancelled(true);
    17. damager.sendMessage(ChatColor.RED + "You can not hit your own teammates!");
    18. }
    19. }
    20. }
    21.  
    22. }
     
  2. Offline

    IkBenHarm

    you can het the shooter of the bow so what you'd do it like this:

    Code:java
    1. else if(e.getDamager() instanceof Arrow && e.getEntity() instanceof Player){
    2. Arrow aDamager = (Arrow) e.getDamager();
    3. if(aDamager.getShooter() instanceof Player){
    4. Player damager = (Player) aDamager.getShooter();
    5. Player taker = (Player) e.getEntity();
     
  3. Offline

    CrazymanJR

    IkBenHarm
    Thanks, going to test it out :)

    IkBenHarm
    Doesn't work, and it give me an error.
    Current Code:
    Code:java
    1. package me.crazymanjr.president.listeners;
    2.  
    3. import me.crazymanjr.president.President;
    4. import me.crazymanjr.president.utils.GameState;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Arrow;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    12.  
    13. public class PlayerHit implements Listener {
    14.  
    15. @SuppressWarnings("deprecation")
    16. @EventHandler
    17. public void onHit(EntityDamageByEntityEvent event) {
    18. Player player = (Player) event.getEntity();
    19. Player damager = (Player) event.getDamager();
    20.  
    21. if (GameState.isState(GameState.IN_GAME)) {
    22.  
    23. if (damager instanceof Player && player instanceof Player) {
    24. if (President.getPresidents().isPresidentPlaying(player) && President.getSecretService().isSecretServicePlaying(damager)) {
    25. event.setCancelled(true);
    26. damager.sendMessage(ChatColor.RED + "You can not hit your own teammates!");
    27. } else if (President.getSecretService().isSecretServicePlaying(player) && President.getPresidents().isPresidentPlaying(damager)) {
    28. event.setCancelled(true);
    29. damager.sendMessage(ChatColor.RED + "You can not hit your own teammates!");
    30. } else if (President.getTerrorists().isTerroristPlaying(player) && President.getTerrorists().isTerroristPlaying(damager)) {
    31. event.setCancelled(true);
    32. damager.sendMessage(ChatColor.RED + "You can not hit your own teammates!");
    33. }
    34. } else if(event.getDamager() instanceof Arrow && event.getEntity() instanceof Player){
    35. Arrow aDamager = (Arrow) event.getDamager();
    36. if(aDamager.getShooter() instanceof Player){
    37. Player damaged = (Player) aDamager.getShooter();
    38. Player taker = (Player) event.getEntity();
    39. if (President.getPresidents().isPresidentPlaying(taker) && President.getSecretService().isSecretServicePlaying(damaged)) {
    40. event.setCancelled(true);
    41. damager.sendMessage(ChatColor.RED + "You can not hit your own teammates!");
    42. } else if (President.getSecretService().isSecretServicePlaying(taker) && President.getPresidents().isPresidentPlaying(damaged)) {
    43. event.setCancelled(true);
    44. damager.sendMessage(ChatColor.RED + "You can not hit your own teammates!");
    45. } else if (President.getTerrorists().isTerroristPlaying(taker) && President.getTerrorists().isTerroristPlaying(damaged)) {
    46. event.setCancelled(true);
    47. damager.sendMessage(ChatColor.RED + "You can not hit your own teammates!");
    48. } else {
    49. event.setCancelled(false);
    50. }
    51.  
    52. }
    53. }
    54. }
    55.  
    56. }
    57.  
    58.  
    59.  
    60.  
    61. }
    62.  

    Error: http://hastebin.com/toqubibocu.vbs

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  4. Offline

    Skionz

    CrazymanJR
    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R3.entity.C
    raftArrow cannot be cast to org.bukkit.entity.Player
    Your casting an arrow to Player :p
     
  5. Offline

    IkBenHarm

    CrazymanJR
    your else if is in the wrong place i think, but i only had a quick look at it
     
  6. Offline

    mythbusterma

    CrazymanJR

    Understanding the code you write is paramount to it working. Learn some Java, and how to read a stack trace that VERY EXPLICITLY says what you did wrong, then try again.
     
Thread Status:
Not open for further replies.

Share This Page