Disable pvp from arrow doesnt work

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

Thread Status:
Not open for further replies.
  1. 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. 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. 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. all
     
  7. @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. Could not pass event EntityDamageByEntityEvent to VS100widzow v1.0
     
  9. 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
     
  10. i dont know how to "rewrite the whole listener" because i cant understand your advises ;-;
    cant you just write what should i change by write example?
    You know what i want to do because i wrote about this
    this would be more helpful for me
     
  11. Offline

    Strahan

    So what, you expect the Bukkit forum users to write the plugin for you? lol It's not that complex. Basically:

    Code:
    Event {
        Is the entity not a player?  Return if so
        Is the damager not an arrow or player?  Return if so
        Is the damager an arrow & arrow's shooter not a player?  Return if so
        Create a var for the victim
        Create a var for the aggressor
        Is the damager a player and aggressor's held item not crossbow?  Return if so
        Loop the board's team list {
            Does entries list contain both victim and aggressor name? Cancel & return if so
        }
    }
    That worked for me.

    EDIT: Updated to account for crossbows. Apparently when you left click a player with a loaded crossbow, it registers a regular melee attack not an arrow attack. Interesting.
     
    Last edited: Aug 24, 2019
Thread Status:
Not open for further replies.

Share This Page