Disable pvp from arrow doesnt work

Discussion in 'Plugin Development' started by Doknesss, Aug 13, 2019.

  1. Offline

    Doknesss

    Hi, i need to make plugin which will block damage from arrows when damager and victim are in the same team -> in this case != yt

    but when im put plugin on server im getting just "-1" message on chat and default pvp is blocked properly but i want also to block damage from arrows and i wrote code like this



    @EventHandler
    public void pvp(EntityDamageByEntityEvent e) {
    Player ofiara = (Player) e.getEntity();
    Player napast = (Player) e.getDamager();
    Bukkit.broadcastMessage("-1");

    if(!yt.contains(ofiara) && widzowie.contains(napast)) {
    napast.sendMessage(ChatColor.RED + "To twój sojusznik!");
    e.setCancelled(true);
    }

    if(yt.contains(ofiara) && yt.contains(napast)) {
    e.setCancelled(true);
    }


    Arrow arrow = (Arrow)e.getDamager();
    Bukkit.broadcastMessage("2");
    if (arrow.getShooter() instanceof Player && (e.getEntity() instanceof Player))
    Bukkit.broadcastMessage("3");
    {
    Player ataker = napast;
    Player beinataked = ofiara;
    if (!yt.contains(ataker))
    {
    Bukkit.broadcastMessage("4");
    ataker.sendMessage(ChatColor.RED + "Nie mozesz atakowac swoich sojusznikow!");
    e.setCancelled(true);
    return;
    } else {
    Bukkit.broadcastMessage("5");
    }
    }
    }


    any suggestions what i could do wrong?
     
  2. Offline

    KarimAKL

    @Doknesss
    1. You are casting without checking.
    ofiara = e.getEntity() -> Player
    napast = e.getDamager() -> Player
    arrow = e.getDamager() -> Arrow
    2. I'm guessing 'yt' is a list, but what does it contain?
    3. You should check if the victim is a player, then check if the damager is a player, if not, check if it's an arrow, and then get the shooter from the arrow.
     
    DerDonut likes this.
  3. Offline

    Doknesss

    ArrayList<Player> yt = new ArrayList<Player>();

    how?

    i did something like this


    if(ofiara instanceof Player && napast instanceof Arrow) {
    Bukkit.broadcastMessage("strzala");
    e.setCancelled(true);
    }

    but this still not works
     
  4. Offline

    DerDonut

    You are handling both, the 'default' pvp and the arrow in this listener right?
    So as @KarimAKL said, at the beginning, check if the victim is a player, then check if the damager is a player (using instanceof) or if he's an arrow, in the last case get the shooter of the arrow with arrow.getShooter(). Now you have 2 player objects and you can test if they are in the same team or not (and then cancel the event)

    EDIT: and please use the function insert > code for posting code
     
  5. Offline

    KarimAKL

    That doesn't help me; i was asking what it contains. Which players do you add to the list?
     
  6. Offline

    Doknesss

    all
     
  7. Offline

    DerDonut

    @Doknesss please rewrite the whole listener with our advises and post it again. You should also get lots of errors because of wrong casting so maybe show us the console log too
     
  8. Offline

    Doknesss

    Could not pass event EntityDamageByEntityEvent to VS100widzow v1.0
     
  9. Offline

    DerDonut

    Of course you get an error in the EntitydamageByentityEvent xd
    If you want us to help you we need the whole stacktrace but since you didn't change anything we already told you about the things you have to do,
    so please...
    If you want us to explain something more detailed say so
     

Share This Page