package me.exphc.PickupArrows;

import java.util.logging.Logger;
import java.util.List;

import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.*;
import org.bukkit.event.*;
import org.bukkit.event.block.*;
import org.bukkit.event.player.*;
import org.bukkit.event.entity.*;
import org.bukkit.entity.*;
import org.bukkit.command.*;
import org.bukkit.*;

import org.bukkit.craftbukkit.entity.CraftArrow;

class PickupArrowsListener implements Listener {
	PickupArrows plugin;

	public PickupArrowsListener(PickupArrows plugin) {
		this.plugin = plugin;

		Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
	}

    @EventHandler(priority = EventPriority.NORMAL)
	public void onEntityShootBow(EntityShootBowEvent event) {
		Entity projectile = event.getProjectile();
		if (!(projectile instanceof Arrow)) {
			return;
		}
		Arrow arrow = (Arrow)projectile;

		Entity shooter = event.getEntity();
		if (shooter instanceof Skeleton || !plugin.getConfig().getBoolean("skeletonsOnly", true)) {
			plugin.log("shot by "+shooter);

            // anyone can pickup
            if (!plugin.getConfig().getBoolean("usePermissions", false)) {
                plugin.log("allowed by configuration");
                setAllowPickup(arrow);
                return;
            }

            // otherwise, check if anyone nearby is allowed
			double r = plugin.getConfig().getDouble("range", 10.0);
			List<Entity> nearbyEntities = shooter.getNearbyEntities(r, r, r);
			for (Entity nearbyEntity: nearbyEntities) {
				if (nearbyEntity instanceof Player) {
					Player player = (Player)nearbyEntity;

					if (player.hasPermission("pickuparrows.allow")) {
						plugin.log("allowed by permissions of "+player.getName());
                        setAllowPickup(arrow);
						return;
					} else {
                        plugin.log("no permission from "+player.getName());
                    }
				}
			}
			plugin.log("not allowed");
		}
	}

    public void setAllowPickup(Arrow arrow) {
        ((CraftArrow)arrow).getHandle().fromPlayer = true;
    }
}

public class PickupArrows extends JavaPlugin {
    Logger log = Logger.getLogger("Minecraft");

    public void onEnable() {
        getConfig().options().copyDefaults(true);
        saveConfig();
        reloadConfig();

		new PickupArrowsListener(this);
    }

    public void onDisable() {
    }

    public void log(String message) {
        if (getConfig().getBoolean("debug", false)) {
            log.info(message);
        }
    }
}

@@ -793,16 +793,38 @@
 ;%0A%09%09if (
+projectile == null %7C%7C 
 !(projec
@@ -948,40 +948,8 @@
 if (
-shooter instanceof Skeleton %7C%7C !
 plug
@@ -1004,48 +1004,126 @@
 ) %7B%0A
-%09%09%09plugin.log(%22shot by %22+shooter);%0A%0A    
+            if (shooter == null %7C%7C !(shooter instanceof Skeleton)) %7B%0A                return;%0A            %7D%0A        %7D%0A%0A
     
@@ -1151,28 +1151,24 @@
 kup%0A        
-   
- 
 if (!plugin.
@@ -1222,36 +1222,32 @@
 ) %7B%0A            
-    
 plugin.log(%22allo
@@ -1274,36 +1274,32 @@
 %22);%0A            
- 
-   
 setAllowPickup(a
@@ -1309,36 +1309,32 @@
 w);%0A            
-   
- 
 return;%0A        
@@ -1329,35 +1329,27 @@
 rn;%0A        
-    %7D%0A%0A    
+%7D%0A%0A
         // o
@@ -1392,19 +1392,24 @@
 allowed%0A
-%09%09%09
+        
 double r
@@ -1456,19 +1456,24 @@
  10.0);%0A
-%09%09%09
+        
 List%3CEnt
@@ -1530,19 +1530,24 @@
  r, r);%0A
-%09%09%09
+        
 for (Ent
@@ -1582,20 +1582,28 @@
 ties) %7B%0A
-%09%09%09%09
+            
 if (near
@@ -1632,21 +1632,32 @@
 ayer) %7B%0A
-%09%09%09%09%09
+                
 Player p
@@ -1687,21 +1687,32 @@
 ntity;%0A%0A
-%09%09%09%09%09
+                
 if (play
@@ -1753,22 +1753,36 @@
 ow%22)) %7B%0A
-%09%09%09%09%09%09
+                    
 plugin.l
@@ -1844,36 +1844,32 @@
                 
-    
 setAllowPickup(a
@@ -1879,27 +1879,52 @@
 w);%0A
-%09%09%09%09%09%09return;%0A%09%09%09%09%09
+                    return;%0A                
 %7D el
@@ -1928,20 +1928,16 @@
  else %7B%0A
-    
         
@@ -2020,28 +2020,42 @@
     
+%7D%0A
     
-%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09
+        %7D%0A        %7D%0A        
 plug
@@ -2070,23 +2070,19 @@
 not 
-allow
+chang
 ed%22);%0A
-%09%09%7D%0A
 %09%7D%0A%0A

Fix potential NPE