Solved Cancelling arrow knockback

Discussion in 'Plugin Development' started by kreashenz, Sep 29, 2013.

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

    kreashenz

    Howdy!
    Is cancelling arrow knockback possible, or projectile knockback all together? I've had a theories of doing this, but mainly cancelling the EntityDamageByEntityEvent and setting the damage to 0. This didn't do anything.. :/

    Any help will be appreciated. Thanks :)
     
  2. Offline

    Loogeh

    kreashenz Maybe on EntityDamageByEntityEvent try teleporting the player to themself
     
  3. Offline

    adam753

    Cancelling ProjectileHitEvent might work.
     
  4. Offline

    kreashenz

    adam753 I can't check if the thing hit is a player. In fact, I can't check what it hit.
     
  5. Offline

    Chinwe

    EntityDamageByEntity event might work, if you check it's an arrow, cancel the event and then damage the player using (something like) player.damage(event.getDamage(), event.getDamager()) ?
     
  6. Offline

    kreashenz

    Chinwe I tried that, but because I needed to cancel all projectile knock backs, I've checked if the damaged is a projectile. I could supply code but I'm on my phone at the moment so I will in the morning.
     
  7. Offline

    Ultimate_n00b

    Code:java
    1. @EventHandler
    2. public void onEntityDBEntity(EntityDamageByEntityEvent event) {
    3. if(event.getDamager() instanceof Projectile) {
    4. event.setCancelled(true);
    5. event.getEntity().damage(event.getDamage(), event.getDamager());
    6. }
    7. }


    Sorry if it isn't perfect, I'm too used to writing in a IDE :p
     
  8. Offline

    kreashenz

    Ok, my code is
    Code:java
    1. @EventHandler
    2. public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent e){
    3. if(e.getDamager() instanceof Projectile){
    4. Projectile proj = (Projectile)e.getDamager();
    5. if(pm.hasKit() && pm.getKit().equals(Kit.ROCK)){
    6. if(cooldown.contains(p.getName())){
    7. e.setCancelled(true);
    8. proj.setBounce(true);
    9. }
    10. }
    11. }
    12. }
    13. }

    That still doesn't work.
     
    Batman500 likes this.
  9. Offline

    Cybermaxke

    Try to use this: ;)
    Code:
    Arrow arrow = ...;
     
    try {
        Object handle = arrow.getClass()
                .getMethod("getHandle", new Class[] {})
                .invoke(arrow, new Object[] {});
     
        handle.getClass()
                .getMethod("a", int.class)
                .invoke(handle, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
     
  10. Offline

    GaaTavares

    Remember: cancelling the event and then damaging the player, won't reduce the damage if the player has armor.
     
  11. Offline

    kreashenz

    Would this work for all projectiles?

    And it also didn't work, I used it like
    Code:java
    1. @EventHandler
    2. public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent e){
    3. if(e.getEntity() instanceof Player){
    4. if(e.getDamager() instanceof Projectile){
    5. Projectile proj = (Projectile)e.getDamager();
    6. if(pm.hasKit() && pm.getKit().equals(Kit.ROCK)){
    7. if(cooldown.contains(p.getName())){
    8. e.setCancelled(true);
    9. try {
    10. Object handle = proj.getClass().getMethod("getHandle", new Class[] {}).invoke(proj, new Object[] {});
    11. handle.getClass().getMethod("a", int.class).invoke(handle, 0);
    12. } catch (Exception ex) {
    13. ex.printStackTrace();
    14. }
    15. }
    16. }
    17. }
    18.  
    19. }
    20. }
     
  12. Offline

    xTrollxDudex

    kreashenz
    I think canceling and damaging the player should work
     
  13. Offline

    kreashenz

    xTrollxDudex Well it doesn't.. I don't want to damage the player either. And what event are you talking about to cancel?
     
  14. Offline

    Loogeh

    kreashenz He's talking about cancelling EntityDamageByEntityEvent (I'm pretty sure)
     
  15. Offline

    kreashenz

    Loogeh Yeah, cancelling it doesn't work.
     
  16. Offline

    Loogeh

    kreashenz It should work though. When you say it doesn't work what do you mean? Where does it get to in the code?
     
  17. Offline

    kreashenz

    Loogeh It doesn't stop the knockback, though it stops the damage. Look at my above code..
     
  18. Offline

    Loogeh

    kreashenz By knockback do you mean the enchantment or just the normal knockback that an arrow has?
     
  19. Offline

    kreashenz

    Loogeh normal knockback the arrow has.
     
  20. Offline

    Quantix

    I just tested this code and it works perfectly, still damaging the player but removing all knockback:
    Code:java
    1. @EventHandler (priority = EventPriority.NORMAL)
    2. public void onEntityDamageByEntity (EntityDamageByEntityEvent event) {
    3. if (event.getEntity() instanceof Player && event.getDamager() instanceof Projectile) {
    4. Player player = (Player) event.getEntity();
    5. player.damage(event.getDamage(), event.getDamager());
    6. event.setCancelled(true);
    7. }
    8. }
    If this code doesn't work for you something else is not working. You could add some debug messages into your event to see if your plugin is correctly listening to the event.
     
  21. Offline

    kreashenz

    ....... And there was my problem. Again, a stupid mistake by me. The player wasn't getting added to the cooldown list because I hadn't create the ability for the kit. Argh..
    Sorry for wasting everyone's time :(
     
    Minecrell likes this.
Thread Status:
Not open for further replies.

Share This Page