change the damage of an arrow

Discussion in 'Plugin Development' started by loman, Feb 26, 2020.

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

    loman

    hi there everyone
    I have a problem about changing the damage of the arrow which is not shot by a bow
    and my API is minecraft 1.12.2 version so I can't use arrow.setdamage() : (
    Can anyone give me some clue of how to do this : )

    Code:
                            Location loc = player.getLocation();
                            Arrow arrow = player.getWorld().spawn(loc., Arrow.class);
                            arrow.setShooter(player);
    Can anyone give me a liitle help :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 27, 2020
  2. Offline

    yPedx

    On EntityDamageByEntityEvent, cancel if it's an arrow projectile and then apply your custom damage to the player.
     
  3. Offline

    loman

    @yPedx
    Thank you for your help but can you show me how to do so : )
    I am new to the plugin so I don't have any idea of how to do that : )
     
  4. Offline

    yPedx

    @loman
    Since I don't know how much you know, I'm going to describe how to do it in the simplest way.

    This will take place in your main class, so that you don't have to worry about other classes.

    First, start off by creating a function (you can name it whatever you want) that takes one argument. The argument type must be EntityDamageByEntityEvent as shown below. You must also put the @EventHandler annotation above this function.
    Code:
    @EventHandler
    public void onHit(EntityDamageByEntityEvent event){
    
    }
    First let's define a variable to hold the damaging entity.
    Code:
    Entity damager = event.getDamager();
    Then, let's check if the damaging entity is an arrow before we change the damage dealt.
    Code:
    if (!(damager instanceof Arrow)) return;
    The above code will make sure we don't continue if the damaging entity is not an arrow. Now that we know it's an arrow, let's set the damage.
    Code:
    event.setDamage(5.0);
    5.0 damage is just an example, you should correct it to your liking.

    Only if you're done writing the above! (open)

    Code:
    @EventHandler
    public void onHit(EntityDamageByEntityEvent event){
        Entity damager = event.getDamager();
    
        if(!(damager instanceof Arrow)) return;
    
        event.setDamage(5.0);
    }


    Now you must register the event. Head over to your onEnable function in your main class and put in the following:
    Code:
    getServer().getPluginManager().registerEvents(this, this);
    If I remember correctly, your Main class must implement Listener too.
     
    Last edited: Feb 28, 2020
    Sw_aG likes this.
  5. Offline

    Dai_Kunai

    Yes, or some people make a separate class that implements Listener and in their main class they write:
    Code:
    getServer().getPluginManager().registerEvents([CUSTOM_LISTENER_CLASS], this);
    into their onEnable(). Then they put all of the event methods in the Listener class. I prefer the first idea better, but some people might find it cleaner to do what I just said. Preference.
     
Thread Status:
Not open for further replies.

Share This Page