new arrow, i can't associate new effect

Discussion in 'Plugin Development' started by angi99552, Feb 9, 2017.

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

    angi99552

    i create the new item "lava arrow", but when i write the onPlayerHitBlock function, i don't know how understand what is the arrow consumed by my bow.

    getCustomName() not work, i cant get the my DisplayName.

    what method do i have to use?

    Code:
    @EventHandler
    public void onPlayerHitBlock(ProjectileHitEvent event)
    {
       Entity entity = event.getEntity();
         
       if (event.entity.getCustomName() ==ChatColor.RED + "" + ChatColor.BOLD + "Lava Arrow")
       {     
         Location acti = event.getHitBlock().getLocation();     
         acti.add(-2,1,-2);
         for (int i=0;i<2;i++){
           for(int ie=0;ie<4;ie++){
             acti.getBlock().setType(Material.LAVA);
             entity.getWorld().spawnParticle(Particle.LAVA,acti,51);
             acti.add(1,0,0 );
           }
           acti.add(-4,0,3 );
         }
       }
    }
    
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    {
       Player p = (Player) sender;
       if (commandLabel.equalsIgnoreCase("frecciatp"))
       {
         ItemStack item = new ItemStack(Material.ARROW,6);
         ItemMeta im = item.getItemMeta();
         im.setDisplayName (ChatColor.RED + "" + ChatColor.BOLD + "Lava Arrow");
         List<String> lore = new ArrayList<String>();
         lore.add(ChatColor.DARK_AQUA + "Arround the arrow);
         lore.add(ChatColor.YELLOW + "spawn a lava prision");
         
         im.setLore(lore);
         item.setItemMeta(im);
         p.getInventory().addItem(item);
       }
    }   
     
  2. Offline

    Zombie_Striker

    @angi99552
    When you do not know what a method or event does, look it up:
    https://hub.spigotmc.org/javadocs/bukkit/

    Event#getEntity() is the arrow.
    You cannot comparing strings with ==. You can only do it with Enums and numbers. For this, use .equals.
    Also, do not blindly cast. How do you know the sender will always be a player? Do an instanceof check first before casting.

    commandLabel should no longer be used. It is only there for backwards compatibility. Change it to cmd.getName()
     
  3. Offline

    angi99552

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    {
         Player p = (Player) sender;
         
         if (sender instanceof Player)
         {
           if (cmd.getName().equalsIgnoreCase("lavaarrow"))
           {
             ItemStack item = new ItemStack(Material.ARROW,6);
             ItemMeta im = item.getItemMeta();
             im.setDisplayName (ChatColor.RED + "" + ChatColor.BOLD + "Lava Arrow");
             List<String> lore = new ArrayList<String>();
             lore.add(ChatColor.DARK_AQUA + "Spawn lava");
             lore.add(ChatColor.YELLOW + "If it hit a block");
       
             im.setLore(lore);   
             item.setItemMeta(im);
             p.getInventory().addItem(item);  
           }
     if (cmd.getName().equalsIgnoreCase("waterarrow"))
           {
             ItemStack item = new ItemStack(Material.ARROW,6);
             ItemMeta im = item.getItemMeta();
             im.setDisplayName (ChatColor.BLUE + "" + ChatColor.BOLD + "Water Arrow");
             List<String> lore = new ArrayList<String>();
             lore.add(ChatColor.DARK_AQUA + "Spawn water");
             lore.add(ChatColor.YELLOW + "If it hit a block");
       
             im.setLore(lore);   
             item.setItemMeta(im);
             p.getInventory().addItem(item);  
           }
      }
    }
    
    @EventHandler
    public void onPlayerHitBlock(ProjectileHitEvent event) {
       Entity entity = event.getEntity();
       
       if (event.getEntityType().toString()=="ARROW")
       {   
       entity.getWorld().strikeLightningEffect(event.getHitBlock().getLocation());
       Location acti = event.getHitBlock().getLocation();
       acti.getBlock().setType(Material.WATER);
       entity.getWorld().spawnParticle(Particle.CLOUD,acti,51);     
       }
    } 
    
    ok thanks for tips

    but i continue dont understand how get the DisplayName of the arrow i shoot.
    getEntityType() result always "ARROW"
    getCustomName result always "";
     
  4. Offline

    ChipDev

    When you get the custom name of the entity, like a horse, it returns the name that is above it. If that is not set, it will return "" in this case.

    If you want to get the custom ITEM name, you have to save the ItemStack of the arrow when it was shot into a Map, and then you can work from there. An alternative is to give the Arrow metadata, but ugh.. thats never going to go well.
     
  5. Offline

    angi99552

    when bow shoot it remove one arrow from inventory
    but how i catch this event?
    PlayerDropItemEvent() not work
    PlayerItemBreakEvent() not work
    PlayerItemConsumeEvent() not work

    Code:
     
       @EventHandler
       public void  onItem(PlayerDropItemEvent e){
         System.out.println(e.getItemDrop().getItemStack());  
       }
       @EventHandler
       public void  onItem(PlayerItemConsumeEvent e){
         System.out.println(e.getItem());  
       }
       @EventHandler
       public void  onItem(PlayerItemBreakEvent e){
    
         System.out.println(e.getBrokenItem());  
       }
     
    
    with a InventoryEvent?
    other advice?
     
  6. Offline

    Zombie_Striker

    @angi99552
    Use PlayerInteractEvent. Bows work by finding the first arrow in the inventory. It starts with slots 0 and 8 (the hotbar) and then goes to 9-35 (the rest of the inventory). For loop through each slot and see if there is an arrow on one of those slots. If so, record the name of the arrow.
     
Thread Status:
Not open for further replies.

Share This Page