private void newGlobalItem(GroundItem item) {
		    if (item == null) {
			    return;
		    }
		    item = itemExists(item);
		    if (item != null) {
			    item.setGlobal(true);
			    foreach(Player p in Server.getPlayerList()) {
				    if (p == null || (item.getDefinition().isPlayerBound() && !item.getOwner().Equals(p))) {
					    continue;
				    }
				    if (p.getLocation().withinDistance(item.getLocation(), 60)) {
					    if(item.getOwner() != null) {
						    p.getPackets().createGroundItem2(item);
					    } else {
						    p.getPackets().createGroundItem(item);
					    }
				    }
			    }
			    if (!item.getDefinition().isPlayerBound()) {
				    item.setOwner(null);
			    }
			    GroundItem i = item;
			    if (!item.isRespawn()) {
                    Event removeGlobalItemEvent = new Event(60000);
				    removeGlobalItemEvent.setAction(() => {
						clearGlobalItem(i);
						removeGlobalItemEvent.stop();
				    });
                    Server.registerEvent(removeGlobalItemEvent);
			    }
		    }
	    }
	    public void newWorldItem(GroundItem item) {
		    lock(items) {
			    items.Add(item);
		    }
		    item.setOwner(null);
		    newGlobalItem(item);
	    }
	    public void newEntityDrop(GroundItem item) {
		    lock(items) {
			    items.Add(item);
		    }
		    if (item.getOwner() != null && !item.getOwner().isDestroyed()) {
			    item.getOwner().getPackets().createGroundItem(item);
		    }
            Event showItemDropGloballyEvent = new Event(60000); //60 seconds to show dropped item to all players.
		    showItemDropGloballyEvent.setAction(() => {
				    newGlobalItem(item);
				    showItemDropGloballyEvent.stop();
		    });
            Server.registerEvent(showItemDropGloballyEvent);
	    }
	    private GroundItem itemExists(GroundItem item) {
		    foreach(GroundItem i in items) {
			    if(i.Equals(item)) {
				    return i;
			    }
		    }
		    return null;
	    }
	    public void pickupItem(Player p, int id, Location location) {
		    GroundItem item = itemExists(location, id);
		    if (item != null && p.getSprites().getPrimarySprite() == -1 && p.getSprites().getSecondarySprite() == -1) {
			    if (item.getDefinition().isPlayerBound() && !item.getOwner().Equals(p)) {
				    return;
			    }
			    if (!p.getInventory().addItem(item.getItemId(), item.getItemAmount())) {
				    return;
			    }
			    clearGlobalItem(item);
			    if (item.isRespawn()) {
				    GroundItem i = item;
                    Event itemRespawnEvent = new Event(60000);
                    itemRespawnEvent.setAction(() => {
						GroundItem respawn = new GroundItem(i.getItemId(), i.getItemAmount(), i.getLocation(), null);
						respawn.setRespawn(true);
						respawn.setGlobal(true);
						newGlobalItem(respawn);
                        itemRespawnEvent.stop();
				    });
			    }
		    }
	    }
	    public void clearGlobalItem(GroundItem item) {
		    lock(items) {
			    items.Remove(item);
		    }
		    if(item != null) {
			    foreach(Player p in Server.getPlayerList()) {
				    if (p == null) {
					    continue;
				    }
				    if (p.getLocation().withinDistance(item.getLocation(), 60)) {
					    p.getPackets().clearGroundItem(item);
				    }
			    }
		    }
	    }
	    public void restoreObject(WorldObject worldObject) {
		    if (worldObject != null) {
			    foreach(Player p in Server.getPlayerList()) {
				    if (p != null) {
					    if (p.getLocation().withinDistance(worldObject.getLocation(), 60)) {
						    if (!worldObject.isFire()) {
							    p.getPackets().createObject(worldObject.getOriginalId(), worldObject.getLocation(), worldObject.getFace(), worldObject.getType());
						    } else {
							    p.getPackets().removeObject(worldObject.getLocation(), worldObject.getFace(), worldObject.getType());
						    }
					    }
				    }
			    }
			    if (worldObject.isFire()) {
				    GroundItem item = new GroundItem(592, 1, worldObject.getLocation(), worldObject.getOwner());
				    Server.getGroundItems().newWorldItem(item);
			    }
		    }
		    lock(objects) {
			    objects.Remove(worldObject);
		    }
	    }
 public static void cutTree(Player p, ushort treeId, Location treeLocation, int i, bool newCut, int distance)
 {
     if (!newCut && p.getTemporaryAttribute("cuttingTree") == null) {
         return;
     }
     if (newCut) {
         if (i == 10 || i == 11) { // Magic or Yew tree.
             if (!Server.getGlobalObjects().objectExists(treeId, treeLocation)) {
             //	misc.WriteError(p.getUsername() + " tried to cut a non existing Magic or Yew tree!");
             //	return;
             }
         }
         Tree newTree = new Tree(i, treeId, treeLocation, LOGS[i], LEVEL[i], TREE_NAME[i], XP[i], distance);
         p.setTemporaryAttribute("cuttingTree", newTree);
     }
     Tree treeToCut = (Tree) p.getTemporaryAttribute("cuttingTree");
     if (!canCut(p, treeToCut, null)) {
         resetWoodcutting(p);
         return;
     }
     if (newCut) {
         p.setLastAnimation(new Animation(getAxeAnimation(p)));
         p.setFaceLocation(treeLocation);
         p.getPackets().sendMessage("You begin to swing your axe at the tree..");
     }
     int delay = getCutTime(p, treeToCut.getTreeIndex());
     Event cutTreeEvent = new Event(delay);
     cutTreeEvent.setAction(() => {
         cutTreeEvent.stop();
         if (p.getTemporaryAttribute("cuttingTree") == null) {
             resetWoodcutting(p);
             return;
         }
         Tree tree = (Tree) p.getTemporaryAttribute("cuttingTree");
         if (!canCut(p, treeToCut, tree)) {
             resetWoodcutting(p);
             return;
         }
         Server.getGlobalObjects().lowerHealth(tree.getTreeId(), tree.getTreeLocation());
         if (!Server.getGlobalObjects().originalObjectExists(tree.getTreeId(), tree.getTreeLocation())) {
             resetWoodcutting(p);
             p.setLastAnimation(new Animation(65535));
         }
         if (p.getInventory().addItem(tree.getLog())) {
             p.getPackets().closeInterfaces();
             int index = tree.getTreeIndex();
             string s = index == 1 || index == 3 || index == 8 ? "an" : "a";
             p.getSkills().addXp(Skills.SKILL.WOODCUTTING, tree.getXp());
             if (index == 6 ) {
                 p.getPackets().sendMessage("You retrieve some Hollow bark from the tree.");
             } else {
                 p.getPackets().sendMessage("You cut down " + s + " " + tree.getName() + " log.");
             }
             if (misc.random(3) == 0) {
                 int nestId = misc.random(10) == 0 ? 5073 : 5074;
                 GroundItem g = new GroundItem(nestId, 1, new Location(p.getLocation().getX(), p.getLocation().getY(), p.getLocation().getZ()), p);
                 Server.getGroundItems().newEntityDrop(g);
                 p.getPackets().sendMessage("Something falls out of the tree and lands at your feet.");
             }
         }
         cutTree(p, tree.getTreeId(),  tree.getTreeLocation(), tree.getTreeIndex(), false, tree.getDistance());
     });
     Server.registerEvent(cutTreeEvent);
     if (delay >= 2550) {
         Event treeCuttingAnimationEvent = new Event(2550);
         int time = delay;
         treeCuttingAnimationEvent.setAction(() => {
             time -= 2550;
             if (time <= 0) {
                 treeCuttingAnimationEvent.stop();
             }
             Tree tree = (Tree) p.getTemporaryAttribute("cuttingTree");
             if (!canCut(p, treeToCut, tree)) {
                 treeCuttingAnimationEvent.stop();
                 return;
             }
             p.setFaceLocation(treeLocation);
             p.setLastAnimation(new Animation(getAxeAnimation(p)));
         });
         Server.registerEvent(treeCuttingAnimationEvent);
     }
 }
	    private void handleDropItem(Player player, Packet packet) {
            int item = packet.readShortA();
		    int slot = packet.readShortA();
		    int interfaceId = packet.readLEShort();
		    int childId = packet.readUShort();
		    if (slot > 28 || slot < 0 || player.isDead() || player.getTemporaryAttribute("cantDoAnything") != null) {
			    return;
		    }
		    SkillHandler.resetAllSkills(player);
		    if (player.getInventory().getItemInSlot(slot) == item) {
			    player.getPackets().closeInterfaces();
			    if (ItemData.isPlayerBound(player.getInventory().getItemInSlot(slot))) {
	                Item[] items = {new Item(player.getInventory().getItemInSlot(slot), 1)};
                    //player.getPackets().sendItems(94, 0, 93, items);
	                player.getPackets().modifyText("Are you sure you want to destroy this item?", 94, 3); // Title
	                //player.getPackets().modifyText("Yes", 94, 4); // Yes
	                //player.getPackets().modifyText("No", 94, 5); // No
	                player.getPackets().modifyText("", 94, 10); // Line 1
	                player.getPackets().modifyText("If you wish to get another Fire cape, you must", 94, 11); // Line 2
	                player.getPackets().modifyText("complete the Fight cave minigame again.", 94, 12); // Line 3
	                player.getPackets().modifyText("Fire Cape", 94, 13); // Item name
	                player.getPackets().sendChatboxInterface(94);
				    return;
			    }
			    int id = player.getInventory().getItemInSlot(slot);
			    int amt = player.getInventory().getAmountInSlot(slot);
			    GroundItem i = new GroundItem(id, amt, new Location(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ()), player);
			    if (player.getInventory().deleteItem(id, slot, amt)) {
				    if (!Server.getGroundItems().addToStack(id, amt, player.getLocation(), player)) {
					    Server.getGroundItems().newEntityDrop(i);
				    }
			    }
		    }
	    }
 public void addItemOrGround(int item, int amount)
 {
     /*
      * We try to add the item to the inventory..
      */
     if (addItem(item, amount)) {
         return;
     }
     /*
      * It didn't add the item above, yet we have room for it?..odd.
      */
     if (getTotalFreeSlots() > 0) {
         return;
     }
     /*
      * Add the item to the ground.
      */
     GroundItem g = new GroundItem(item, amount, p.getLocation(), p);
     Server.getGroundItems().newEntityDrop(g);
 }
        public override void dropLoot() {
		    Entity killer = this.getKiller();
		    Player p = killer is Player ? (Player) killer : null;
		    NpcDrop drop = this.npcDef.getDrop();
		    if(killer == null || p == null) {
			    return;
		    }
		    if (drop != null) {
			    try {
				    List<Item> drops = new List<Item>();
				    int random = misc.random(100);
				    int random2 = 100 - random;
				    if (random2 == 0){
					    random2++;
				    }
				    if (random2 < 25) { // 25% - semi rare
                        if (drop.getUncommonDrops() != null && drop.getUncommonDrops().Count > 0)
                        	drops.Add(drop.getUncommonDrops()[misc.random(drop.getUncommonDrops().Count - 1)]);
				    } else if (random2 >= 25 && random2 < 95) { // 65%  common
                        if (drop.getCommonDrops() != null && drop.getCommonDrops().Count > 0)
						    drops.Add(drop.getCommonDrops()[misc.random(drop.getCommonDrops().Count - 1)]);
				    } else if (random2 >= 95) { // 5% - rare
                        if (drop.getRareDrops() != null && drop.getRareDrops().Count > 0)
						    drops.Add(drop.getRareDrops()[misc.random(drop.getRareDrops().Count - 1)]);
				    }
				    random = random2;
				    if (drop.getAlwaysDrops().Count != 0) {
					    foreach (Item d in drop.getAlwaysDrops()) {
						    drops.Add(d);
					    }
				    }
				    foreach(Item each_drop in drops) {
                        int amount = each_drop.getItemAmount();
                        int itemId = each_drop.getItemId();
					    if (amount < 0) {
						    amount = misc.random((amount - (amount * 2)));
						    if (amount == 0) {
							    amount = 1;
						    }
					    }
					    if (itemId == 8844) { // defender
						    itemId = WarriorGuildData.DEFENDERS[((Player)killer).getDefenderWave()];
					    }
					    bool stackable = ItemData.forId(itemId).isNoted() || ItemData.forId(itemId).isStackable();
					    if (stackable || (!stackable && amount == 1)) {
						    if (Server.getGroundItems().addToStack(itemId, amount, this.getLocation(), p)) {

						    } else {
							    GroundItem item = new GroundItem(itemId, amount, this.getLocation(), p);
							    Server.getGroundItems().newEntityDrop(item);
						    }
					    } else {
						    for (int i = 0; i < amount; i++) {
							    GroundItem item = new GroundItem(itemId, 1, this.getLocation(), p);
							    Server.getGroundItems().newEntityDrop(item);
						    }
					    }
				    }
			    } catch (Exception e) {
                    misc.WriteError("Error at npc dropLoot, msg=" + e.Message);
			    }
		    }
	    }
	    public static void createGroundArrow(Entity killer, Entity target, int arrow) {
		    if (misc.random(1) == 1) {
			    return;
		    }
		    GroundItem i = new GroundItem(arrow, 1, target.getLocation(), ((Player)killer));
		    if (Server.getGroundItems().addToStack(arrow, 1, target.getLocation(), ((Player)killer))) {
			    return;
		    } else {
			    Server.getGroundItems().newEntityDrop(i);
		    }
	    }
        public override void dropLoot()
        {
            Entity whoKiller = this.getKiller();
            /*
             * Replaced if killer is [Npc] to [this] (this player).
             * Also I couldn't do it in 1 line of coding because
             * this.getKiller() may change while being casted to Player
             * even if it was Npc (then it would be based on luck timing).
             */
            Player killer = whoKiller is Npc ? this : (Player)(whoKiller);

            int amountToKeep = isSkulled() ? 0 : 3;
            if (prayers.isProtectItem()) {
                amountToKeep = isSkulled() ? 1 : 4;
            }

            int[] protectedItems = new int[amountToKeep];
            bool[] saved = new bool[amountToKeep];
            if (protectedItems.Length > 0) {
                protectedItems[0] = ProtectedItems.getProtectedItem1(this)[0];
            }
            if (protectedItems.Length > 1) {
                protectedItems[1] = ProtectedItems.getProtectedItem2(this)[0];
            }
            if (protectedItems.Length > 2) {
                protectedItems[2] = ProtectedItems.getProtectedItem3(this)[0];
            }
            if (protectedItems.Length > 3) {
                protectedItems[3] = ProtectedItems.getProtectedItem4(this)[0];
            }
            bool save = false;

            foreach (Item item in getInventory().getItems()) {
                save = false;

                if (item.getItemId() > 0) {
                    for (int j = 0; j < protectedItems.Length; j++) {
                        if (amountToKeep > 0 && protectedItems[j] > 0) {
                            if (!saved[j] && !save) {
                                if (item.getItemId() == protectedItems[j] && item.getItemAmount() == 1) {
                                    saved[j] = true;
                                    save = true;
                                }
                                if (item.getItemId() == protectedItems[j] && item.getItemAmount() > 1) {
                                    item.setItemAmount(item.getItemAmount() - 1);
                                    saved[j] = true;
                                    save = true;
                                }
                            }
                        }
                    }
                    if (!save) {
                        int itemId = item.getItemId();
                        //If inventory items have to break on dropped death, this is where they go
                        if (itemId >= 4708 && itemId <= 4759) {
                            itemId = BrokenBarrows.getBrokenId(itemId);
                        }
                        Server.getGroundItems().newEntityDrop(new GroundItem(itemId, item.getItemAmount(), this.getLocation(), item.getDefinition().isPlayerBound() ? this : killer));
                    }
                }
            }
            inventory.clearAll();
            saved = new bool[amountToKeep];
            foreach (ItemData.EQUIP equip in Enum.GetValues(typeof(ItemData.EQUIP))) {
                if (equip == ItemData.EQUIP.NOTHING) continue;
                save = false;
                Item item = this.getEquipment().getSlot(equip);
                if (item.getItemId() > 0) {
                    for (int j = 0; j < protectedItems.Length; j++) {
                        if (amountToKeep > 0 && protectedItems[j] > -1) {
                            if (!saved[j] && !save) {
                                if (item.getItemId() == protectedItems[j] && item.getItemAmount() == 1) {
                                    saved[j] = true;
                                    save = true;
                                }
                                if (item.getItemId() == protectedItems[j] && item.getItemAmount() > 1) {
                                    item.setItemAmount(item.getItemAmount() - 1);
                                    saved[j] = true;
                                    save = true;
                                }
                            }
                        }
                    }
                    if (!save) {
                        //If equipped items have to break on dropped death, this is where they go
                        int itemId = item.getItemId();
                        if (itemId >= 4708 && itemId <= 4759) {
                            itemId = BrokenBarrows.getBrokenId(itemId);
                        }
                        Server.getGroundItems().newEntityDrop(new GroundItem(itemId, item.getItemAmount(), this.getLocation(), item.getDefinition().isPlayerBound() ? this : killer));
                    }
                }
            }
            equipment.clearAll();
            GroundItem bones = new GroundItem(526, 1, this.getLocation(), killer);
            Server.getGroundItems().newEntityDrop(bones);
            inventory.setProtectedItems(protectedItems);
        }
 public void clearGroundItem(GroundItem item)
 {
     if (item != null)
     {
         sendCoords(item.getLocation());
         connection.SendPacket(new PacketBuilder().setId(240)
             .addByte((byte)0)
             .addUShort(item.getItemId()).toPacket());
     }
 }
 public void createGroundItem2(GroundItem item)
 {
     if (item != null)
     {
         sendCoords(item.getLocation());
         connection.SendPacket(new PacketBuilder().setId(135)
             .addUShortA(item.getOwner().getIndex())
             .addByteC((byte)0)
             .addLEShort(item.getItemAmount())
             .addLEShort(item.getItemId()).toPacket());
     }
 }