Solved Arrow = Ban Code

Discussion in 'Plugin Development' started by Giorgio, Nov 17, 2012.

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

    Giorgio

    Hello everyone i need help with when a player shoots a player with an arrow it bans them. If anyone can write a simple listener that can do this It would really help me, im doing my best for other objects to do this, i just don't understand how the Arrow hit entity works.
     
  2. Offline

    SoThatsIt

    harder than i thought it would be but here ya go

    Code:
    @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent e){
            if(e.getDamager().getType() == EntityType.ARROW){
                if(e.getEntity() instanceof Player){
                    Player p = (Player) e.getEntity();
                    p.setBanned(true);
                }
            }
        }
     
  3. Offline

    Giorgio

  4. Offline

    SoThatsIt

    hmm i dont know why you get those errors i didnt test it though
    here this might work

    Code:
    public void onEntityDamageByEntity(EntityDamageByEntityEvent e){
            if(e.getDamager().getType() == EntityType.ARROW){
                if(e.getEntity().getType() == EntityType.PLAYER){
                    Player p = (Player) e.getEntity();
                    p.setBanned(true);
                }
            }
        }
     
  5. Offline

    ZeusAllMighty11 Retired Staff

    I have EXACT code for this if you want it. PM me. :)
     
  6. Offline

    Giorgio

  7. Offline

    gomeow

    does anyone have code that will work for creative players? I tried making this a while back, using ProjectileHitEvent
     
  8. Offline

    skipperguy12





    Better:


    public void onEntityDamageByEntity(EntityDamageByEntityEvent e){
    //only interested in projectile damage
    if(event.getCause() != DamageCause.PROJECTILE)
    return;

    Projectile proj = (Projectile)event.getDamager();
    //could be skeleton or dispenser, we only want player
    if(!(proj.getShooter() instanceof Player) || !(event.getEntity() instanceof Player)) return;
    Player victim = (Player) event.getEntity();
    victim.setBanned(True);
    }

    Only for creative players? Check if the victim has gamemode of creative. For all players, including creative, use my code.
     
    SgtStud likes this.
  9. Offline

    Giorgio

  10. Offline

    jasalb236

    thats because it still casts the entity to a player...
     
  11. Offline

    Giorgio

  12. Offline

    jasalb236

    did you use the code of skipperguy12?
     
  13. Offline

    Giorgio

    yes, still get the error

    if(e instanceof Player){
    }else{
    e.setCancelled(true);
     
  14. Offline

    jasalb236

    I didn't test it, but try this:

    public void onEntityDamageByEntity(EntityDamageByEntityEvent event){

    if(event.getCause() != DamageCause.PROJECTILE){
    if(event.getDamager() instanceof Player || event.getEntity() instanceof Player){
    Player victim = (Player) event.getEntity();
    victim.setBanned(true);
    }
    }
    }

    wait change it to this:

    public void onEntityDamageByEntity(EntityDamageByEntityEvent event){

    if(event.getCause() == DamageCause.PROJECTILE){
    if(event.getDamager() instanceof Player || event.getEntity() instanceof Player){
    Player victim = (Player) event.getEntity();
    victim.setBanned(true);
    }
    }
    }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  15. Offline

    Giorgio

    Still no good.

    ok didn't see your new post

    jasalb236
    Ok i still got this:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  16. Offline

    fireblast709

    do not cast an Arrow to Player
     
  17. Offline

    jasalb236

    No I shouldn't but I don't get what the code is that casts an arrow to a player. Maybe the Player victim = (Player) event.getEntity()?
     
  18. Offline

    Giorgio

    What? i have to cast it to the player in order to ban them.

    Thats exactly what the issue is i just don't know how to bypass it or go around it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  19. Offline

    fireblast709

    Code:java
    1. @EventHandler
    2. public onDamage(EntityDamageByEntityEvent event)
    3. {
    4. if(event.getEntity() instanceof Player && (event.getDamager() instanceof Player || event.getDamager() instanceof Projectile))
    5. {
    6. Player damaged = (Player)event.getEntity();
    7. Player damager = null;
    8. if(event.getDamager() instanceof Projectile)
    9. {
    10. Projectile p = (Projectile)event.getDamager();
    11. if(p.getShooter() instanceof Player)
    12. {
    13. damager = (Player)p.getShooter();
    14. }
    15. else
    16. {
    17. // It was a skeleton, ghast, blaze, etc...
    18. return;
    19. }
    20. }
    21. else
    22. {
    23. damager = (Player)e.getDamager();
    24. }
    25. // Now you have 2 Players, do whatever checks
    26. }
    27. }

    le sigh... not that hard :3
     
  20. Offline

    Giorgio


    And how do you suggest i define player?
     
  21. Offline

    lol768 Retired Staff

    You shouldn't need to.
     
  22. Offline

    fireblast709

    Giorgio
    Player damaged - the player that got hit
    Player damager - the player that damaged

    So that means the damager is trying to ban damaged. Add your other code for the actual banning after the comment "// Now you have 2 Players, do whatever checks"
     
  23. Offline

    Giorgio

    Ok, i still get the error, here is what im using:

    Code:
    package Listeners;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.inventory.ItemStack;
     
    import Commands.BanSwordCommand;
     
    public class WoodSwordBan
      implements Listener
    {
      public WoodSwordBan()
      {
      }
      @EventHandler(ignoreCancelled=true)
      public void onPlayerClick(EntityDamageByEntityEvent event) {
        Player damager = (Player)event.getDamager();
        Entity damaged = event.getEntity();
        ItemStack inHand = damager.getItemInHand();
     
        if (BanSwordCommand.bsToggled) {
          if ((!(event.getDamager() instanceof Player)) || (!(event.getEntity() instanceof Player))) {
            return;
          }
     
          if (!(event.getEntity() instanceof Player)) {
            damaged = event.getEntity();
          }
     
          if (((event.getDamager() instanceof Player)) && ((event.getEntity() instanceof Player))) {
            if ((inHand == null) || (inHand.getType() != Material.WOOD_SWORD))
              return;
            if (inHand.getType() == Material.WOOD_SWORD) {
              ((Player)damaged).kickPlayer(ChatColor.RED + "The Bansword has spoken");
              ((OfflinePlayer)damaged).setBanned(true);
            }
          }
        } else if (!BanSwordCommand.bsToggled) {
          return;
        }
      }
    }
     
  24. Offline

    fireblast709

    Code:java
    1. package Listeners;
    2. import org.bukkit.ChatColor;
    3. import org.bukkit.Material;
    4. import org.bukkit.OfflinePlayer;
    5. import org.bukkit.entity.Entity;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    10. import org.bukkit.inventory.ItemStack;
    11.  
    12. import Commands.BanSwordCommand;
    13.  
    14. public class WoodSwordBan implements Listener
    15. {
    16.  
    17. @EventHandler(ignoreCancelled=true)
    18. public void onPlayerClick(EntityDamageByEntityEvent event)
    19. {
    20. Entity damager = event.getDamager();
    21. Entity damaged = event.getEntity();
    22. ItemStack inHand = damager.getItemInHand();
    23.  
    24. if (BanSwordCommand.bsToggled)
    25. {
    26. if (inHand == null || inHand.getType() != Material.WOOD_SWORD)
    27. {
    28. return;
    29. }
    30. if (!(event.getDamager() instanceof Player) || !(event.getEntity() instanceof Player))
    31. {
    32. return;
    33. }
    34.  
    35. Player banned = (Player)damaged;
    36. ((Player)damaged).setBanned(true);
    37. ((Player)damaged).kickPlayer(ChatColor.RED + "The Bansword has spoken");
    38. }
    39. }
    40. }
     
  25. Offline

    Giorgio

    fireblast709

    I just realized i sent you the wrong bit of code

    its:
    Code:
    package Listeners;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
     
    public class ArrowBan
      implements Listener
    {
      public ArrowBan()
      {
      }
      @EventHandler
      public void onDamage(EntityDamageByEntityEvent event){
      if(event.getEntity() instanceof Player && (event.getDamager() instanceof Player || event.getDamager() instanceof Projectile))
      {
      Player damaged = (Player)event.getEntity();
      Player damager = null;
      if(event.getDamager() instanceof Projectile){
      Projectile p = (Projectile)event.getDamager();
      if(p.getShooter() instanceof Player){
      damager = (Player)p.getShooter();
     
      }else{
          // Do nothing
        return;
      }
     
      }else{
        damager = (Player)event.getDamager();
      }
        damager.sendMessage(ChatColor.GREEN + "Successfully banned player");
        damaged.setBanned(true);
        damaged.kickPlayer("Bansword has spoken!");
      }
        }
          }
     
  26. Offline

    fireblast709

    and the error is...?
     
  27. Offline

    messageofdeath

    Giorgio Here i have the code works perfectly on 1.4.5 dev build should also work below that!

    PHP:
    @EventHandler
        
    public void onHitWithProjectile(EntityDamageByEntityEvent event) {
            if(
    event.getDamager() instanceof Arrow) {
                if(
    event.getEntity() instanceof Player) {
                    
    Arrow arrow = (Arrow)event.getDamager();
                    
    Player victim = (Player)event.getEntity();
                    if(
    arrow.getShooter() instanceof Player) {
                        
    Player shooter = (Player)arrow.getShooter();
                        if(
    shooter.hasPermission("ban.with.arrows")) {
                            
    victim.setBanned(true);
                            
    victim.kickPlayer(ChatColor.RED "You a now banned!");
                        }
                    }
                }
            }
        }
     
  28. Offline

    Giorgio

  29. Offline

    messageofdeath

    How did mine not work. It meet your description and it worked for me
     
  30. Offline

    Giorgio

    Last edited by a moderator: May 29, 2016
Thread Status:
Not open for further replies.

Share This Page