
Originally Posted by
Master Sheikh
This. Someone who has downloaded the source just post the method here and I'll fix it.
Code:
package org.fury.rs2.model.item;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.fury.rs2.model.Item;
import org.fury.rs2.model.Location;
import org.fury.rs2.model.World;
import org.fury.rs2.model.player.Player;
import org.fury.rs2.model.region.Region;
/**
* Handles all of the ground items
*
* @author Arithium
* @author kLeptO <http://www.rune-server.ee/members/klepto/>
*/
public class GroundItemHandler {
/**
* A list containing all of the ground items
*/
private static final List<GroundItem> groundItems = new CopyOnWriteArrayList<GroundItem>();
/**
* Registers a new ground item.
*
* @param groundItem
* the ground item
*/
public static boolean register(GroundItem groundItem) {
if (find(groundItem.getLocation(), groundItem.getItem().getId()) != null) {
if (groundItem.getItem().getDefinition().isStackable()) {
find(groundItem.getLocation(), groundItem.getItem().getId()).setItem(new Item(groundItem.getItem().getId(), find(groundItem.getLocation(), groundItem.getItem().getId()).getItem().getCount() + groundItem.getItem().getCount()));
return false;
}
} else {
groundItems.add(groundItem);
}
return true;
}
/**
* Registers a new global ground item.
*
* @param groundItem
* the ground item
*/
public static void registerGlobalItem(GroundItem groundItem) {
groundItems.add(groundItem);
sendGroundItem(groundItem);
}
/**
* Unregisters an existing ground item.
*
* @param groundItem
* the ground item
*/
public static void deregister(GroundItem groundItem) {
groundItems.remove(groundItem);
}
/**
* Updates all visible ground items.
*/
public static void updateGroundItems() {
for (GroundItem groundItem : groundItems) {
if (groundItem == null) {
continue;
} else {
groundItem.decreaseTimer();
}
if (groundItem.getTimer() == GroundItem.LIFE_SPAN / 2) {
sendGroundItem(groundItem);
} else if (groundItem.getTimer() == 0) {
sendRemoveGroundItem(groundItem);
deregister(groundItem);
}
}
}
/**
* Finds the ground item by a given location.
*
* @param location
* the item location
*
* @param itemIndex
* the item index
*/
public static GroundItem find(Location location, int itemIndex) {
for (GroundItem groundItem : groundItems) {
if (groundItem == null) {
continue;
}
if (groundItem.getLocation().equals(location) && groundItem.getItem().getId() == itemIndex) {
return groundItem;
}
}
return null;
}
/**
* Sends a ground item to all players except it's owner.
*
* @param groundItem
* the ground item
*/
public static void sendGroundItem(GroundItem groundItem) {
Region region = World.getWorld().getRegionManager().getRegionByLocation(groundItem.getLocation());
if (region == null) {
return;
}
for (Player player : region.getPlayers()) {
if (player == null) {
continue;
}
if (groundItem.getTimer() <= GroundItem.LIFE_SPAN || (groundItem.getTimer() <= 100 && !player.getName().equals(groundItem.getOwner()))) {
player.getPacketSender().sendGroundItem(groundItem);
}
}
}
/**
* Sends a remove ground item packet to all players.
*
* @param groundItem
* the ground item
*/
public static void sendRemoveGroundItem(GroundItem groundItem) {
Region region = World.getWorld().getRegionManager().getRegionByLocation(groundItem.getLocation());
if (region == null) {
return;
}
for (Player player : region.getPlayers()) {
if (player == null) {
continue;
}
player.getPacketSender().sendRemoveGroundItem(groundItem);
}
}
/**
* Sends all visible ground items to the player.
*
* @param player
* the player
*/
public static void sendGroundItems(Player player) {
for (GroundItem groundItem : groundItems) {
if (groundItem == null) {
continue;
}
if (!groundItem.getLocation().withinRange(player.getLocation(), 32)) {
continue;
}
if (groundItem.getTimer() <= GroundItem.LIFE_SPAN / 2 || player.getName().equalsIgnoreCase(groundItem.getOwner())) {
player.getPacketSender().sendGroundItem(groundItem);
}
}
}
/**
* Sends remove packet for all visible ground items to the player.
*
* @param player
* the player
*/
public static void sendRemoveGroundItems(Player player) {
for (GroundItem groundItem : groundItems) {
if (groundItem == null) {
continue;
}
if (groundItem.getLocation().withinRange(player.getLocation(), 32)) {
continue;
}
if (groundItem.getTimer() <= GroundItem.LIFE_SPAN / 2 || player.getName().equalsIgnoreCase(groundItem.getOwner())) {
player.getPacketSender().sendRemoveGroundItem(groundItem);
}
}
}
}