2011-05-15 19:33:03 +02:00
|
|
|
package com.Acrobot.ChestShop.Shop;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.Breeze.Utils.InventoryUtil;
|
|
|
|
import com.Acrobot.Breeze.Utils.MaterialUtil;
|
|
|
|
import com.Acrobot.Breeze.Utils.PriceUtil;
|
|
|
|
import com.Acrobot.Breeze.Utils.StringUtil;
|
2011-05-21 19:55:55 +02:00
|
|
|
import com.Acrobot.ChestShop.ChestShop;
|
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-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.ChestShop.Containers.AdminChest;
|
2012-05-10 16:32:25 +02:00
|
|
|
import com.Acrobot.ChestShop.Containers.Container;
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.ChestShop.Containers.ShopChest;
|
2012-01-25 16:32:34 +01:00
|
|
|
import com.Acrobot.ChestShop.Economy.Economy;
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.ChestShop.Events.TransactionEvent;
|
2011-06-23 23:25:34 +02:00
|
|
|
import com.Acrobot.ChestShop.Permission;
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
2012-05-10 16:32:25 +02:00
|
|
|
import org.bukkit.Material;
|
2012-06-08 15:28:36 +02:00
|
|
|
import org.bukkit.block.Chest;
|
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;
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
import static com.Acrobot.Breeze.Utils.PriceUtil.*;
|
|
|
|
import static com.Acrobot.ChestShop.Config.Language.*;
|
|
|
|
import static com.Acrobot.ChestShop.Config.Property.ALLOW_PARTIAL_TRANSACTIONS;
|
|
|
|
import static com.Acrobot.ChestShop.Config.Property.SHOW_MESSAGE_OUT_OF_STOCK;
|
|
|
|
import static com.Acrobot.ChestShop.Events.TransactionEvent.Type.BUY;
|
|
|
|
import static com.Acrobot.ChestShop.Events.TransactionEvent.Type.SELL;
|
|
|
|
import static com.Acrobot.ChestShop.Signs.ChestShopSign.*;
|
|
|
|
|
2011-05-15 18:16:25 +02:00
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class Shop {
|
2012-06-08 15:28:36 +02:00
|
|
|
private final Container container;
|
|
|
|
private final String owner;
|
|
|
|
|
|
|
|
private ItemStack stock;
|
|
|
|
|
|
|
|
private final Sign sign;
|
|
|
|
|
|
|
|
public Shop(Container container, ItemStack stock, Sign sign) {
|
|
|
|
this.container = container;
|
2012-06-25 17:16:41 +02:00
|
|
|
this.sign = sign;
|
2012-06-08 15:28:36 +02:00
|
|
|
|
2012-06-25 17:16:41 +02:00
|
|
|
this.owner = sign.getLine(NAME_LINE);
|
2012-06-08 15:28:36 +02:00
|
|
|
this.stock = stock;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
public void buyFromPlayer(Player player) {
|
|
|
|
Language message = sell(player);
|
|
|
|
|
|
|
|
sendMessage(player, message);
|
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
public void sellToPlayer(Player player) {
|
|
|
|
Language message = buy(player);
|
|
|
|
|
|
|
|
sendMessage(player, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Language buy(Player player) {
|
|
|
|
double price = getBuyPrice(sign.getLine(PRICE_LINE));
|
|
|
|
|
|
|
|
if (price == NO_PRICE) {
|
|
|
|
return NO_BUYING_HERE;
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
2012-06-08 15:28:36 +02:00
|
|
|
|
|
|
|
if (container == null) {
|
|
|
|
return NO_CHEST_DETECTED;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
if (!hasPermission(player, stock.getType(), true)) {
|
|
|
|
return NO_PERMISSION;
|
2011-06-23 23:25:34 +02:00
|
|
|
}
|
2012-06-08 15:28:36 +02:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
String playerName = player.getName();
|
2012-06-08 15:28:36 +02:00
|
|
|
String itemName = StringUtil.capitalizeFirstLetter(stock.getType().name());
|
|
|
|
double balance = Economy.balance(playerName);
|
|
|
|
|
|
|
|
if (!Economy.hasEnough(playerName, price)) {
|
|
|
|
int possiblePartialItemCount = calculateItemAmount(balance, price);
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
if (!partialTransactionAllowed(possiblePartialItemCount)) {
|
|
|
|
return NOT_ENOUGH_MONEY;
|
2012-02-16 19:09:37 +01:00
|
|
|
} else {
|
2012-06-10 23:32:57 +02:00
|
|
|
price = (price / stock.getAmount()) * possiblePartialItemCount;
|
|
|
|
stock.setAmount(possiblePartialItemCount);
|
2012-02-16 19:09:37 +01:00
|
|
|
}
|
2011-06-09 22:54:01 +02:00
|
|
|
}
|
2012-06-08 15:28:36 +02:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
if (!stockFitsPlayer(player)) {
|
2012-06-08 15:28:36 +02:00
|
|
|
return NOT_ENOUGH_SPACE_IN_INVENTORY;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
if (!shopHasEnoughItems()) {
|
|
|
|
int possiblePartialItemCount = getStockAmount(stock);
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
if (!partialTransactionAllowed(possiblePartialItemCount)) {
|
|
|
|
if (Config.getBoolean(SHOW_MESSAGE_OUT_OF_STOCK)) {
|
|
|
|
sendMessageToOwner(Config.getLocal(NOT_ENOUGH_STOCK_IN_YOUR_SHOP).replace("%material", itemName));
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|
2012-02-16 19:09:37 +01:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
return NOT_ENOUGH_STOCK;
|
2012-02-16 19:09:37 +01:00
|
|
|
} else {
|
2012-06-10 23:32:57 +02:00
|
|
|
price = (price / stock.getAmount()) * possiblePartialItemCount;
|
|
|
|
stock.setAmount(possiblePartialItemCount);
|
2012-02-16 19:09:37 +01:00
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
Economy.add(getOwnerAccount(), price);
|
|
|
|
Economy.subtract(playerName, price);
|
2011-07-23 21:00:47 +02:00
|
|
|
|
2012-06-25 17:16:41 +02:00
|
|
|
container.removeItem(stock.clone()); //Bad bukkit! You shouldn't change ItemStacks during the process!
|
|
|
|
InventoryUtil.add(stock.clone(), player.getInventory());
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
player.updateInventory();
|
|
|
|
|
2012-06-10 23:32:57 +02:00
|
|
|
TransactionEvent event = new TransactionEvent(BUY, container, sign, player, this.owner, stock, price);
|
2012-06-08 15:28:36 +02:00
|
|
|
ChestShop.callEvent(event);
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
return null;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
private Language sell(Player player) {
|
|
|
|
double price = getSellPrice(sign.getLine(PRICE_LINE));
|
2012-04-19 15:49:48 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
if (container == null) {
|
|
|
|
return NO_CHEST_DETECTED;
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
2012-06-25 17:16:41 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
if (price == PriceUtil.NO_PRICE) {
|
|
|
|
return NO_SELLING_HERE;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
2012-06-25 17:16:41 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
if (!hasPermission(player, stock.getType(), false)) {
|
|
|
|
return NO_PERMISSION;
|
2011-06-23 23:25:34 +02:00
|
|
|
}
|
2011-08-13 12:08:34 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
String ownerAccount = getOwnerAccount();
|
|
|
|
|
|
|
|
if (!Economy.hasEnough(ownerAccount, price)) {
|
|
|
|
int possiblePartialItemCount = calculateItemAmount(Economy.balance(ownerAccount), price);
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
if (!partialTransactionAllowed(possiblePartialItemCount)) {
|
|
|
|
return NOT_ENOUGH_MONEY_SHOP;
|
2012-02-16 19:09:37 +01:00
|
|
|
} else {
|
2012-06-10 23:32:57 +02:00
|
|
|
price = (price / stock.getAmount()) * possiblePartialItemCount;
|
|
|
|
stock.setAmount(possiblePartialItemCount);
|
2012-02-16 19:09:37 +01:00
|
|
|
}
|
2011-06-09 22:54:01 +02:00
|
|
|
}
|
2012-06-08 15:28:36 +02:00
|
|
|
|
|
|
|
if (!playerHasEnoughItems(player)) {
|
|
|
|
int possiblePartialItemCount = InventoryUtil.getAmount(stock, player.getInventory());
|
|
|
|
|
|
|
|
if (!partialTransactionAllowed(possiblePartialItemCount)) {
|
|
|
|
return NOT_ENOUGH_ITEMS_TO_SELL;
|
2012-02-16 19:09:37 +01:00
|
|
|
} else {
|
2012-06-10 23:32:57 +02:00
|
|
|
price = (price / stock.getAmount()) * possiblePartialItemCount;
|
|
|
|
stock.setAmount(possiblePartialItemCount);
|
2012-02-16 19:09:37 +01:00
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
if (!stockFitsChest()) {
|
|
|
|
return NOT_ENOUGH_SPACE_IN_CHEST;
|
2011-08-13 12:08:34 +02:00
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
Economy.subtract(ownerAccount, price);
|
|
|
|
Economy.add(player.getName(), price);
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2012-06-25 17:16:41 +02:00
|
|
|
container.addItem(stock.clone());
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2012-06-25 17:16:41 +02:00
|
|
|
InventoryUtil.remove(stock.clone(), player.getInventory());
|
2011-06-09 22:54:01 +02:00
|
|
|
player.updateInventory();
|
|
|
|
|
2012-06-10 23:32:57 +02:00
|
|
|
TransactionEvent event = new TransactionEvent(SELL, container, sign, player, this.owner, stock, price);
|
2012-06-08 15:28:36 +02:00
|
|
|
ChestShop.callEvent(event);
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
return null;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
private String getOwnerAccount() {
|
|
|
|
return isAdminShop() ? Config.getString(Property.SERVER_ECONOMY_ACCOUNT) : owner;
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
private boolean isAdminShop() {
|
|
|
|
return ChestShopSign.isAdminShop(owner);
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
private boolean stockFitsPlayer(Player player) {
|
|
|
|
return InventoryUtil.fits(stock, player.getInventory());
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
private boolean stockFitsChest() {
|
|
|
|
return container.fits(stock);
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
private int getStockAmount(ItemStack item) {
|
|
|
|
return container.amount(item);
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
private boolean shopHasEnoughItems() {
|
|
|
|
return container.hasEnough(stock);
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
private boolean playerHasEnoughItems(Player player) {
|
2012-06-10 23:32:57 +02:00
|
|
|
return InventoryUtil.getAmount(stock, player.getInventory()) >= stock.getAmount();
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
private int calculateItemAmount(double money, double basePrice) {
|
2012-06-10 23:32:57 +02:00
|
|
|
return (int) Math.floor(money / (basePrice / stock.getAmount()));
|
2012-02-16 19:09:37 +01:00
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
private static void sendMessage(Player player, Language message) {
|
|
|
|
if (message != null) {
|
|
|
|
player.sendMessage(Config.getLocal(message));
|
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
private void sendMessageToOwner(String msg) {
|
|
|
|
Player player = ChestShop.getBukkitServer().getPlayer(owner);
|
|
|
|
|
|
|
|
if (player != null) {
|
|
|
|
player.sendMessage(msg);
|
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
public static Shop getShopFromSign(Sign sign) {
|
|
|
|
Chest chestMc = uBlock.findConnectedChest(sign);
|
|
|
|
ItemStack item = MaterialUtil.getItem(sign.getLine(ITEM_LINE));
|
|
|
|
|
|
|
|
if (item == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
int itemAmount = Integer.parseInt(sign.getLine(QUANTITY_LINE));
|
|
|
|
item.setAmount(itemAmount);
|
|
|
|
|
|
|
|
if (ChestShopSign.isAdminShop(sign)) {
|
|
|
|
return new Shop(new AdminChest(), item, sign);
|
|
|
|
} else {
|
|
|
|
return new Shop(chestMc != null ? new ShopChest(chestMc) : null, item, sign);
|
|
|
|
}
|
2012-02-16 19:09:37 +01:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
private static boolean partialTransactionAllowed(int itemCount) {
|
|
|
|
return Config.getBoolean(ALLOW_PARTIAL_TRANSACTIONS) && itemCount > 0;
|
2012-05-10 16:32:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
private static boolean hasPermission(Player player, Material material, boolean buying) {
|
|
|
|
if (buying) {
|
|
|
|
return Permission.has(player, Permission.BUY) || Permission.has(player, Permission.BUY_ID + Integer.toString(material.getId()));
|
|
|
|
} else {
|
|
|
|
return Permission.has(player, Permission.SELL) || Permission.has(player, Permission.SELL_ID + Integer.toString(material.getId()));
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
}
|