2016-06-28 18:30:48 +02:00
|
|
|
package de.epiceric.shopchest.listeners;
|
2015-09-02 13:06:48 +02:00
|
|
|
|
2016-05-28 15:47:57 +02:00
|
|
|
import de.epiceric.shopchest.ShopChest;
|
2016-06-22 20:51:19 +02:00
|
|
|
import de.epiceric.shopchest.config.Regex;
|
2016-07-05 18:41:31 +02:00
|
|
|
import de.epiceric.shopchest.event.ShopBuySellEvent;
|
|
|
|
import de.epiceric.shopchest.event.ShopCreateEvent;
|
|
|
|
import de.epiceric.shopchest.event.ShopInfoEvent;
|
|
|
|
import de.epiceric.shopchest.event.ShopRemoveEvent;
|
2016-06-22 20:51:19 +02:00
|
|
|
import de.epiceric.shopchest.language.LanguageUtils;
|
|
|
|
import de.epiceric.shopchest.language.LocalizedMessage;
|
2016-05-28 15:47:57 +02:00
|
|
|
import de.epiceric.shopchest.shop.Shop;
|
|
|
|
import de.epiceric.shopchest.shop.Shop.ShopType;
|
2016-05-29 15:32:54 +02:00
|
|
|
import de.epiceric.shopchest.sql.Database;
|
2016-06-22 20:51:19 +02:00
|
|
|
import de.epiceric.shopchest.utils.ClickType;
|
|
|
|
import de.epiceric.shopchest.utils.ShopUtils;
|
|
|
|
import de.epiceric.shopchest.utils.Utils;
|
2016-05-28 15:47:57 +02:00
|
|
|
import net.milkbowl.vault.economy.Economy;
|
|
|
|
import net.milkbowl.vault.economy.EconomyResponse;
|
|
|
|
import net.milkbowl.vault.permission.Permission;
|
2015-09-02 13:06:48 +02:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.block.Block;
|
2016-07-01 16:13:44 +02:00
|
|
|
import org.bukkit.block.BlockFace;
|
2015-09-02 13:06:48 +02:00
|
|
|
import org.bukkit.block.Chest;
|
|
|
|
import org.bukkit.enchantments.Enchantment;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
2016-08-04 21:12:19 +02:00
|
|
|
import org.bukkit.event.EventPriority;
|
2015-09-02 13:06:48 +02:00
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
import org.bukkit.event.block.Action;
|
|
|
|
import org.bukkit.event.player.PlayerInteractEvent;
|
|
|
|
import org.bukkit.event.player.PlayerMoveEvent;
|
|
|
|
import org.bukkit.inventory.Inventory;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
2016-05-31 18:07:38 +02:00
|
|
|
import org.bukkit.inventory.PlayerInventory;
|
2015-09-13 13:13:04 +02:00
|
|
|
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
|
2016-08-05 22:16:14 +02:00
|
|
|
import org.bukkit.inventory.meta.PotionMeta;
|
|
|
|
import org.bukkit.potion.Potion;
|
2015-09-04 12:31:19 +02:00
|
|
|
|
2016-05-28 15:47:57 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2015-09-02 13:06:48 +02:00
|
|
|
|
2016-06-28 18:30:48 +02:00
|
|
|
public class ShopInteractListener implements Listener {
|
2016-05-28 15:47:57 +02:00
|
|
|
|
|
|
|
private ShopChest plugin;
|
2016-06-22 20:51:19 +02:00
|
|
|
private Permission perm;
|
|
|
|
private Economy econ;
|
|
|
|
private Database database;
|
2016-07-13 15:03:54 +02:00
|
|
|
private ShopUtils shopUtils;
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-06-28 18:30:48 +02:00
|
|
|
public ShopInteractListener(ShopChest plugin) {
|
2016-05-28 15:47:57 +02:00
|
|
|
this.plugin = plugin;
|
2016-06-22 20:51:19 +02:00
|
|
|
this.perm = plugin.getPermission();
|
|
|
|
this.econ = plugin.getEconomy();
|
|
|
|
this.database = plugin.getShopDatabase();
|
2016-07-13 15:03:54 +02:00
|
|
|
this.shopUtils = plugin.getShopUtils();
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
2016-08-04 21:12:19 +02:00
|
|
|
@EventHandler(priority = EventPriority.HIGH)
|
|
|
|
public void onPlayerInteractCreate(PlayerInteractEvent e) {
|
|
|
|
Player p = e.getPlayer();
|
|
|
|
Block b = e.getClickedBlock();
|
|
|
|
|
2016-08-06 13:32:37 +02:00
|
|
|
if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
|
|
|
|
if (b.getType().equals(Material.CHEST) || b.getType().equals(Material.TRAPPED_CHEST)) {
|
|
|
|
if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
|
|
|
|
if (ClickType.getPlayerClickType(p) != null) {
|
|
|
|
if (ClickType.getPlayerClickType(p).getClickType() == ClickType.EnumClickType.CREATE) {
|
|
|
|
if (!shopUtils.isShop(b.getLocation())) {
|
|
|
|
if (e.isCancelled() && !perm.has(p, "shopchest.create.protected")) {
|
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_CREATE_PROTECTED));
|
|
|
|
ClickType.removePlayerClickType(p);
|
|
|
|
plugin.debug(p.getName() + " is not allowed to create a shop on the selected chest");
|
|
|
|
return;
|
|
|
|
}
|
2016-08-04 21:12:19 +02:00
|
|
|
|
2016-08-06 13:32:37 +02:00
|
|
|
e.setCancelled(true);
|
2016-08-04 21:12:19 +02:00
|
|
|
|
2016-08-06 13:32:37 +02:00
|
|
|
if (b.getRelative(BlockFace.UP).getType() == Material.AIR) {
|
|
|
|
ClickType clickType = ClickType.getPlayerClickType(p);
|
|
|
|
ItemStack product = clickType.getProduct();
|
|
|
|
double buyPrice = clickType.getBuyPrice();
|
|
|
|
double sellPrice = clickType.getSellPrice();
|
|
|
|
ShopType shopType = clickType.getShopType();
|
2016-08-04 21:12:19 +02:00
|
|
|
|
2016-08-06 13:32:37 +02:00
|
|
|
create(p, b.getLocation(), product, buyPrice, sellPrice, shopType);
|
|
|
|
} else {
|
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_BLOCKED));
|
|
|
|
plugin.debug("Chest is blocked");
|
|
|
|
}
|
2016-08-04 21:12:19 +02:00
|
|
|
} else {
|
2016-08-06 13:32:37 +02:00
|
|
|
e.setCancelled(true);
|
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_ALREADY_SHOP));
|
|
|
|
plugin.debug("Chest is already a shop");
|
2016-08-04 21:12:19 +02:00
|
|
|
}
|
|
|
|
|
2016-08-06 13:32:37 +02:00
|
|
|
ClickType.removePlayerClickType(p);
|
|
|
|
}
|
2016-08-04 21:12:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 15:47:57 +02:00
|
|
|
@EventHandler
|
|
|
|
public void onPlayerInteract(PlayerInteractEvent e) {
|
|
|
|
Block b = e.getClickedBlock();
|
|
|
|
Player p = e.getPlayer();
|
|
|
|
|
|
|
|
if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
|
|
|
|
|
|
|
|
if (b.getType().equals(Material.CHEST) || b.getType().equals(Material.TRAPPED_CHEST)) {
|
|
|
|
|
|
|
|
if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
|
|
|
|
|
|
|
|
if (ClickType.getPlayerClickType(p) != null) {
|
|
|
|
|
|
|
|
switch (ClickType.getPlayerClickType(p).getClickType()) {
|
|
|
|
case INFO:
|
|
|
|
e.setCancelled(true);
|
|
|
|
|
2016-07-13 15:03:54 +02:00
|
|
|
if (shopUtils.isShop(b.getLocation())) {
|
|
|
|
Shop shop = shopUtils.getShop(b.getLocation());
|
2016-05-28 15:47:57 +02:00
|
|
|
info(p, shop);
|
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_NO_SHOP));
|
2016-08-05 12:46:41 +02:00
|
|
|
plugin.debug("Chest is not a shop");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ClickType.removePlayerClickType(p);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REMOVE:
|
|
|
|
e.setCancelled(true);
|
|
|
|
|
2016-07-13 15:03:54 +02:00
|
|
|
if (shopUtils.isShop(b.getLocation())) {
|
|
|
|
Shop shop = shopUtils.getShop(b.getLocation());
|
2016-05-28 15:47:57 +02:00
|
|
|
|
|
|
|
if (shop.getVendor().getUniqueId().equals(p.getUniqueId()) || perm.has(p, "shopchest.removeOther")) {
|
|
|
|
remove(p, shop);
|
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_REMOVE_OTHERS));
|
2016-08-05 12:46:41 +02:00
|
|
|
plugin.debug(p.getName() + " is not permitted to remove another player's shop");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_NO_SHOP));
|
2016-08-05 12:46:41 +02:00
|
|
|
plugin.debug("Chest is not a shop");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ClickType.removePlayerClickType(p);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2016-07-13 15:03:54 +02:00
|
|
|
if (shopUtils.isShop(b.getLocation())) {
|
2016-05-28 15:47:57 +02:00
|
|
|
e.setCancelled(true);
|
2016-07-13 15:03:54 +02:00
|
|
|
Shop shop = shopUtils.getShop(b.getLocation());
|
2016-05-28 15:47:57 +02:00
|
|
|
|
|
|
|
if (p.isSneaking()) {
|
|
|
|
if (!shop.getVendor().getUniqueId().equals(p.getUniqueId())) {
|
|
|
|
if (perm.has(p, "shopchest.openOther")) {
|
2016-06-22 20:51:19 +02:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OPENED_SHOP, new LocalizedMessage.ReplacedRegex(Regex.VENDOR, shop.getVendor().getName())));
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug(p.getName() + " is opening " + shop.getVendor().getName() + "'s shop (#" + shop.getID() + ")" );
|
2016-05-28 15:47:57 +02:00
|
|
|
e.setCancelled(false);
|
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_OPEN_OTHERS));
|
2016-08-05 12:46:41 +02:00
|
|
|
plugin.debug(p.getName() + " is not permitted to open another player's shop");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
e.setCancelled(false);
|
|
|
|
}
|
|
|
|
} else {
|
2016-06-08 16:06:36 +02:00
|
|
|
if (shop.getShopType() == ShopType.ADMIN || !shop.getVendor().getUniqueId().equals(p.getUniqueId())) {
|
2016-08-05 12:46:41 +02:00
|
|
|
plugin.debug(p.getName() + " wants to buy");
|
2016-06-08 16:06:36 +02:00
|
|
|
if (shop.getBuyPrice() > 0) {
|
|
|
|
if (perm.has(p, "shopchest.buy")) {
|
|
|
|
if (shop.getShopType() == ShopType.ADMIN) {
|
|
|
|
buy(p, shop);
|
|
|
|
} else {
|
2016-05-28 15:47:57 +02:00
|
|
|
Chest c = (Chest) b.getState();
|
2016-05-31 18:07:38 +02:00
|
|
|
if (Utils.getAmount(c.getInventory(), shop.getProduct()) >= shop.getProduct().getAmount()) {
|
2016-05-28 15:47:57 +02:00
|
|
|
buy(p, shop);
|
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OUT_OF_STOCK));
|
2016-08-05 12:46:41 +02:00
|
|
|
plugin.debug("Shop is out of stock");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-08 16:06:36 +02:00
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_BUY));
|
2016-08-05 12:46:41 +02:00
|
|
|
plugin.debug(p.getName() + " is not permitted to buy");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUYING_DISABLED));
|
2016-08-05 12:46:41 +02:00
|
|
|
plugin.debug("Buying is disabled");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-06-08 16:06:36 +02:00
|
|
|
e.setCancelled(false);
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} else if (e.getAction() == Action.LEFT_CLICK_BLOCK) {
|
|
|
|
|
2016-07-13 15:03:54 +02:00
|
|
|
if (shopUtils.isShop(b.getLocation())) {
|
2016-08-10 13:00:51 +02:00
|
|
|
if (p.isSneaking())
|
|
|
|
return;
|
|
|
|
|
2016-05-28 15:47:57 +02:00
|
|
|
e.setCancelled(true);
|
2016-07-13 15:03:54 +02:00
|
|
|
Shop shop = shopUtils.getShop(b.getLocation());
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-06-08 16:06:36 +02:00
|
|
|
if ((shop.getShopType() == ShopType.ADMIN) || (!shop.getVendor().getUniqueId().equals(p.getUniqueId()))) {
|
2016-08-05 12:46:41 +02:00
|
|
|
plugin.debug(p.getName() + " wants to sell");
|
2016-06-08 16:06:36 +02:00
|
|
|
if (shop.getSellPrice() > 0) {
|
|
|
|
if (perm.has(p, "shopchest.sell")) {
|
|
|
|
if (Utils.getAmount(p.getInventory(), shop.getProduct()) >= shop.getProduct().getAmount()) {
|
|
|
|
sell(p, shop);
|
2016-05-28 15:47:57 +02:00
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NOT_ENOUGH_ITEMS));
|
2016-08-05 12:46:41 +02:00
|
|
|
plugin.debug(p.getName() + " doesn't have enough items");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
2016-06-08 16:06:36 +02:00
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_SELL));
|
2016-08-05 12:46:41 +02:00
|
|
|
plugin.debug(p.getName() + " is not permitted to sell");
|
2016-06-08 16:06:36 +02:00
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SELLING_DISABLED));
|
2016-08-05 12:46:41 +02:00
|
|
|
plugin.debug("Selling is disabled");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-06-08 16:06:36 +02:00
|
|
|
e.setCancelled(false);
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (ClickType.getPlayerClickType(p) != null) ClickType.removePlayerClickType(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-30 21:59:06 +02:00
|
|
|
/**
|
|
|
|
* Create a new shop
|
|
|
|
*
|
|
|
|
* @param executor Player, who executed the command, will receive the message and become the vendor of the shop
|
|
|
|
* @param location Where the shop will be located
|
|
|
|
* @param product Product of the Shop
|
|
|
|
* @param buyPrice Buy price
|
|
|
|
* @param sellPrice Sell price
|
|
|
|
* @param shopType Type of the shop
|
|
|
|
*/
|
2016-05-28 15:47:57 +02:00
|
|
|
private void create(Player executor, Location location, ItemStack product, double buyPrice, double sellPrice, ShopType shopType) {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug(executor.getName() + " is creating new shop...");
|
2016-07-01 14:28:31 +02:00
|
|
|
|
2016-08-03 13:49:31 +02:00
|
|
|
int id = database.getNextFreeID();
|
2016-07-08 16:55:57 +02:00
|
|
|
double creationPrice = (shopType == ShopType.NORMAL) ? plugin.getShopChestConfig().shop_creation_price_normal : plugin.getShopChestConfig().shop_creation_price_admin;
|
2016-07-05 18:41:31 +02:00
|
|
|
|
2016-08-03 13:49:31 +02:00
|
|
|
ShopCreateEvent event = new ShopCreateEvent(executor, Shop.createImaginaryShop(executor, product, location, buyPrice, sellPrice,shopType), creationPrice);
|
2016-07-05 18:41:31 +02:00
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
|
2016-08-03 13:49:31 +02:00
|
|
|
if (event.isCancelled()) {
|
|
|
|
plugin.debug("Create event cancelled (#" + id + ")");
|
|
|
|
return;
|
|
|
|
}
|
2016-07-01 14:33:13 +02:00
|
|
|
EconomyResponse r = plugin.getEconomy().withdrawPlayer(executor, creationPrice);
|
|
|
|
if (!r.transactionSuccess()) {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Economy transaction failed: " + r.errorMessage);
|
2016-07-01 14:33:13 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED, new LocalizedMessage.ReplacedRegex(Regex.ERROR, r.errorMessage)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-01 14:28:31 +02:00
|
|
|
Shop shop = new Shop(id, plugin, executor, product, location, buyPrice, sellPrice, shopType);
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Shop created (#" + id + ")");
|
2016-07-13 15:03:54 +02:00
|
|
|
shopUtils.addShop(shop, true);
|
2016-06-22 20:51:19 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_CREATED));
|
2016-05-28 15:47:57 +02:00
|
|
|
|
|
|
|
for (Player p : Bukkit.getOnlinePlayers()) {
|
|
|
|
Bukkit.getPluginManager().callEvent(new PlayerMoveEvent(p, p.getLocation(), p.getLocation()));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-30 21:59:06 +02:00
|
|
|
/**
|
|
|
|
* Remove a shop
|
|
|
|
* @param executor Player, who executed the command and will receive the message
|
|
|
|
* @param shop Shop to be removed
|
|
|
|
*/
|
2016-05-28 15:47:57 +02:00
|
|
|
private void remove(Player executor, Shop shop) {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug(executor.getName() + " is removing shop (#" + shop.getID() + ")");
|
2016-07-05 18:41:31 +02:00
|
|
|
ShopRemoveEvent event = new ShopRemoveEvent(executor, shop);
|
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
2016-08-03 13:49:31 +02:00
|
|
|
if (event.isCancelled()) {
|
|
|
|
plugin.debug("Remove event cancelled (#" + shop.getID() + ")");
|
|
|
|
return;
|
|
|
|
}
|
2016-07-05 18:41:31 +02:00
|
|
|
|
2016-07-13 15:03:54 +02:00
|
|
|
shopUtils.removeShop(shop, true);
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Removed shop (#" + shop.getID() + ")");
|
2016-06-22 20:51:19 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_REMOVED));
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 21:59:06 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param executor Player, who executed the command and will retrieve the information
|
|
|
|
* @param shop Shop from which the information will be retrieved
|
|
|
|
*/
|
2016-05-28 15:47:57 +02:00
|
|
|
private void info(Player executor, Shop shop) {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug(executor.getName() + " is retrieving shop info (#" + shop.getID() + ")");
|
2016-07-05 18:41:31 +02:00
|
|
|
ShopInfoEvent event = new ShopInfoEvent(executor, shop);
|
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
2016-08-03 13:49:31 +02:00
|
|
|
if (event.isCancelled()) {
|
|
|
|
plugin.debug("Info event cancelled (#" + shop.getID() + ")");
|
|
|
|
return;
|
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
Chest c = (Chest) shop.getLocation().getBlock().getState();
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
int amount = Utils.getAmount(c.getInventory(), shop.getProduct());
|
2016-06-22 20:51:19 +02:00
|
|
|
Material type = shop.getProduct().getType();
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-06-22 20:51:19 +02:00
|
|
|
String vendor = LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_INFO_VENDOR, new LocalizedMessage.ReplacedRegex(Regex.VENDOR, shop.getVendor().getName()));
|
|
|
|
String product = LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_INFO_PRODUCT, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(shop.getProduct().getAmount())),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(shop.getProduct())));
|
2016-05-28 15:47:57 +02:00
|
|
|
String enchantmentString = "";
|
2016-06-22 20:51:19 +02:00
|
|
|
String potionEffectString = "";
|
|
|
|
String musicDiscName = LanguageUtils.getMusicDiscName(type);
|
|
|
|
String price = LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_INFO_PRICE, new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(shop.getBuyPrice())),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(shop.getSellPrice())));
|
|
|
|
String shopType = LanguageUtils.getMessage(shop.getShopType() == ShopType.NORMAL ? LocalizedMessage.Message.SHOP_INFO_NORMAL : LocalizedMessage.Message.SHOP_INFO_ADMIN);
|
|
|
|
String stock = LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_INFO_STOCK, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(amount)));
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-08-05 22:16:14 +02:00
|
|
|
boolean potionExtended = false;
|
|
|
|
|
2016-05-28 15:47:57 +02:00
|
|
|
Map<Enchantment, Integer> enchantmentMap;
|
|
|
|
|
2016-06-22 20:51:19 +02:00
|
|
|
if (Utils.getMajorVersion() >= 9) {
|
2016-07-30 17:40:19 +02:00
|
|
|
if (type == Material.TIPPED_ARROW || type == Material.LINGERING_POTION || type == Material.SPLASH_POTION) {
|
2016-06-22 20:51:19 +02:00
|
|
|
potionEffectString = LanguageUtils.getPotionEffectName(shop.getProduct());
|
2016-08-05 22:16:14 +02:00
|
|
|
PotionMeta potionMeta = (PotionMeta) shop.getProduct().getItemMeta();
|
|
|
|
potionExtended = potionMeta.getBasePotionData().isExtended();
|
|
|
|
|
2016-06-22 20:51:19 +02:00
|
|
|
if (potionEffectString == null)
|
|
|
|
potionEffectString = LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_INFO_NONE);
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-30 17:40:19 +02:00
|
|
|
if (type == Material.POTION) {
|
2016-06-22 20:51:19 +02:00
|
|
|
potionEffectString = LanguageUtils.getPotionEffectName(shop.getProduct());
|
2016-08-05 22:16:14 +02:00
|
|
|
if (Utils.getMajorVersion() < 9) {
|
|
|
|
Potion potion = Potion.fromItemStack(shop.getProduct());
|
|
|
|
potionExtended = potion.hasExtendedDuration();
|
|
|
|
} else {
|
|
|
|
PotionMeta potionMeta = (PotionMeta) shop.getProduct().getItemMeta();
|
|
|
|
potionExtended = potionMeta.getBasePotionData().isExtended();
|
|
|
|
}
|
|
|
|
|
2016-06-22 20:51:19 +02:00
|
|
|
if (potionEffectString == null)
|
|
|
|
potionEffectString = LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_INFO_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-28 15:47:57 +02:00
|
|
|
if (shop.getProduct().getItemMeta() instanceof EnchantmentStorageMeta) {
|
|
|
|
EnchantmentStorageMeta esm = (EnchantmentStorageMeta) shop.getProduct().getItemMeta();
|
|
|
|
enchantmentMap = esm.getStoredEnchants();
|
|
|
|
} else {
|
|
|
|
enchantmentMap = shop.getProduct().getEnchantments();
|
|
|
|
}
|
|
|
|
|
|
|
|
Enchantment[] enchantments = enchantmentMap.keySet().toArray(new Enchantment[enchantmentMap.size()]);
|
|
|
|
|
|
|
|
for (int i = 0; i < enchantments.length; i++) {
|
|
|
|
Enchantment enchantment = enchantments[i];
|
|
|
|
|
|
|
|
if (i == enchantments.length - 1) {
|
2016-06-22 20:51:19 +02:00
|
|
|
enchantmentString += LanguageUtils.getEnchantmentName(enchantment, enchantmentMap.get(enchantment));
|
2016-05-28 15:47:57 +02:00
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
enchantmentString += LanguageUtils.getEnchantmentName(enchantment, enchantmentMap.get(enchantment)) + ", ";
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
executor.sendMessage(" ");
|
2016-06-08 16:08:49 +02:00
|
|
|
if (shop.getShopType() != ShopType.ADMIN) executor.sendMessage(vendor);
|
2016-05-28 15:47:57 +02:00
|
|
|
executor.sendMessage(product);
|
2016-06-08 16:08:49 +02:00
|
|
|
if (shop.getShopType() != ShopType.ADMIN) executor.sendMessage(stock);
|
2016-06-22 20:51:19 +02:00
|
|
|
if (enchantmentString.length() > 0)
|
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_INFO_ENCHANTMENTS, new LocalizedMessage.ReplacedRegex(Regex.ENCHANTMENT, enchantmentString)));
|
|
|
|
if (potionEffectString.length() > 0)
|
2016-08-05 22:16:14 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_INFO_POTION_EFFECT, new LocalizedMessage.ReplacedRegex(Regex.POTION_EFFECT, potionEffectString),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.EXTENDED, (potionExtended ? LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_INFO_EXTENDED) : ""))));
|
2016-06-22 20:51:19 +02:00
|
|
|
if (musicDiscName.length() > 0)
|
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_INFO_MUSIC_TITLE, new LocalizedMessage.ReplacedRegex(Regex.MUSIC_TITLE, musicDiscName)));
|
2016-05-28 15:47:57 +02:00
|
|
|
executor.sendMessage(price);
|
|
|
|
executor.sendMessage(shopType);
|
|
|
|
executor.sendMessage(" ");
|
|
|
|
}
|
|
|
|
|
2016-06-30 21:59:06 +02:00
|
|
|
/**
|
|
|
|
* A player buys from a shop
|
|
|
|
* @param executor Player, who executed the command and will buy the product
|
|
|
|
* @param shop Shop, from which the player buys
|
|
|
|
*/
|
2016-05-28 15:47:57 +02:00
|
|
|
private void buy(Player executor, Shop shop) {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug(executor.getName() + " is buying (#" + shop.getID() + ")");
|
2016-05-28 15:47:57 +02:00
|
|
|
if (econ.getBalance(executor) >= shop.getBuyPrice()) {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug(executor.getName() + " has enough money (#" + shop.getID() + ")");
|
2016-05-28 15:47:57 +02:00
|
|
|
|
|
|
|
Block b = shop.getLocation().getBlock();
|
|
|
|
Chest c = (Chest) b.getState();
|
|
|
|
|
|
|
|
HashMap<Integer, Integer> slotFree = new HashMap<>();
|
2016-05-31 18:07:38 +02:00
|
|
|
ItemStack product = new ItemStack(shop.getProduct());
|
2016-05-28 15:47:57 +02:00
|
|
|
Inventory inventory = executor.getInventory();
|
|
|
|
|
|
|
|
for (int i = 0; i < 36; i++) {
|
|
|
|
ItemStack item = inventory.getItem(i);
|
|
|
|
if (item == null) {
|
|
|
|
slotFree.put(i, product.getMaxStackSize());
|
|
|
|
} else {
|
2016-05-31 18:07:38 +02:00
|
|
|
if (item.isSimilar(product)) {
|
2016-05-28 15:47:57 +02:00
|
|
|
int amountInSlot = item.getAmount();
|
|
|
|
int amountToFullStack = product.getMaxStackSize() - amountInSlot;
|
|
|
|
slotFree.put(i, amountToFullStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-22 20:51:19 +02:00
|
|
|
if (Utils.getMajorVersion() >= 9) {
|
2016-05-28 15:47:57 +02:00
|
|
|
ItemStack item = inventory.getItem(40);
|
|
|
|
if (item == null) {
|
|
|
|
slotFree.put(40, product.getMaxStackSize());
|
|
|
|
} else {
|
2016-05-31 18:07:38 +02:00
|
|
|
if (item.isSimilar(product)) {
|
2016-05-28 15:47:57 +02:00
|
|
|
int amountInSlot = item.getAmount();
|
|
|
|
int amountToFullStack = product.getMaxStackSize() - amountInSlot;
|
|
|
|
slotFree.put(40, amountToFullStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int freeAmount = 0;
|
|
|
|
for (int value : slotFree.values()) {
|
|
|
|
freeAmount += value;
|
|
|
|
}
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
if (freeAmount >= product.getAmount()) {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug(executor.getName() + " has enough inventory space (#" + shop.getID() + ")");
|
2016-05-28 15:47:57 +02:00
|
|
|
|
|
|
|
EconomyResponse r = econ.withdrawPlayer(executor, shop.getBuyPrice());
|
2016-05-31 18:07:38 +02:00
|
|
|
EconomyResponse r2 = (shop.getShopType() != ShopType.ADMIN) ? econ.depositPlayer(shop.getVendor(), shop.getBuyPrice()) : null;
|
2016-05-28 15:47:57 +02:00
|
|
|
|
|
|
|
if (r.transactionSuccess()) {
|
|
|
|
if (r2 != null) {
|
|
|
|
if (r2.transactionSuccess()) {
|
2016-07-05 18:41:31 +02:00
|
|
|
ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.BUY);
|
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
if (event.isCancelled()) {
|
|
|
|
econ.depositPlayer(executor, shop.getBuyPrice());
|
|
|
|
econ.withdrawPlayer(shop.getVendor(), shop.getBuyPrice());
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Buy event cancelled (#" + shop.getID() + ")");
|
2016-07-05 18:41:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
addToInventory(inventory, product);
|
|
|
|
removeFromInventory(c.getInventory(), product);
|
|
|
|
executor.updateInventory();
|
2016-06-22 20:51:19 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUY_SUCCESS, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(product.getAmount())),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(product)), new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(shop.getBuyPrice())),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.VENDOR, shop.getVendor().getName())));
|
2016-05-31 18:07:38 +02:00
|
|
|
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug(executor.getName() + " successfully bought (#" + shop.getID() + ")");
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
if (shop.getVendor().isOnline()) {
|
2016-06-22 20:51:19 +02:00
|
|
|
shop.getVendor().getPlayer().sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SOMEONE_BOUGHT, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(product.getAmount())),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(product)), new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(shop.getBuyPrice())),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.PLAYER, executor.getName())));
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
2016-05-31 18:07:38 +02:00
|
|
|
|
2016-05-28 15:47:57 +02:00
|
|
|
} else {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Economy transaction failed: " + r2.errorMessage + " (#" + shop.getID() + ")");
|
2016-06-22 20:51:19 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED, new LocalizedMessage.ReplacedRegex(Regex.ERROR, r2.errorMessage)));
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-07-05 18:41:31 +02:00
|
|
|
ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.BUY);
|
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
if (event.isCancelled()) {
|
|
|
|
econ.depositPlayer(executor, shop.getBuyPrice());
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Buy event cancelled (#" + shop.getID() + ")");
|
2016-07-05 18:41:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
addToInventory(inventory, product);
|
|
|
|
executor.updateInventory();
|
2016-06-22 20:51:19 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUY_SUCESS_ADMIN, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(product.getAmount())),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(product)), new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(shop.getBuyPrice()))));
|
2016-08-03 13:49:31 +02:00
|
|
|
|
|
|
|
plugin.debug(executor.getName() + " successfully bought (#" + shop.getID() + ")");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Economy transaction failed: " + r.errorMessage + " (#" + shop.getID() + ")");
|
2016-06-22 20:51:19 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED, new LocalizedMessage.ReplacedRegex(Regex.ERROR, r.errorMessage)));
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NOT_ENOUGH_INVENTORY_SPACE));
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NOT_ENOUGH_MONEY));
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-30 21:59:06 +02:00
|
|
|
/**
|
|
|
|
* A player sells to a shop
|
|
|
|
* @param executor Player, who executed the command and will sell the product
|
|
|
|
* @param shop Shop, to which the player sells
|
|
|
|
*/
|
2016-05-28 15:47:57 +02:00
|
|
|
private void sell(Player executor, Shop shop) {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug(executor.getName() + " is selling (#" + shop.getID() + ")");
|
|
|
|
|
2016-06-09 19:39:00 +02:00
|
|
|
if (econ.getBalance(shop.getVendor()) >= shop.getSellPrice() || shop.getShopType() == ShopType.ADMIN) {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Vendor has enough money (#" + shop.getID() + ")");
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
Block block = shop.getLocation().getBlock();
|
|
|
|
Chest chest = (Chest) block.getState();
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
HashMap<Integer, Integer> slotFree = new HashMap<>();
|
|
|
|
ItemStack product = new ItemStack(shop.getProduct());
|
|
|
|
Inventory inventory = chest.getInventory();
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
for (int i = 0; i < inventory.getSize(); i++) {
|
|
|
|
ItemStack item = inventory.getItem(i);
|
|
|
|
if (item == null) {
|
|
|
|
slotFree.put(i, product.getMaxStackSize());
|
|
|
|
} else {
|
|
|
|
if (item.isSimilar(product)) {
|
|
|
|
int amountInSlot = item.getAmount();
|
|
|
|
int amountToFullStack = product.getMaxStackSize() - amountInSlot;
|
|
|
|
slotFree.put(i, amountToFullStack);
|
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
int freeAmount = 0;
|
|
|
|
for (int value : slotFree.values()) {
|
|
|
|
freeAmount += value;
|
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
if (freeAmount >= product.getAmount()) {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Chest has enough inventory space (#" + shop.getID() + ")");
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-06-08 16:17:33 +02:00
|
|
|
EconomyResponse r = econ.depositPlayer(executor, shop.getSellPrice());
|
|
|
|
EconomyResponse r2 = (shop.getShopType() != ShopType.ADMIN) ? econ.withdrawPlayer(shop.getVendor(), shop.getSellPrice()) : null;
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
if (r.transactionSuccess()) {
|
|
|
|
if (r2 != null) {
|
2016-05-28 15:47:57 +02:00
|
|
|
if (r2.transactionSuccess()) {
|
2016-07-05 18:41:31 +02:00
|
|
|
ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.SELL);
|
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
if (event.isCancelled()) {
|
|
|
|
econ.withdrawPlayer(executor, shop.getBuyPrice());
|
|
|
|
econ.depositPlayer(shop.getVendor(), shop.getBuyPrice());
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Sell event cancelled (#" + shop.getID() + ")");
|
2016-07-05 18:41:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
addToInventory(inventory, product);
|
|
|
|
removeFromInventory(executor.getInventory(), product);
|
|
|
|
executor.updateInventory();
|
2016-06-22 20:51:19 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SELL_SUCESS, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(product.getAmount())),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(product)), new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(shop.getSellPrice())),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.VENDOR, shop.getVendor().getName())));
|
2016-05-31 18:07:38 +02:00
|
|
|
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug(executor.getName() + " successfully sold (#" + shop.getID() + ")");
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
if (shop.getVendor().isOnline()) {
|
2016-06-22 20:51:19 +02:00
|
|
|
shop.getVendor().getPlayer().sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SOMEONE_SOLD, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(product.getAmount())),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(product)), new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(shop.getSellPrice())),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.PLAYER, executor.getName())));
|
2016-05-31 18:07:38 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 15:47:57 +02:00
|
|
|
} else {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Economy transaction failed: " + r2.errorMessage + " (#" + shop.getID() + ")");
|
2016-06-22 20:51:19 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED, new LocalizedMessage.ReplacedRegex(Regex.ERROR, r2.errorMessage)));
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
2016-05-31 18:07:38 +02:00
|
|
|
|
2016-05-28 15:47:57 +02:00
|
|
|
} else {
|
2016-07-05 18:41:31 +02:00
|
|
|
ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.SELL);
|
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
if (event.isCancelled()) {
|
|
|
|
econ.withdrawPlayer(executor, shop.getBuyPrice());
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Sell event cancelled (#" + shop.getID() + ")");
|
2016-07-05 18:41:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
removeFromInventory(executor.getInventory(), product);
|
|
|
|
executor.updateInventory();
|
2016-06-22 20:51:19 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SELL_SUCESS_ADMIN, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(product.getAmount())),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(product)), new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(shop.getSellPrice()))));
|
2016-08-03 13:49:31 +02:00
|
|
|
|
|
|
|
plugin.debug(executor.getName() + " successfully sold (#" + shop.getID() + ")");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Economy transaction failed: " + r.errorMessage + " (#" + shop.getID() + ")");
|
2016-06-22 20:51:19 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED, new LocalizedMessage.ReplacedRegex(Regex.ERROR, r.errorMessage)));
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_NOT_ENOUGH_INVENTORY_SPACE));
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
} else {
|
2016-06-22 20:51:19 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.VENDOR_NOT_ENOUGH_MONEY));
|
2016-05-31 18:07:38 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-06-30 21:59:06 +02:00
|
|
|
/**
|
|
|
|
* Adds items to an inventory
|
|
|
|
* @param inventory The inventory, to which the items will be added
|
|
|
|
* @param itemStack Items to add
|
|
|
|
* @return Whether all items were added to the inventory
|
|
|
|
*/
|
2016-05-31 18:07:38 +02:00
|
|
|
private boolean addToInventory(Inventory inventory, ItemStack itemStack) {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Adding items to inventory...");
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
HashMap<Integer, ItemStack> inventoryItems = new HashMap<>();
|
|
|
|
int amount = itemStack.getAmount();
|
|
|
|
int added = 0;
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
if (inventory instanceof PlayerInventory) {
|
2016-06-22 20:51:19 +02:00
|
|
|
if (Utils.getMajorVersion() >= 9) {
|
2016-05-31 18:07:38 +02:00
|
|
|
inventoryItems.put(40, inventory.getItem(40));
|
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
for (int i = 0; i < 36; i++) {
|
|
|
|
inventoryItems.put(i, inventory.getItem(i));
|
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < inventory.getSize(); i++) {
|
|
|
|
inventoryItems.put(i, inventory.getItem(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
slotLoop:
|
|
|
|
for (int slot : inventoryItems.keySet()) {
|
|
|
|
while (added < amount) {
|
|
|
|
ItemStack item = inventory.getItem(slot);
|
|
|
|
|
|
|
|
if (item != null) {
|
|
|
|
if (item.isSimilar(itemStack)) {
|
|
|
|
if (item.getAmount() != item.getMaxStackSize()) {
|
|
|
|
ItemStack newItemStack = new ItemStack(item);
|
|
|
|
newItemStack.setAmount(item.getAmount() + 1);
|
|
|
|
inventory.setItem(slot, newItemStack);
|
|
|
|
added++;
|
2016-05-28 15:47:57 +02:00
|
|
|
} else {
|
2016-05-31 18:07:38 +02:00
|
|
|
continue slotLoop;
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-31 18:07:38 +02:00
|
|
|
continue slotLoop;
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
2016-05-31 18:07:38 +02:00
|
|
|
} else {
|
|
|
|
ItemStack newItemStack = new ItemStack(itemStack);
|
|
|
|
newItemStack.setAmount(1);
|
|
|
|
inventory.setItem(slot, newItemStack);
|
|
|
|
added++;
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
2016-05-31 18:07:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (added == amount);
|
|
|
|
}
|
|
|
|
|
2016-06-30 21:59:06 +02:00
|
|
|
/**
|
|
|
|
* Removes items to from an inventory
|
|
|
|
* @param inventory The inventory, from which the items will be removed
|
|
|
|
* @param itemStack Items to remove
|
|
|
|
* @return Whether all items were removed from the inventory
|
|
|
|
*/
|
2016-05-31 18:07:38 +02:00
|
|
|
private boolean removeFromInventory(Inventory inventory, ItemStack itemStack) {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug("Removing items from inventory...");
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
HashMap<Integer, ItemStack> inventoryItems = new HashMap<>();
|
|
|
|
int amount = itemStack.getAmount();
|
|
|
|
int removed = 0;
|
|
|
|
|
|
|
|
if (inventory instanceof PlayerInventory) {
|
2016-06-22 20:51:19 +02:00
|
|
|
if (Utils.getMajorVersion() >= 9) {
|
2016-05-31 18:07:38 +02:00
|
|
|
inventoryItems.put(40, inventory.getItem(40));
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
for (int i = 0; i < 36; i++) {
|
|
|
|
inventoryItems.put(i, inventory.getItem(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < inventory.getSize(); i++) {
|
|
|
|
inventoryItems.put(i, inventory.getItem(i));
|
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
2016-05-31 18:07:38 +02:00
|
|
|
|
|
|
|
slotLoop:
|
|
|
|
for (int slot : inventoryItems.keySet()) {
|
|
|
|
while (removed < amount) {
|
|
|
|
ItemStack item = inventory.getItem(slot);
|
|
|
|
|
|
|
|
if (item != null) {
|
|
|
|
if (item.isSimilar(itemStack)) {
|
|
|
|
if (item.getAmount() > 0) {
|
|
|
|
int newAmount = item.getAmount() - 1;
|
|
|
|
|
|
|
|
ItemStack newItemStack = new ItemStack(item);
|
|
|
|
newItemStack.setAmount(newAmount);
|
|
|
|
|
|
|
|
if (newAmount == 0)
|
|
|
|
inventory.setItem(slot, null);
|
|
|
|
else
|
|
|
|
inventory.setItem(slot, newItemStack);
|
|
|
|
|
|
|
|
removed++;
|
|
|
|
} else {
|
|
|
|
continue slotLoop;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continue slotLoop;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continue slotLoop;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (removed == amount);
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
2015-09-02 13:06:48 +02:00
|
|
|
|
|
|
|
}
|