Advertisement
Guest User

ArrowTracker

a guest
Aug 2nd, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1.         @EventHandler
  2.     private void onLaunch(final EntityShootBowEvent e) {
  3.         // Make this your main class.
  4.         final JavaPlugin plugin = this;
  5.  
  6.         if (e.getEntity() instanceof Player) {
  7.             final Player p = (Player) e.getEntity();
  8.             final List<ItemStack> arsenal = new ArrayList<ItemStack>();
  9.             for (ItemStack item : p.getInventory().getContents()) {
  10.                 if (item != null && item.getType() == Material.ARROW) {
  11.                     // Store all the arrows in the inventory.
  12.                     arsenal.add(item.clone());
  13.                 }
  14.             }
  15.             new BukkitRunnable() {
  16.                 public void run() {
  17.                     for (ItemStack item : arsenal) {
  18.                         if (!p.getInventory().contains(item)) {
  19.                             // This is the arrow ItemStack that was shot as it
  20.                             // is no longer there exactly.
  21.                             String name = item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : "";
  22.                             System.out.println("A(n) " + name + (name.isEmpty() ? "" : " ") + "arrow has been fired.");
  23.                             e.getProjectile().setMetadata("ArrowTracker", new FixedMetadataValue(plugin, name));
  24.                             return;
  25.                         }
  26.                     }
  27.                 }
  28.             }.runTaskLater(plugin, 0L);
  29.         }
  30.     }
  31.  
  32.     @EventHandler
  33.     private void onHit(EntityDamageByEntityEvent e) {
  34.         List<MetadataValue> meta = e.getDamager().getMetadata("ArrowTracker");
  35.         String nameMeta = "";
  36.         for (MetadataValue value : meta) {
  37.             // Where 'this' is your main class.
  38.             if (value.getOwningPlugin().equals(this)) {
  39.                 nameMeta = value.asString();
  40.                 break;
  41.             }
  42.         }
  43.         if (!nameMeta.isEmpty())
  44.             System.out.println("An entity was damaged by a(n) " + nameMeta + " arrow");
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement