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-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-05-10 16:32:25 +02:00
|
|
|
import com.Acrobot.ChestShop.Containers.Container;
|
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;
|
2012-05-10 16:32:25 +02:00
|
|
|
import org.bukkit.Material;
|
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;
|
2012-04-19 17:12:49 +02:00
|
|
|
private final Container 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 17:12:49 +02:00
|
|
|
public Shop(Container 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
|
|
|
}
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
public void buyItemFrom(Player player) {
|
2012-04-19 15:49:48 +02:00
|
|
|
double buyPrice = uSign.buyPrice(sign.getLine(2));
|
2012-05-10 16:32:25 +02:00
|
|
|
|
|
|
|
if (chest == null) {
|
|
|
|
sendMessage(player, Language.NO_CHEST_DETECTED);
|
2011-07-23 21:00:47 +02:00
|
|
|
return;
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
if (Double.compare(buyPrice, 0.01D) < 0) {
|
|
|
|
sendMessage(player, Language.NO_BUYING_HERE);
|
2011-07-23 21:00:47 +02:00
|
|
|
return;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
2012-05-10 16:32:25 +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()))) {
|
2012-05-10 16:32:25 +02:00
|
|
|
sendMessage(player, 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();
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
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) {
|
2012-05-10 16:32:25 +02:00
|
|
|
sendMessage(player, Language.NOT_ENOUGH_MONEY);
|
2012-02-16 19:09:37 +01:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
buyPrice = (buyPrice / stockAmount) * items;
|
|
|
|
stockAmount = items;
|
|
|
|
}
|
2011-06-09 22:54:01 +02:00
|
|
|
}
|
|
|
|
if (!stockFitsPlayer(player)) {
|
2012-05-10 16:32:25 +02:00
|
|
|
sendMessage(player, Language.NOT_ENOUGH_SPACE_IN_INVENTORY);
|
2011-07-23 21:00:47 +02:00
|
|
|
return;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
String materialName = uSign.capitalizeFirstLetter(stock.getType().name());
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
if (!hasEnoughStock()) {
|
2012-02-16 19:09:37 +01:00
|
|
|
int items = stockAmount(stock, durability);
|
|
|
|
if (!Config.getBoolean(Property.ALLOW_PARTIAL_TRANSACTIONS) || items < 1) {
|
2012-05-10 16:32:25 +02:00
|
|
|
sendMessage(player, Language.NOT_ENOUGH_STOCK);
|
|
|
|
|
|
|
|
if (!Config.getBoolean(Property.SHOW_MESSAGE_OUT_OF_STOCK)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-02-16 19:09:37 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
Economy.add(getOwnerAccount(), buyPrice);
|
2012-01-25 16:32:34 +01:00
|
|
|
Economy.subtract(playerName, buyPrice);
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
chest.removeItem(stock, durability, stockAmount);
|
|
|
|
|
2011-07-23 21:00:47 +02:00
|
|
|
|
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)) {
|
2012-05-10 16:32:25 +02:00
|
|
|
String message = formatMessage(Language.YOU_BOUGHT_FROM_SHOP, materialName, formatedPrice);
|
|
|
|
message = message.replace("%owner", owner);
|
|
|
|
|
|
|
|
player.sendMessage(message);
|
2011-08-13 12:08:34 +02:00
|
|
|
}
|
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)) {
|
2012-05-10 16:32:25 +02:00
|
|
|
String message = formatMessage(Language.SOMEBODY_BOUGHT_FROM_YOUR_SHOP, materialName, formatedPrice);
|
|
|
|
message = message.replace("%buyer", player.getName());
|
|
|
|
|
|
|
|
sendMessageToOwner(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shopShouldBeRemoved()) {
|
|
|
|
removeShop();
|
2011-08-13 12:08:34 +02:00
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
public void sellItemTo(Player player) {
|
2012-04-19 15:49:48 +02:00
|
|
|
double sellPrice = uSign.sellPrice(sign.getLine(2));
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
if (chest == null) {
|
|
|
|
sendMessage(player, Language.NO_CHEST_DETECTED);
|
2011-07-23 21:00:47 +02:00
|
|
|
return;
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|
2012-05-10 16:32:25 +02:00
|
|
|
if (Double.compare(sellPrice, 0.01D) < 0) {
|
|
|
|
sendMessage(player, 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()))) {
|
2012-05-10 16:32:25 +02:00
|
|
|
sendMessage(player, 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();
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
if (!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) {
|
2012-05-10 16:32:25 +02:00
|
|
|
sendMessage(player, Language.NOT_ENOUGH_MONEY_SHOP);
|
2012-02-16 19:09:37 +01:00
|
|
|
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) {
|
2012-05-10 16:32:25 +02:00
|
|
|
sendMessage(player, Language.NOT_ENOUGH_ITEMS_TO_SELL);
|
2012-02-16 19:09:37 +01:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
sellPrice = (sellPrice / stockAmount) * items;
|
|
|
|
stockAmount = items;
|
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
if (!stockFitsChest(chest)) {
|
|
|
|
sendMessage(player, Language.NOT_ENOUGH_SPACE_IN_CHEST);
|
2011-07-23 21:00:47 +02:00
|
|
|
return;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
Economy.subtract(account, sellPrice);
|
|
|
|
Economy.add(player.getName(), sellPrice);
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
chest.addItem(stock, stockAmount);
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
String materialName = uSign.capitalizeFirstLetter(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)) {
|
2012-05-10 16:32:25 +02:00
|
|
|
String message = formatMessage(Language.YOU_SOLD_TO_SHOP, materialName, formatedBalance);
|
|
|
|
message = message.replace("%buyer", owner);
|
|
|
|
|
|
|
|
player.sendMessage(message);
|
2011-08-13 12:08:34 +02:00
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
|
2011-07-05 19:08:55 +02:00
|
|
|
uInventory.remove(player.getInventory(), stock, stockAmount, durability);
|
2012-05-10 16:32:25 +02:00
|
|
|
|
2012-04-19 15:49:48 +02:00
|
|
|
Logging.logTransaction(false, this, sellPrice, player);
|
2012-05-10 16:32:25 +02:00
|
|
|
|
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)) {
|
2012-05-10 16:32:25 +02:00
|
|
|
String message = formatMessage(Language.SOMEBODY_SOLD_TO_YOUR_SHOP, materialName, formatedBalance);
|
|
|
|
message = message.replace("%seller", player.getName());
|
|
|
|
|
|
|
|
sendMessageToOwner(message);
|
2011-08-13 12:08:34 +02:00
|
|
|
}
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
private boolean shopShouldBeRemoved() {
|
|
|
|
return Config.getBoolean(Property.REMOVE_EMPTY_SHOPS) && shopIsEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean shopIsEmpty() {
|
|
|
|
return chest.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void removeShop() {
|
|
|
|
sign.getBlock().setType(Material.AIR);
|
|
|
|
|
|
|
|
chest.addItem(new ItemStack(Material.SIGN, 1), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String formatMessage(Language message, String materialName, String price) {
|
|
|
|
return Config.getLocal(message)
|
|
|
|
.replace("%amount", String.valueOf(stockAmount))
|
|
|
|
.replace("%item", materialName)
|
|
|
|
.replace("%price", price);
|
|
|
|
}
|
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
private String getOwnerAccount() {
|
2012-05-10 16:32:25 +02:00
|
|
|
return isAdminShop() ? 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
|
|
|
}
|
|
|
|
|
2012-04-19 17:12:49 +02:00
|
|
|
private boolean stockFitsChest(Container 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
|
|
|
}
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
private static void sendMessage(Player player, Language message) {
|
|
|
|
player.sendMessage(Config.getLocal(message));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|