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;
|
2012-01-25 16:32:34 +01:00
|
|
|
import com.Acrobot.ChestShop.Economy.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-07-05 19:08:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uInventory;
|
|
|
|
import com.Acrobot.ChestShop.Utils.uSign;
|
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-07-23 21:00:47 +02:00
|
|
|
private final short durability;
|
|
|
|
private final ChestObject chest;
|
2011-08-13 12:08:34 +02:00
|
|
|
|
|
|
|
public final ItemStack stock;
|
2012-02-16 19:09:37 +01:00
|
|
|
public int stockAmount;
|
2011-07-23 21:00:47 +02:00
|
|
|
public final String owner;
|
2012-03-17 15:00:25 +01:00
|
|
|
public final Sign sign;
|
2011-07-23 21:00:47 +02:00
|
|
|
|
2012-04-19 15:49:48 +02:00
|
|
|
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.owner = sign.getLine(0);
|
2011-07-05 19:08:55 +02:00
|
|
|
this.stockAmount = uSign.itemAmount(sign.getLine(1));
|
2012-01-25 16:32:34 +01:00
|
|
|
this.sign = sign;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
public void buy(Player player) {
|
2012-04-19 15:49:48 +02:00
|
|
|
double buyPrice = uSign.buyPrice(sign.getLine(2));
|
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-07-23 21:00:47 +02:00
|
|
|
return;
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
|
|
|
if (buyPrice == -1) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NO_BUYING_HERE));
|
2011-07-23 21:00:47 +02:00
|
|
|
return;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
2011-08-13 12:08:34 +02:00
|
|
|
if (!Permission.has(player, Permission.BUY) && !Permission.has(player, Permission.BUY_ID + Integer.toString(stock.getTypeId()))) {
|
2011-06-23 23:25:34 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NO_PERMISSION));
|
2011-07-23 21:00:47 +02:00
|
|
|
return;
|
2011-06-23 23:25:34 +02:00
|
|
|
}
|
2011-06-09 22:54:01 +02:00
|
|
|
String playerName = player.getName();
|
|
|
|
if (!Economy.hasEnough(playerName, buyPrice)) {
|
2012-04-19 15:49:48 +02:00
|
|
|
int items = calculateItemAmount(Economy.balance(playerName), buyPrice);
|
2012-02-16 19:09:37 +01:00
|
|
|
if (!Config.getBoolean(Property.ALLOW_PARTIAL_TRANSACTIONS) || items < 1) {
|
|
|
|
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_MONEY));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
buyPrice = (buyPrice / stockAmount) * items;
|
|
|
|
stockAmount = items;
|
|
|
|
}
|
2011-06-09 22:54:01 +02:00
|
|
|
}
|
|
|
|
if (!stockFitsPlayer(player)) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_SPACE_IN_INVENTORY));
|
2011-07-23 21:00:47 +02:00
|
|
|
return;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-04-19 16:13:42 +02:00
|
|
|
String materialName = uSign.capitalizeFirst(stock.getType().name());
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
if (!isAdminShop() && !hasEnoughStock()) {
|
2012-02-16 19:09:37 +01:00
|
|
|
int items = stockAmount(stock, durability);
|
|
|
|
if (!Config.getBoolean(Property.ALLOW_PARTIAL_TRANSACTIONS) || items < 1) {
|
|
|
|
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_STOCK));
|
|
|
|
|
|
|
|
if (!Config.getBoolean(Property.SHOW_MESSAGE_OUT_OF_STOCK)) return;
|
|
|
|
sendMessageToOwner(Config.getLocal(Language.NOT_ENOUGH_STOCK_IN_YOUR_SHOP).replace("%material", materialName));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
buyPrice = (buyPrice / stockAmount) * items;
|
|
|
|
stockAmount = items;
|
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
String account = getOwnerAccount();
|
2012-01-25 16:32:34 +01:00
|
|
|
if (!account.isEmpty() && Economy.hasAccount(account)) {
|
|
|
|
if (!isAdminShop()) Economy.add(account, buyPrice);
|
|
|
|
else Economy.addServer(account, buyPrice);
|
|
|
|
}
|
2011-07-23 21:00:47 +02:00
|
|
|
|
2012-01-25 16:32:34 +01:00
|
|
|
Economy.subtract(playerName, buyPrice);
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
if (!isAdminShop()) chest.removeItem(stock, durability, stockAmount);
|
|
|
|
|
2011-05-21 19:55:55 +02:00
|
|
|
String formatedPrice = Economy.formatBalance(buyPrice);
|
2011-08-13 12:08:34 +02:00
|
|
|
if (Config.getBoolean(Property.SHOW_TRANSACTION_INFORMATION_CLIENT)) {
|
|
|
|
player.sendMessage(Config.getLocal(Language.YOU_BOUGHT_FROM_SHOP)
|
|
|
|
.replace("%amount", String.valueOf(stockAmount))
|
|
|
|
.replace("%item", materialName)
|
|
|
|
.replace("%owner", owner)
|
|
|
|
.replace("%price", formatedPrice));
|
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-07-05 19:08:55 +02:00
|
|
|
uInventory.add(player.getInventory(), stock, stockAmount);
|
2012-04-19 15:49:48 +02:00
|
|
|
Logging.logTransaction(true, this, buyPrice, player);
|
2011-06-09 22:54:01 +02:00
|
|
|
player.updateInventory();
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
if (Config.getBoolean(Property.SHOW_TRANSACTION_INFORMATION_OWNER)) {
|
|
|
|
sendMessageToOwner(Config.getLocal(Language.SOMEBODY_BOUGHT_FROM_YOUR_SHOP)
|
|
|
|
.replace("%amount", String.valueOf(stockAmount))
|
|
|
|
.replace("%item", materialName)
|
|
|
|
.replace("%buyer", playerName)
|
|
|
|
.replace("%price", formatedPrice));
|
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
public void sell(Player player) {
|
2012-04-19 15:49:48 +02:00
|
|
|
double sellPrice = uSign.sellPrice(sign.getLine(2));
|
|
|
|
|
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-07-23 21:00:47 +02:00
|
|
|
return;
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
|
|
|
if (sellPrice == -1) {
|
2011-06-11 17:36:55 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NO_SELLING_HERE));
|
2011-07-23 21:00:47 +02:00
|
|
|
return;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
2011-08-13 12:08:34 +02:00
|
|
|
if (!Permission.has(player, Permission.SELL) && !Permission.has(player, Permission.SELL_ID + Integer.toString(stock.getTypeId()))) {
|
2011-06-23 23:25:34 +02:00
|
|
|
player.sendMessage(Config.getLocal(Language.NO_PERMISSION));
|
2011-07-23 21:00:47 +02:00
|
|
|
return;
|
2011-06-23 23:25:34 +02:00
|
|
|
}
|
2011-08-13 12:08:34 +02:00
|
|
|
|
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-07-23 21:00:47 +02:00
|
|
|
if (accountExists && !Economy.hasEnough(account, sellPrice)) {
|
2012-04-19 15:49:48 +02:00
|
|
|
int items = calculateItemAmount(Economy.balance(account), sellPrice);
|
2012-02-16 19:09:37 +01:00
|
|
|
if (!Config.getBoolean(Property.ALLOW_PARTIAL_TRANSACTIONS) || items < 1) {
|
|
|
|
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_MONEY_SHOP));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
sellPrice = (sellPrice / stockAmount) * items;
|
|
|
|
stockAmount = items;
|
|
|
|
}
|
2011-06-09 22:54:01 +02:00
|
|
|
}
|
2012-02-16 19:09:37 +01:00
|
|
|
if (uInventory.amount(player.getInventory(), stock, durability) < stockAmount) {
|
|
|
|
int items = uInventory.amount(player.getInventory(), stock, durability);
|
|
|
|
if (!Config.getBoolean(Property.ALLOW_PARTIAL_TRANSACTIONS) || items < 1) {
|
|
|
|
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_ITEMS_TO_SELL));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
sellPrice = (sellPrice / stockAmount) * items;
|
|
|
|
stockAmount = items;
|
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-02-16 19:09:37 +01:00
|
|
|
if (!isAdminShop() && !stockFitsChest(chest)) {
|
|
|
|
player.sendMessage(Config.getLocal(Language.NOT_ENOUGH_SPACE_IN_CHEST));
|
2011-07-23 21:00:47 +02:00
|
|
|
return;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-01-25 16:32:34 +01:00
|
|
|
if (accountExists) Economy.subtract(account, sellPrice);
|
2011-07-23 21:00:47 +02:00
|
|
|
if (!isAdminShop()) chest.addItem(stock, stockAmount);
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2012-01-25 16:32:34 +01:00
|
|
|
if (!isAdminShop()) Economy.add(player.getName(), sellPrice);
|
|
|
|
else Economy.addServer(player.getName(), sellPrice);
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2012-04-19 16:13:42 +02:00
|
|
|
String materialName = uSign.capitalizeFirst(stock.getType().name());
|
2011-05-21 19:55:55 +02:00
|
|
|
String formatedBalance = Economy.formatBalance(sellPrice);
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
if (Config.getBoolean(Property.SHOW_TRANSACTION_INFORMATION_CLIENT)) {
|
|
|
|
player.sendMessage(Config.getLocal(Language.YOU_SOLD_TO_SHOP)
|
|
|
|
.replace("%amount", String.valueOf(stockAmount))
|
|
|
|
.replace("%item", materialName)
|
|
|
|
.replace("%buyer", owner)
|
|
|
|
.replace("%price", formatedBalance));
|
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-07-05 19:08:55 +02:00
|
|
|
uInventory.remove(player.getInventory(), stock, stockAmount, durability);
|
2012-04-19 15:49:48 +02:00
|
|
|
Logging.logTransaction(false, this, sellPrice, player);
|
2011-06-09 22:54:01 +02:00
|
|
|
player.updateInventory();
|
|
|
|
|
2011-08-13 12:08:34 +02:00
|
|
|
if (Config.getBoolean(Property.SHOW_TRANSACTION_INFORMATION_OWNER)) {
|
|
|
|
sendMessageToOwner(Config.getLocal(Language.SOMEBODY_SOLD_TO_YOUR_SHOP)
|
|
|
|
.replace("%amount", String.valueOf(stockAmount))
|
|
|
|
.replace("%item", materialName)
|
|
|
|
.replace("%seller", player.getName())
|
|
|
|
.replace("%price", formatedBalance));
|
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
private String getOwnerAccount() {
|
2011-07-23 21:00:47 +02:00
|
|
|
return uSign.isAdminShop(owner) ? Config.getString(Property.SERVER_ECONOMY_ACCOUNT) : owner;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
private boolean isAdminShop() {
|
2011-07-05 19:08:55 +02:00
|
|
|
return uSign.isAdminShop(owner);
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
|
|
|
|
private int stockAmount(ItemStack item, short durability) {
|
2012-02-16 19:09:37 +01:00
|
|
|
return chest.amount(item, durability);
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
private boolean stockFitsPlayer(Player player) {
|
2011-07-05 19:08:55 +02:00
|
|
|
return uInventory.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
|
|
|
}
|
|
|
|
|
2012-04-19 15:49:48 +02:00
|
|
|
private int calculateItemAmount(double money, double basePrice) {
|
|
|
|
return (int) Math.floor(money / (basePrice / stockAmount));
|
2012-02-16 19:09:37 +01: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
|
|
|
}
|
|
|
|
}
|