Solved Making arrows from a certain bow explode

Discussion in 'Plugin Development' started by man_in_matrix, Apr 26, 2021.

  1. Offline

    man_in_matrix

    I'm trying to make a plugin that makes arrows explode on impact. I already made the arrows explode using ProjectileHitEvent. But, I only want arrows fired from a certain bow with special metadata to be explosive. Is there any way to do that? Kind of like the explosive bow from Hypixel Skyblock.
     
    Last edited: Apr 26, 2021
  2. Offline

    davidclue

    You can use an EntityShootBowEvent event to check and save the player who shot if they were holding your custom bow. Use your projectile hit event to cast the entity to an arrow and then use .getShooter() to check if your player is saved from EntityShootBowEvent.
     
  3. Offline

    man_in_matrix

    This is the code that makes the item.
    Code:
    package com.matrix.manhunt.items;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class ItemManager {
    
        public static ItemStack ExplosiveBow;
      
    
        public static void init() {
        createBow();
      
        }
      
        private static void createBow() {
            ItemStack item = new ItemStack(Material.BOW, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.DARK_RED + "Explosive" + ChatColor.DARK_RED +  "Bow");
            List<String> loreEBow = new ArrayList<>();
            loreEBow.add("§7Explodes on impact!");
            meta.setLore(loreEBow);
            meta.addEnchant(Enchantment.LUCK, 1, false);
            meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            item.setItemMeta(meta);
            ExplosiveBow = item;
        }
    
      
      
    }
    
    And this is the event class that SHOULD make the arrow from the explosive bow explode on impact.

    Code:
    package com.matrix.manhunt.events;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityShootBowEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class Events implements Listener{
    
      
      
      
        @EventHandler
        public void onEntityShootBow(EntityShootBowEvent event) {
            if (event.getProjectile() instanceof Arrow) {
                Arrow a = (Arrow) event.getProjectile();
                if (a.getShooter() instanceof Player) {
                    ItemStack bow = event.getBow();
                    if (bow.hasItemMeta() && bow.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.DARK_RED + "Explosive" + ChatColor.DARK_RED + "Bow")) {
                        System.out.println("Exp Bow!!!");
                        event.getEntity().getWorld().createExplosion(event.getEntity().getLocation(), 2.0f);
                    }
                }
            }}}
      
    
    But it doesn't work. I have the bow but I think something is wrong with the @EventHandler script, however, I don't know what.

    Because right now the explosive bow is the only bow that explodes BUT it explodes right when you shoot and not when and where the arrow lands.
     
    Last edited: Apr 27, 2021
  4. Offline

    OkayName

    I have an idea: Make an object, then, once a bow has been shot, store the arrow data in the object, and when an arrow hits the ground or an entity, which you can detect by ProjectileHitEvent, check if the projectile is an arrow and if it is equal to the stored arrow object.
     
  5. Offline

    Chr0mosom3

    This is the code that makes the arrow explode. It is ran when the player shoots the arrow.

    The way that I would do this, is when the player shoots the arrow, it would set the metadata of the arrow with "explosive: true". Then when the arrow lands, check if explosive is true, if yes then explode.
     
    davidclue likes this.
  6. Offline

    man_in_matrix

  7. Offline

    davidclue

    All you need to do is check when the arrow hits the ground or an entity with a ProjectileHitEvent and then do what chr0mo said and instead of saving a player, add metadata to the arrow when they fire it from your custom bow and then in the ProjectileHitEvent just check if the arrow has the metadata and create the explosion. After you create the explosion removes the metadata so if the arrow were to bounce it won't create another explosion, or maybe just kill the arrow it's up to you.
     
  8. Offline

    man_in_matrix

    It worked Thanks to all who helped :D
     

Share This Page