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-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-05-21 19:55:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.Config;
|
|
|
|
import com.Acrobot.ChestShop.Utils.InventoryUtil;
|
|
|
|
import com.Acrobot.ChestShop.Utils.SignUtil;
|
2011-05-29 13:25:25 +02:00
|
|
|
import net.minecraft.server.EntityPlayer;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.block.Sign;
|
2011-05-29 13:25:25 +02:00
|
|
|
import org.bukkit.craftbukkit.entity.CraftPlayer;
|
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;
|
|
|
|
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];
|
|
|
|
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) {
|
|
|
|
if(chest == null && !isAdminShop()){
|
|
|
|
player.sendMessage(Config.getLocal("NO_CHEST_DETECTED"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (buyPrice == -1) {
|
2011-05-21 19:55:55 +02:00
|
|
|
player.sendMessage(Config.getLocal("NO_BUYING_HERE"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
if (!fits(stock, player)) {
|
2011-05-21 19:55:55 +02:00
|
|
|
player.sendMessage(Config.getLocal("NOT_ENOUGH_SPACE_IN_INVENTORY"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String materialName = stock.getType().name();
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
if (!isAdminShop() && !hasEnoughStock()) {
|
2011-05-21 19:55:55 +02:00
|
|
|
player.sendMessage(Config.getLocal("NOT_ENOUGH_STOCK"));
|
|
|
|
sendMessageToOwner(Config.getLocal("NOT_ENOUGH_STOCK_IN_YOUR_SHOP").replace("%material", materialName));
|
|
|
|
return false;
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
if (!getOwner().isEmpty() && Economy.hasAccount(getOwner())) {
|
2011-05-21 19:55:55 +02:00
|
|
|
Economy.add(getOwner(), buyPrice);
|
|
|
|
}
|
|
|
|
Economy.substract(player.getName(), buyPrice);
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
if (!isAdminShop()) {
|
2011-05-21 19:55:55 +02:00
|
|
|
chest.removeItem(stock, stock.getDurability(), stockAmount);
|
|
|
|
}
|
|
|
|
|
|
|
|
String formatedPrice = Economy.formatBalance(buyPrice);
|
|
|
|
player.sendMessage(Config.getLocal("YOU_BOUGHT_FROM_SHOP")
|
|
|
|
.replace("%amount", String.valueOf(stockAmount))
|
|
|
|
.replace("%item", materialName)
|
|
|
|
.replace("%owner", owner)
|
|
|
|
.replace("%price", formatedPrice));
|
|
|
|
|
|
|
|
sendMessageToOwner(Config.getLocal("SOMEBODY_BOUGHT_FROM_YOUR_SHOP")
|
|
|
|
.replace("%amount", String.valueOf(stockAmount))
|
|
|
|
.replace("%item", materialName)
|
|
|
|
.replace("%buyer", player.getName())
|
|
|
|
.replace("%price", formatedPrice));
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
InventoryUtil.add(player.getInventory(), stock, stockAmount);
|
|
|
|
|
|
|
|
Logging.logTransaction(true, this, player);
|
|
|
|
|
|
|
|
updateInventory(player);
|
2011-05-21 19:55:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public boolean sell(Player player) {
|
|
|
|
if(chest == null && !isAdminShop()){
|
|
|
|
player.sendMessage(Config.getLocal("NO_CHEST_DETECTED"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (sellPrice == -1) {
|
2011-05-21 19:55:55 +02:00
|
|
|
player.sendMessage(Config.getLocal("NO_SELLING_HERE"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
if (!isAdminShop() && !fits(stock, chest)) {
|
2011-05-21 19:55:55 +02:00
|
|
|
player.sendMessage(Config.getLocal("NOT_ENOUGH_SPACE_IN_CHEST"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
if (InventoryUtil.amount(player.getInventory(), stock, stock.getDurability()) < stockAmount) {
|
2011-05-21 19:55:55 +02:00
|
|
|
player.sendMessage(Config.getLocal("NOT_ENOUGH_ITEMS_TO_SELL"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
if (!getOwner().isEmpty() && Economy.hasAccount(getOwner())) {
|
2011-05-21 19:55:55 +02:00
|
|
|
Economy.substract(getOwner(), sellPrice);
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
if (!isAdminShop()) {
|
2011-05-21 19:55:55 +02:00
|
|
|
chest.addItem(stock, stock.getDurability(), stockAmount);
|
|
|
|
}
|
|
|
|
|
|
|
|
Economy.add(player.getName(), sellPrice);
|
|
|
|
|
|
|
|
String materialName = stock.getType().name();
|
|
|
|
String formatedBalance = Economy.formatBalance(sellPrice);
|
|
|
|
|
|
|
|
player.sendMessage(Config.getLocal("YOU_SOLD_TO_SHOP")
|
|
|
|
.replace("%amount", String.valueOf(stockAmount))
|
|
|
|
.replace("%item", materialName)
|
|
|
|
.replace("%buyer", owner)
|
|
|
|
.replace("%price", formatedBalance));
|
|
|
|
|
|
|
|
sendMessageToOwner(Config.getLocal("SOMEBODY_SOLD_TO_YOUR_SHOP")
|
|
|
|
.replace("%amount", String.valueOf(stockAmount))
|
|
|
|
.replace("%item", materialName)
|
|
|
|
.replace("%seller", player.getName())
|
|
|
|
.replace("%price", formatedBalance));
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
InventoryUtil.remove(player.getInventory(), stock, stockAmount, stock.getDurability());
|
|
|
|
|
|
|
|
Logging.logTransaction(false, this, player);
|
|
|
|
|
|
|
|
updateInventory(player);
|
2011-05-21 19:55:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
private String getOwner() {
|
|
|
|
if (SignUtil.isAdminShop(owner)) {
|
2011-05-21 19:55:55 +02:00
|
|
|
return Config.getString("shopEconomyAccount");
|
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-05-21 19:55:55 +02:00
|
|
|
return chest.hasEnough(stock, stockAmount, stock.getDurability());
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
private void updateInventory(Player player){
|
|
|
|
EntityPlayer p = ((CraftPlayer) player).getHandle();
|
|
|
|
p.a(p.activeContainer);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean fits(ItemStack item, Player player) {
|
2011-05-21 19:55:55 +02:00
|
|
|
return InventoryUtil.fits(player.getInventory(), item, item.getAmount(), item.getDurability()) <= 0;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
private static boolean fits(ItemStack item, ChestObject chest) {
|
2011-05-21 19:55:55 +02:00
|
|
|
return chest.fits(item, item.getAmount(), item.getDurability());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|