Custom items: Help

Discussion in 'Plugin Development' started by Evonoucono, Jul 19, 2015.

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

    Evonoucono

    I currently have some code here that will make arrows explode on impact dealing damage in an area around the projectile. However I do not want all bows to have this effect. Is there anyway to make a custom bow item with a different name that will make the code activate for it, and not other regular bows?
    Code:
        @EventHandler
        public void onHit(ProjectileHitEvent event){
            Projectile explosivearrow = event.getEntity();
            if (explosivearrow instanceof Arrow){
                Arrow arrow = (Arrow)explosivearrow;
                @SuppressWarnings("deprecation")
                LivingEntity shooter = arrow.getShooter();
                Player player = (Player)shooter;
                Location location = arrow.getLocation();
                int x = location.getBlockX();
                int y = location.getBlockY();
                int z = location.getBlockZ();
                arrow.getWorld().createExplosion(x, y, z, 1.5F, false, false);
                arrow.remove();
                player.sendMessage("Arrow exploded");
                       
            }
        }
     
  2. Offline

    justin_393

    ItemMeta
     
  3. Offline

    Monollyth

    Yes, there are multiple ways. You could check in the if the bow in the shooters hand has a certain display name or not, and if it does, then make an explosion where the arrow hit.
     
  4. Offline

    DoggyCode™

    Here, I'm feeling good to spoonfeed you!:

    Code:
             @EventHandler
               public void onHit(ProjectileHitEvent event){
                 //The Bow (add this to their inventory somehow) (I assume you know how to do that)
                 ItemStack itemStack = new ItemStack(Material.BOW);
                 ItemMeta meta = itemStack.getItemMeta();
                 meta.setDisplayName("§6Explosive Bow");
                 ArrayList<String> lore = new ArrayList<String>();
                 lore.add("§eFire explosive arrows!");
                 meta.setLore(lore);
                 itemStack.setItemMeta(meta);
                
                   Projectile explosivearrow = event.getEntity();
                   if (explosivearrow instanceof Arrow){
                       Arrow arrow = (Arrow)explosivearrow;
                       LivingEntity shooter = (LivingEntity) arrow.getShooter();
                       Player player = (Player)shooter;
                       Location location = arrow.getLocation();
                       ItemStack hand = player.getItemInHand();
                       int x = location.getBlockX();
                       int y = location.getBlockY();
                       int z = location.getBlockZ();
                       if(hand.hasItemMeta()) {
                          if(hand!=null&&hand.getType()==Material.BOW&&hand.getItemMeta().getDisplayName().equalsIgnoreCase("§6Explosive Bow")) {
                               arrow.getWorld().createExplosion(x, y, z, 1.5F, false, false);
                               arrow.remove();
                               player.sendMessage("Arrow exploded");
                           }
                          return;
                       }
                       return;
                   }
               }
    Anyways, here's a example of how you can add it to a inventory:
    Code:
            } if(cmd.getName().equalsIgnoreCase("ebow")) {
                 //The Bow (add this to their inventory somehow) (I assume you know how to do that)
                 ItemStack itemStack = new ItemStack(Material.BOW); 
                 ItemMeta meta = itemStack.getItemMeta();
                 meta.setDisplayName("§6Explosive Bow");
                 ArrayList<String> lore = new ArrayList<String>();
                 lore.add("§eFire explosive arrows!");
                 meta.setLore(lore);
                 itemStack.setItemMeta(meta);
                 
                 if(sender instanceof Player) {
                   Player p = (Player)sender;
                   p.getInventory().addItem(itemStack);
                   sender.sendMessage(ChatColor.GREEN + "Explosive Bow added to your inventory!");
                   return true;
                 } else {
                   sender.sendMessage("No console...");
                   return true;
                 }
            }
    
    No problemo :p
     
  5. Offline

    Evonoucono

    HEY THANKS SO MUCH! This is exactly what I was looking for, and I went through the code a couple of times and I think I understand it enough to start creating other custom items. You're amazing, thanks :)

    EDIT: Already made a new item with this, thanks again!
     
    Last edited: Jul 20, 2015
  6. Offline

    DoggyCode™

Thread Status:
Not open for further replies.

Share This Page