Get shooter of explosive ?

Discussion in 'Spigot Plugin Development' started by Drawethree, Apr 24, 2017.

  1. Hey guys, i'm making a TNT BOW, and for some reasons i need to get player, which shooted from the tnt bow if the tnt damages other player. How can I do that ?
     
  2. What projectile are you shooting?
     
  3. Its arrow, but im removing it and spawning a TNT with its velocity.
    Code (Text):
        @EventHandler
        public void onShoot(EntityShootBowEvent e) {
            if (e.getEntity() instanceof Player) {
                Player p = (Player) e.getEntity();
                if (GameManager.getManager().isInGame(p)) {
                    if (GameManager.getManager().gameState == GAMESTATE.PVP) {
                        if (e.getBow() != null) {
                            if (e.getBow().hasItemMeta()) {
                                ItemMeta meta = e.getBow().getItemMeta();
                                if (meta.getDisplayName() != null) {
                                    if (meta.getDisplayName().equals(explosiveBow.getItemMeta().getDisplayName())) {
                                        TNTPrimed tnt = p.getWorld().spawn(e.getProjectile().getLocation(),
                                                TNTPrimed.class);
                                        Vector v = e.getProjectile().getVelocity();
                                        tnt.setVelocity(v.multiply(0.5));
                                        e.getProjectile().remove();
                                        tnt.setFuseTicks(40);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
     
  4. You'll need to track who shot this specific bit of TNT yourself, there's a couple of ways to do this.

    You could add metadata to the spawned entity and read it when it explodes like:
    Tntarrow_playerName.

    Then when it explodes get the meta and check to see if any of its values start with TntArrow_

    Or make a hashmap <Entity,Player> that is accessible outside of the shoot event. Add the TNT entity to it along with the player who shot the arrow. Whenever it explodes check the hash map to see if the TNT entity exists and get the corresponding player value as your shooter.

    Using this method you'll also need to remove the TNT entities from the hash map once they're exploded / no longer relevant


    Sent from my iPhone using Tapatalk