Code:
package com.divine.fate.main.world.plugins;
import java.util.HashMap;
import com.divine.fate.main.cycle.CycleEvent;
import com.divine.fate.main.cycle.CycleEventContainer;
import com.divine.fate.main.cycle.CycleEventHandler;
import com.divine.fate.main.model.entity.players.Player;
import com.divine.fate.main.model.entity.players.PlayerHandler;
/**
* Handles the lending of an item
* @author Final Project
*
*/
public class ItemLending {
/**
* Creates an instance of the player
*/
private Player player;
/**
* Constructs the class for the player
* @param player
* the player
*/
public ItemLending(Player player) {
this.player = player;
}
public enum LendItems {
ABYSSAL_WHIP(4151, 13444);
private final int id, lendId;
public int getItemId() {
return id;
}
public int getLendId() {
return lendId;
}
private LendItems(final int itemId, final int lendId) {
this.id = itemId;
this.lendId = lendId;
}
static HashMap<Integer, LendItems> lend = new HashMap<Integer, LendItems>();
static {
for (final LendItems l : lend.values())
lend.put(l.getItemId(), l);
}
}
/**
* If item being lent is a lendable item
* @param itemId
* the item being lent
* @return
* if is lendable
*/
private boolean canLend(int itemId) {
return LendItems.lend.containsKey(itemId);
}
/**
* Determines the lend times from hours to seconds
*/
private int[][] lendTimes = {
{ 1, 3600 }, { 2, 7200 },
{ 3, 10800 }, { 4, 14400 },
{ 5, 18000 }, { 6, 21600 },
{ 7, 25200 }, { 8, 28800 },
{ 9, 32400 }, { 10, 36000 },
{ 11, 39600 }, { 12, },
{ 13, }, { 14, },
{ 15, }, { 16, },
{ 17, }, { 18, },
{ 19, }, { 20, },
{ 21, }, { 22, },
{ 23, }, { 24, },
};
/**
* Grabs the lend time
* @param time
* the lend time in hours
* @return
* the lend time in seconds
*/
private int getLendTimes(int time) {
for (int i = 0; i < lendTimes.length; i++) {
if (time == lendTimes[i][0]) {
return lendTimes[i][1];
}
}
return 0;
}
/**
* Lends the item to a player
* @param itemId
* the item to be lent
* @param playerId
* the player to lend item to
*/
public void lendItem(int itemId, int playerId) {
Player other = PlayerHandler.players[playerId];
if (other.getItems().freeSlots() < 1) {
player.sendMessage("The other player doesn't have enough inventory space!");
return;
}
if (other.hasLentItem) {
player.sendMessage("The other player already has a lent item.");
other.sendMessage("You already have a lent item.");
return;
}
if (canLend(itemId)) {
for (final LendItems lendItems : LendItems.values()) {
if (itemId == lendItems.getItemId()) {
if (player.getItems().playerHasItem(itemId, 1)) {
other.getDH().sendItems(5, lendItems.getItemId());
player.sendMessage("Sending lent item request...");
break;
}
}
}
} else {
player.sendMessage("This item cannot be lent.");
}
}
/**
* Handles the time deduction for a lent item
*/
public void handleLendTime() {
CycleEventHandler.getSingleton().addEvent(this, new CycleEvent() {
@Override
public void execute(CycleEventContainer container) {
if (player.lendTime <= 0) {
container.stop();
return;
} else if (player.lendTime >= 0) {
player.lendTime --;
}
}
@Override
public void stop() {
player.lendTime = 0;
}
}, getLendTimes(player.lendTime));
}
/**
* Handles the dropping of a lent item
* @param itemId
* the lent item being dropped
* @param amount
* the amount being dropped
*/
public void dropLentItem(int itemId, int amount) {
for (final LendItems lendItems : LendItems.values()) {
if (itemId == lendItems.getItemId()) {
player.getItems().deleteItem(itemId, amount);
player.lendTime = 0;
break;
}
}
}
public void handleDialogueButton(int buttonId) {
switch (buttonId) {
case 9157:
if (player.itemAction == 1) {
//usedOn.getItems().addItem(lendItems.getLendId(), 1);
//player.getItems().deleteItem(lendItems.getItemId(), 1);
}
break;
}
}
}