2011-05-15 19:33:03 +02:00
|
|
|
package com.Acrobot.ChestShop.Shop;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-05-21 19:55:55 +02:00
|
|
|
import com.Acrobot.ChestShop.ChestShop;
|
2011-05-15 19:33:03 +02:00
|
|
|
import com.Acrobot.ChestShop.Chests.ChestObject;
|
2011-06-09 22:54:01 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Config;
|
2011-06-11 17:36:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Language;
|
|
|
|
import com.Acrobot.ChestShop.Config.Property;
|
2011-05-21 19:55:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Economy;
|
2011-05-29 13:25:25 +02:00
|
|
|
import com.Acrobot.ChestShop.Logging.Logging;
|
2011-06-23 23:25:34 +02:00
|
|
|
import com.Acrobot.ChestShop.Permission;
|
2011-05-21 19:55:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.InventoryUtil;
|
|
|
|
import com.Acrobot.ChestShop.Utils.SignUtil;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.block.Sign;
|
2011-05-21 19:55:55 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class Shop {
|
2011-05-29 13:25:25 +02:00
|
|
|
public ItemStack stock;
|
2011-06-19 23:52:36 +02:00
|
|
|
public short durability;
|
2011-05-29 13:25:25 +02:00
|
|
|
public int stockAmount;
|
|
|
|
public ChestObject chest;
|
|
|
|
public float buyPrice;
|
|
|
|
public float sellPrice;
|
|
|
|
public String owner;
|
|
|
|
|
|
|
|
public Shop(ChestObject chest, Sign sign, ItemStack... itemStacks) {
|
2011-05-21 19:55:55 +02:00
|
|
|
this.stock = itemStacks[0];
|
2011-06-19 23:52:36 +02:00
|
|
|
this.durability = stock.getDurability();
|
2011-05-21 19:55:55 +02:00
|
|
|
this.chest = chest;
|
|
|
|
this.buyPrice = SignUtil.buyPrice(sign.getLine(2));
|
|
|
|
this.sellPrice = SignUtil.sellPrice(sign.getLine(2));
|
|
|
|
this.owner = sign.getLine(0);
|
|
|
|
this.stockAmount = SignUtil.itemAmount(sign.getLine(1));
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public boolean buy(Player player) {
|
2011-06-09 22:54:01 +02:00
|
|
|
if (chest == null && !isAdminShop()) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NO_CHEST_DETECTED));
|
2011-05-29 13:25:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (buyPrice == -1) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NO_BUYING_HERE));
|
2011-05-21 19:55:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-06-23 23:25:34 +02:00
|
|
|
if(!Permission.has(player, Permission.BUY)){
|
|
|
|
player.sendMessage(Config.getLocal(Language.NO_PERMISSION));
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-09 22:54:01 +02:00
|
|
|
String playerName = player.getName();
|
|
|
|
if (!Economy.hasEnough(playerName, buyPrice)) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_MONEY));
|
2011-06-09 22:54:01 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!stockFitsPlayer(player)) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_SPACE_IN_INVENTORY));
|
2011-05-21 19:55:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String materialName = stock.getType().name();
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
if (!isAdminShop() && !hasEnoughStock()) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_STOCK));
|
|
|
|
sendMessageToOwner(Config.getLocal(Language.NOT_ENOUGH_STOCK_IN_YOUR_SHOP).replace("%material", materialName));
|
2011-05-21 19:55:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
String account = getOwnerAccount();
|
|
|
|
if (!account.isEmpty() && Economy.hasAccount(account)) {
|
|
|
|
Economy.add(account, buyPrice);
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
2011-06-09 22:54:01 +02:00
|
|
|
Economy.substract(playerName, buyPrice);
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
if (!isAdminShop()) {
|
2011-06-19 23:52:36 +02:00
|
|
|
chest.removeItem(stock, durability, stockAmount);
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
String formatedPrice = Economy.formatBalance(buyPrice);
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.YOU_BOUGHT_FROM_SHOP)
|
2011-05-21 19:55:55 +02:00
|
|
|
.replace("%amount", String.valueOf(stockAmount))
|
|
|
|
.replace("%item", materialName)
|
|
|
|
.replace("%owner", owner)
|
|
|
|
.replace("%price", formatedPrice));
|
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
InventoryUtil.add(player.getInventory(), stock, stockAmount);
|
|
|
|
Logging.logTransaction(true, this, player);
|
|
|
|
player.updateInventory();
|
|
|
|
|
2011-06-11 17:36:55 +02:00
|
|
|
sendMessageToOwner(Config.getLocal(Language.SOMEBODY_BOUGHT_FROM_YOUR_SHOP)
|
2011-05-21 19:55:55 +02:00
|
|
|
.replace("%amount", String.valueOf(stockAmount))
|
|
|
|
.replace("%item", materialName)
|
2011-06-09 22:54:01 +02:00
|
|
|
.replace("%buyer", playerName)
|
2011-05-21 19:55:55 +02:00
|
|
|
.replace("%price", formatedPrice));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public boolean sell(Player player) {
|
2011-06-09 22:54:01 +02:00
|
|
|
if (chest == null && !isAdminShop()) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NO_CHEST_DETECTED));
|
2011-05-29 13:25:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (sellPrice == -1) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NO_SELLING_HERE));
|
2011-05-21 19:55:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-06-23 23:25:34 +02:00
|
|
|
if(!Permission.has(player, Permission.SELL)){
|
|
|
|
player.sendMessage(Config.getLocal(Language.NO_PERMISSION));
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-09 22:54:01 +02:00
|
|
|
String account = getOwnerAccount();
|
|
|
|
boolean accountExists = !account.isEmpty() && Economy.hasAccount(account);
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
if (accountExists) {
|
|
|
|
if (!Economy.hasEnough(account, sellPrice)) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_MONEY_SHOP));
|
2011-06-09 22:54:01 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isAdminShop() && !stockFitsChest(chest)) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_SPACE_IN_CHEST));
|
2011-05-21 19:55:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-19 23:52:36 +02:00
|
|
|
if (InventoryUtil.amount(player.getInventory(), stock, durability) < stockAmount) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_ITEMS_TO_SELL));
|
2011-05-21 19:55:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
|
|
|
|
if (accountExists) {
|
|
|
|
Economy.substract(account, sellPrice);
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
if (!isAdminShop()) {
|
2011-06-14 15:07:44 +02:00
|
|
|
chest.addItem(stock, stockAmount);
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Economy.add(player.getName(), sellPrice);
|
|
|
|
|
|
|
|
String materialName = stock.getType().name();
|
|
|
|
String formatedBalance = Economy.formatBalance(sellPrice);
|
|
|
|
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.YOU_SOLD_TO_SHOP)
|
2011-05-21 19:55:55 +02:00
|
|
|
.replace("%amount", String.valueOf(stockAmount))
|
|
|
|
.replace("%item", materialName)
|
|
|
|
.replace("%buyer", owner)
|
|
|
|
.replace("%price", formatedBalance));
|
|
|
|
|
2011-06-19 23:52:36 +02:00
|
|
|
InventoryUtil.remove(player.getInventory(), stock, stockAmount, durability);
|
2011-06-09 22:54:01 +02:00
|
|
|
Logging.logTransaction(false, this, player);
|
|
|
|
player.updateInventory();
|
|
|
|
|
2011-06-11 17:36:55 +02:00
|
|
|
sendMessageToOwner(Config.getLocal(Language.SOMEBODY_SOLD_TO_YOUR_SHOP)
|
2011-05-21 19:55:55 +02:00
|
|
|
.replace("%amount", String.valueOf(stockAmount))
|
|
|
|
.replace("%item", materialName)
|
|
|
|
.replace("%seller", player.getName())
|
|
|
|
.replace("%price", formatedBalance));
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-05-21 19:55:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
private String getOwnerAccount() {
|
2011-05-29 13:25:25 +02:00
|
|
|
if (SignUtil.isAdminShop(owner)) {
|
2011-06-11 17:36:55 +02:00
|
|
|
return Config.getString(Property.SERVER_ECONOMY_ACCOUNT);
|
2011-05-29 13:25:25 +02:00
|
|
|
} else {
|
2011-05-21 19:55:55 +02:00
|
|
|
return owner;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
private boolean isAdminShop() {
|
2011-05-21 19:55:55 +02:00
|
|
|
return SignUtil.isAdminShop(owner);
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
private boolean hasEnoughStock() {
|
2011-06-19 23:52:36 +02:00
|
|
|
return chest.hasEnough(stock, stockAmount, durability);
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
private boolean stockFitsPlayer(Player player) {
|
2011-06-19 23:52:36 +02:00
|
|
|
return InventoryUtil.fits(player.getInventory(), stock, stockAmount, durability) <= 0;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
private boolean stockFitsChest(ChestObject chest) {
|
2011-06-19 23:52:36 +02:00
|
|
|
return chest.fits(stock, stockAmount, durability);
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
private void sendMessageToOwner(String msg) {
|
|
|
|
if (!isAdminShop()) {
|
2011-05-21 19:55:55 +02:00
|
|
|
Player player = ChestShop.getBukkitServer().getPlayer(owner);
|
2011-05-29 13:25:25 +02:00
|
|
|
if (player != null) {
|
2011-05-21 19:55:55 +02:00
|
|
|
player.sendMessage(msg);
|
|
|
|
}
|
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
}
|