[/code]
package org.apollo.game.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apollo.game.event.impl.GroundItemEvent;
import org.apollo.game.event.impl.RemoveGroundItemEvent;
import org.apollo.game.event.impl.PositionEvent;
import org.apollo.game.scheduling.impl.ProcessGroundItems Task;
public class GroundItem {
/**
* The amount of pulses before a ground item turns global.
*/
public static final int GLOBAL_PULSES = 250;
/**
* The task that processes all registered ground items.
*/
private static ProcessGroundItemsTask processTask;
/**
* The name of the player who controls this item.
*/
private final String controllerName;
/**
* The item that is on the floor.
*/
private final Item item;
/**
* The position of the item.
*/
private final Position position;
/**
* The x region coordinate.
*/
private final int regionX;
/**
* The y region coordinate.
*/
private final int regionY;
/**
* The amount of remaining pulses until this item disappears.
*/
private int pulses;
/**
* Creates a private ground item.
* @param controllerName The controller of this item.
* @param item The item.
* @param position The position.
*/
public GroundItem(String controllerName, Item item, Position position, int regionX, int regionY) {
pulses = /*item.getDefinition().isUntradeable() ? 300 :*/ 350;
// Tradeable items appear to other players 60 seconds after being dropped, and disappear
// after another 150 seconds if not picked up. Untradeable items simply disappear 180 seconds after being dropped.
this.controllerName = controllerName;
this.item = item;
this.position = position;
this.regionX = regionX;
this.regionY = regionY;
}
/**
* The map of the list of ground items on a position.
*/
private static final Map<Position, List<GroundItem>> groundItems = new HashMap<Position, List<GroundItem>>();
/**
* Gets a the map of ground items.
*/
public static Map<Position, List<GroundItem>> getGroundItems() {
return groundItems;
}
/**
* Gets a the map of ground items for a position.
* @param position The position.
*/
public static List<GroundItem> getGroundItems(Position position) {
return groundItems.get(position);
}
/**
* Gets a ground item by its id.
* @param id The id.
* @return {@code null} if there is no ground item of this id on this position, the ground item otherwise.
*/
public static GroundItem getGroundItem(Position position, int id) {
if (groundItems.get(position) != null) {
for (GroundItem groundItem : groundItems.get(position)) {
if (groundItem.getItem().getId() == id) {
return groundItem;
}
}
}
return null;
}
public static void createGroundItem(final Player p, int itemId, int itemAmount, Position pos) {
Item item = new Item(itemId, itemAmount);
boolean stackable = item.getDefinition().isStackable();
if(stackable) {
GroundItem groundItem = new GroundItem(p.getName(), item, pos, p.getLastKnownRegion().getTopLeftRegionX(), p.getLastKnownRegion().getTopLeftRegionY());
p.send(new PositionEvent(p.getPosition()));
p.send(new GroundItemEvent(itemId, itemAmount));
updateMap(groundItem, item);
} else {
for(int i = 0; i < itemAmount; i++) {
GroundItem groundItem = new GroundItem(p.getName(), new Item(itemId, 1), pos, p.getLastKnownRegion().getTopLeftRegionX(), p.getLastKnownRegion().getTopLeftRegionY());
p.send(new PositionEvent(p.getPosition()));
p.send(new GroundItemEvent(itemId, 1));
updateMap(groundItem, new Item(itemId, 1));
}
}
}
public static void updateMap(GroundItem g, Item item) {
if(groundItems.get(g.getPosition()) == null) {
groundItems.put(g.getPosition(), new ArrayList<GroundItem>());
}
groundItems.get(g.getPosition()).add(g);
processTaskCheck();
}
public static void processTaskCheck() {
if (processTask == null) {
processTask = new ProcessGroundItemsTask();
World.getWorld().schedule(processTask);
}
}
/**
* Deletes and unregisters a ground item.
* @param groundItem The ground item to unregister.
*/
public static void deleteGroundItem(GroundItem groundItem) {
groundItems.get(groundItem.getPosition()).remove(g roundItem);
if (groundItems.get(groundItem.getPosition()).isEmpty ()) {
groundItems.remove(groundItem.getPosition());
}
boolean global = groundItem.getPulses() <= GLOBAL_PULSES /*&& !groundItem.getItem().getDefinition().*/;
for (Player player : World.getWorld().getPlayerRepository()) {
//if (global || player.getName().equals(groundItem.getControllerNa me()))
//if (player.getPosition().isWithinDistance(groundItem. getPosition(), 60)) {
player.send(new PositionEvent(groundItem.getPosition()));
player.send(new RemoveGroundItemEvent(groundItem.getItem().getId() , groundItem.getItem().getAmount()));
//}
}
}
/*
* Handles login
*
*/
public static void login(Player player) {
Collection collection = getGroundItems().values();
Iterator iterator = collection.iterator();
while(iterator.hasNext()) {
List list = (List) iterator.next();
if(list == null) {
return;
}
for(int i = 0; i < list.size(); i++) {
GroundItem g = (GroundItem) list.get(i);
if(g == null) {
continue;
}
if(g.controllerName.equals(player.getName())) {
player.send(new PositionEvent(g.getPosition()));
player.send(new GroundItemEvent(g.getItem().getId(), g.getItem().getAmount()));
}
}
}
}
/*
* Handles pickupItem
*
*/
public static void pickupItem(Player p, Position position, int itemId) {
if(p.getInventory().freeSlots() <= 0) {
p.sendMessage("Not enough inventory slots");
return;
}
List list = getGroundItems(position);
if(list == null) {
return;
}
for(int i = 0; i < list.size(); i++) {
GroundItem groundItem = (GroundItem) list.get(i);
if(groundItem == null) {
continue;
}
if(groundItem.item.getId() == itemId) {
deleteGroundItem(groundItem);
p.getInventory().add(groundItem.item);
return;
}
}
}
/**
* Gets the controller's name.
* @return The controller's name.
*/
public String getControllerName() {
return controllerName;
}
/**
* Gets the pulses.
* @return The pulses.
*/
public int getPulses() {
return pulses;
}
/**
* Decreases the pulses.
* @param pulses The pulses to decrease by.
*/
public void decreasePulses(int pulses) {
this.pulses -= pulses;
}
/**
* Gets the item.
* @return The item.
*/
public Item getItem() {
return item;
}
/**
* Gets the position.
* @return The position.
*/
public Position getPosition() {
return position;
}
/**
* Gets the regionX.
* @return The regionX.
*/
public int getRegionX() {
return regionX;
}
/**
* Gets the regionY.
* @return The regionY.
*/
public int getRegionY() {
return regionY;
}
}
[/code]