Remove specific arrows

Discussion in 'Plugin Development' started by iKreal, Aug 2, 2018.

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

    iKreal

    Hello,
    I'm making a firearm (especially a Pistol) on Minecraft, and as I'm starting with Java Language, I have some problems.
    This is the event which triggers the launch of arrows:
    Code:
    @EventHandler
        public void onInteract(PlayerInteractEvent event) {
           
            Player player = event.getPlayer();
            Action action = event.getAction();
            ItemStack it = event.getItem();
            Location ploc = player.getLocation();
    
            if(it == null) return;
           
            if(it.getType() == Material.WOOD_HOE && it.hasItemMeta() && it.getItemMeta().hasDisplayName() && it.getItemMeta().getDisplayName().equalsIgnoreCase("§fPistol")) {
                if(action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {
                    player.launchProjectile(Arrow.class, ploc.getDirection().normalize().multiply(8));
    
                }
            }
        }
    
    So here I've got many problems: I would like the arrows launched be removed when land and be invisible.
    Because the pistol is a gun, with ammos and not arrows. And I was wondering if I could emit a few particles on the arrow when "flying"... but it doesn't really work.
    To remove the arrows, I found a solution:
    Code:
    @EventHandler
        public void onProjectileHit(ProjectileHitEvent event) {
            Entity entity = event.getEntity();
            if (entity.getType() == EntityType.ARROW) {
                entity.remove();
            }
        }
    
    but a bad solution because every arrow is removed, even the ones launched by a bow.

    That's why I'm asking for your help. Thanks for reading and thanks to the helpers! :)
     
  2. Offline

    timtower Administrator Administrator Moderator

    @iKreal Add the entity to a list when you shoot it.
    Then in the hit event check if it is in that list. If so: remove from the list and remove the arrow.
     
  3. Offline

    iKreal

    Ok, thank you, but as I am starting, I don't know how to add it in the list...
    (I know how lists work but I only know the basic things)
    (Ah and I am French, so sorry if some sentences aren't right)
     
  4. Offline

    iKreal

    up [sign] :)
     
  5. Offline

    Tango_

    @iKreal
    You could also give the arrow a metadata value and check if the arrow has the specific metadata when being removed. Something like this:

    Code:
        @EventHandler
        public void onInteract(PlayerInteractEvent event) {
        
            Player player = event.getPlayer();
            Action action = event.getAction();
            ItemStack it = event.getItem();
            Location ploc = player.getLocation();
    
            if(it == null) return;
        
            if(it.getType() == Material.WOOD_HOE && it.hasItemMeta() && it.getItemMeta().hasDisplayName() && it.getItemMeta().getDisplayName().equalsIgnoreCase("§fPistol")) {
                if(action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {
                    Arrow pistol = player.launchProjectile(Arrow.class, ploc.getDirection().normalize().multiply(8));
                    pistol.setMetadata("pistol", new FixedMetadataValue (this, ""));
                }
            }
        }
      
        @EventHandler
        public void onProjectileHit(ProjectileHitEvent event) {
            Entity entity = event.getEntity();
            if (entity.getType() == EntityType.ARROW && entity.hasMetadata("pistol")) {
                entity.remove();
            }
        }
     
  6. Offline

    iKreal

    Oh, thank you, i'll try that! :)
     
Thread Status:
Not open for further replies.

Share This Page