2016-06-28 18:30:48 +02:00
|
|
|
package de.epiceric.shopchest.listeners;
|
2015-09-02 13:06:48 +02:00
|
|
|
|
2016-11-20 14:15:53 +01:00
|
|
|
import com.palmergames.bukkit.towny.object.TownBlock;
|
|
|
|
import com.palmergames.bukkit.towny.object.TownBlockType;
|
|
|
|
import com.palmergames.bukkit.towny.object.TownyUniverse;
|
2016-11-13 13:43:03 +01:00
|
|
|
import com.sk89q.worldguard.bukkit.RegionContainer;
|
|
|
|
import com.sk89q.worldguard.bukkit.RegionQuery;
|
|
|
|
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
|
|
|
import com.sk89q.worldguard.protection.flags.StateFlag;
|
2016-05-28 15:47:57 +02:00
|
|
|
import de.epiceric.shopchest.ShopChest;
|
2016-08-16 15:06:26 +02:00
|
|
|
import de.epiceric.shopchest.config.Config;
|
2016-06-22 20:51:19 +02:00
|
|
|
import de.epiceric.shopchest.config.Regex;
|
2017-03-21 15:03:12 +01:00
|
|
|
import de.epiceric.shopchest.event.*;
|
2016-06-22 20:51:19 +02:00
|
|
|
import de.epiceric.shopchest.language.LanguageUtils;
|
|
|
|
import de.epiceric.shopchest.language.LocalizedMessage;
|
2016-11-18 17:32:52 +01:00
|
|
|
import de.epiceric.shopchest.nms.Hologram;
|
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;
|
2016-11-18 21:14:01 +01:00
|
|
|
import de.epiceric.shopchest.utils.Permissions;
|
2016-06-22 20:51:19 +02:00
|
|
|
import de.epiceric.shopchest.utils.ShopUtils;
|
|
|
|
import de.epiceric.shopchest.utils.Utils;
|
2016-11-13 13:43:03 +01:00
|
|
|
import de.epiceric.shopchest.worldguard.ShopFlag;
|
2017-03-17 14:36:24 +01:00
|
|
|
import fr.xephi.authme.AuthMe;
|
2016-05-28 15:47:57 +02:00
|
|
|
import net.milkbowl.vault.economy.Economy;
|
|
|
|
import net.milkbowl.vault.economy.EconomyResponse;
|
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;
|
2016-11-20 14:15:53 +01:00
|
|
|
import org.bukkit.block.DoubleChest;
|
2015-09-02 13:06:48 +02:00
|
|
|
import org.bukkit.enchantments.Enchantment;
|
2016-11-18 17:32:52 +01:00
|
|
|
import org.bukkit.entity.ArmorStand;
|
|
|
|
import org.bukkit.entity.Entity;
|
2015-09-02 13:06:48 +02:00
|
|
|
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;
|
2016-11-18 17:32:52 +01:00
|
|
|
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
2017-03-17 15:01:11 +01:00
|
|
|
import org.bukkit.event.player.PlayerArmorStandManipulateEvent;
|
2016-11-18 17:32:52 +01:00
|
|
|
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
|
2015-09-02 13:06:48 +02:00
|
|
|
import org.bukkit.event.player.PlayerInteractEvent;
|
2016-11-20 14:15:53 +01:00
|
|
|
import org.bukkit.inventory.*;
|
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 Economy econ;
|
|
|
|
private Database database;
|
2016-07-13 15:03:54 +02:00
|
|
|
private ShopUtils shopUtils;
|
2016-08-16 15:06:26 +02:00
|
|
|
private Config config;
|
2016-11-13 13:43:03 +01:00
|
|
|
private WorldGuardPlugin worldGuard;
|
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.econ = plugin.getEconomy();
|
|
|
|
this.database = plugin.getShopDatabase();
|
2016-07-13 15:03:54 +02:00
|
|
|
this.shopUtils = plugin.getShopUtils();
|
2016-08-16 15:06:26 +02:00
|
|
|
this.config = plugin.getShopChestConfig();
|
2016-11-13 13:43:03 +01:00
|
|
|
this.worldGuard = plugin.getWorldGuard();
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
2017-03-17 15:01:11 +01:00
|
|
|
@EventHandler(ignoreCancelled = true)
|
|
|
|
public void onPlayerManipulateArmorStand(PlayerArmorStandManipulateEvent e) {
|
|
|
|
// When clicking an armor stand with an armor item, the armor stand will take it.
|
|
|
|
// As a hologram consists of armor stands, they would also take the item.
|
|
|
|
ArmorStand armorStand = e.getRightClicked();
|
|
|
|
if (Hologram.isPartOfHologram(armorStand)) {
|
|
|
|
e.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2017-03-17 14:36:24 +01:00
|
|
|
if (config.enable_authme_integration && plugin.hasAuthMe() && !AuthMe.getApi().isAuthenticated(p)) return;
|
|
|
|
|
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())) {
|
2016-11-13 13:43:03 +01:00
|
|
|
|
2016-11-20 14:15:53 +01:00
|
|
|
boolean externalPluginsAllowed = true;
|
|
|
|
|
|
|
|
Location[] chestLocations = {b.getLocation(), null};
|
|
|
|
|
|
|
|
InventoryHolder ih = ((Chest) b.getState()).getInventory().getHolder();
|
|
|
|
if (ih instanceof DoubleChest) {
|
|
|
|
DoubleChest dc = (DoubleChest) ih;
|
|
|
|
chestLocations[0] = ((Chest) dc.getLeftSide()).getLocation();
|
|
|
|
chestLocations[1] = ((Chest) dc.getRightSide()).getLocation();
|
|
|
|
}
|
2016-11-13 13:43:03 +01:00
|
|
|
|
2016-11-23 16:05:33 +01:00
|
|
|
if (plugin.hasWorldGuard() && config.enable_worldguard_integration) {
|
2016-11-13 13:43:03 +01:00
|
|
|
RegionContainer container = worldGuard.getRegionContainer();
|
|
|
|
RegionQuery query = container.createQuery();
|
2016-11-20 14:15:53 +01:00
|
|
|
|
|
|
|
for (Location loc : chestLocations) {
|
|
|
|
if (loc != null) {
|
|
|
|
externalPluginsAllowed &= query.testState(loc, p, ShopFlag.CREATE_SHOP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 16:05:33 +01:00
|
|
|
if (plugin.hasTowny() && config.enable_towny_integration) {
|
2016-11-20 14:15:53 +01:00
|
|
|
for (Location loc : chestLocations) {
|
|
|
|
if (loc != null) {
|
|
|
|
TownBlock townBlock = TownyUniverse.getTownBlock(loc);
|
|
|
|
externalPluginsAllowed &= (townBlock != null && townBlock.getType() == TownBlockType.COMMERCIAL);
|
|
|
|
}
|
|
|
|
}
|
2016-11-13 13:43:03 +01:00
|
|
|
}
|
|
|
|
|
2016-11-20 14:15:53 +01:00
|
|
|
if ((e.isCancelled() || !externalPluginsAllowed) && !p.hasPermission(Permissions.CREATE_PROTECTED)) {
|
2016-08-06 13:32:37 +02:00
|
|
|
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");
|
2016-11-13 13:43:03 +01:00
|
|
|
e.setCancelled(true);
|
2016-08-06 13:32:37 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 15:03:12 +01:00
|
|
|
private void handleInteractEvent(PlayerInteractEvent e) {
|
2016-05-28 15:47:57 +02:00
|
|
|
Block b = e.getClickedBlock();
|
|
|
|
Player p = e.getPlayer();
|
2017-01-03 15:35:35 +01:00
|
|
|
boolean inverted = config.invert_mouse_buttons;
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2017-03-21 15:40:23 +01:00
|
|
|
if (Utils.getMajorVersion() >= 9) {
|
|
|
|
if (e.getHand() == EquipmentSlot.OFF_HAND) return;
|
|
|
|
}
|
|
|
|
|
2016-05-28 15:47:57 +02:00
|
|
|
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)) {
|
2017-01-03 15:35:35 +01:00
|
|
|
if (ClickType.getPlayerClickType(p) != null) {
|
|
|
|
if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
|
2016-05-28 15:47:57 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-01-04 14:34:34 +01:00
|
|
|
if (shop.getShopType() == ShopType.ADMIN) {
|
|
|
|
if (p.hasPermission(Permissions.REMOVE_ADMIN)) {
|
|
|
|
remove(p, shop);
|
|
|
|
} else {
|
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_REMOVE_ADMIN));
|
|
|
|
plugin.debug(p.getName() + " is not permitted to remove an admin shop");
|
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
} else {
|
2017-01-04 14:34:34 +01:00
|
|
|
if (shop.getVendor().getUniqueId().equals(p.getUniqueId()) || p.hasPermission(Permissions.REMOVE_OTHER)) {
|
|
|
|
remove(p, shop);
|
|
|
|
} else {
|
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_REMOVE_OTHERS));
|
|
|
|
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;
|
|
|
|
|
2017-03-21 14:25:49 +01:00
|
|
|
case OPEN:
|
|
|
|
e.setCancelled(true);
|
2017-01-03 15:35:35 +01:00
|
|
|
|
2017-03-21 14:25:49 +01:00
|
|
|
if (shopUtils.isShop(b.getLocation())) {
|
|
|
|
Shop shop = shopUtils.getShop(b.getLocation());
|
|
|
|
if (p.getUniqueId().equals(shop.getVendor().getUniqueId()) || p.hasPermission(Permissions.OPEN_OTHER)) {
|
2017-03-21 15:03:12 +01:00
|
|
|
open(p, shop, true);
|
2017-03-21 14:25:49 +01:00
|
|
|
} else {
|
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_OPEN_OTHERS));
|
|
|
|
plugin.debug(p.getName() + " is not permitted to open another player's shop");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
2017-03-21 14:25:49 +01:00
|
|
|
} else {
|
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_NO_SHOP));
|
|
|
|
plugin.debug("Chest is not a shop");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
2016-11-13 13:43:03 +01:00
|
|
|
|
2017-03-21 14:25:49 +01:00
|
|
|
ClickType.removePlayerClickType(p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (shopUtils.isShop(b.getLocation())) {
|
|
|
|
Shop shop = shopUtils.getShop(b.getLocation());
|
|
|
|
|
2017-03-25 13:45:17 +01:00
|
|
|
if (e.getAction() == Action.LEFT_CLICK_BLOCK && p.isSneaking() && Utils.hasAxeInHand(p)) {
|
|
|
|
return;
|
2017-01-03 15:35:35 +01:00
|
|
|
}
|
|
|
|
|
2017-03-21 15:03:12 +01:00
|
|
|
if (e.getAction() == Action.RIGHT_CLICK_BLOCK && p.getUniqueId().equals(shop.getVendor().getUniqueId()) && shop.getShopType() != ShopType.ADMIN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-03 15:35:35 +01:00
|
|
|
if ((e.getAction() == Action.RIGHT_CLICK_BLOCK && !inverted) || (e.getAction() == Action.LEFT_CLICK_BLOCK && inverted)) {
|
|
|
|
e.setCancelled(true);
|
|
|
|
|
|
|
|
if (shop.getShopType() == ShopType.ADMIN || !shop.getVendor().getUniqueId().equals(p.getUniqueId())) {
|
|
|
|
plugin.debug(p.getName() + " wants to buy");
|
|
|
|
|
|
|
|
if (shop.getBuyPrice() > 0) {
|
|
|
|
if (p.hasPermission(Permissions.BUY)) {
|
|
|
|
boolean worldGuardAllowed = true;
|
|
|
|
|
|
|
|
if (shop.getShopType() == ShopType.ADMIN) {
|
|
|
|
if (plugin.hasWorldGuard() && config.enable_worldguard_integration) {
|
|
|
|
RegionContainer container = worldGuard.getRegionContainer();
|
|
|
|
RegionQuery query = container.createQuery();
|
|
|
|
worldGuardAllowed = query.testState(b.getLocation(), p, ShopFlag.USE_ADMIN_SHOP);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (worldGuardAllowed || p.hasPermission(Permissions.WORLDGUARD_BYPASS)) {
|
2017-03-21 15:40:23 +01:00
|
|
|
buy(p, shop, p.isSneaking());
|
2016-11-13 13:43:03 +01:00
|
|
|
} else {
|
2017-01-03 15:35:35 +01:00
|
|
|
plugin.debug(p.getName() + " doesn't have worldguard permission");
|
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_WG_BUY));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (plugin.hasWorldGuard() && config.enable_worldguard_integration) {
|
|
|
|
RegionContainer container = worldGuard.getRegionContainer();
|
|
|
|
RegionQuery query = container.createQuery();
|
|
|
|
worldGuardAllowed = query.testState(b.getLocation(), p, ShopFlag.USE_SHOP);
|
|
|
|
}
|
2016-11-13 13:43:03 +01:00
|
|
|
|
2017-01-03 15:35:35 +01:00
|
|
|
if (worldGuardAllowed || p.hasPermission(Permissions.WORLDGUARD_BYPASS)) {
|
|
|
|
Chest c = (Chest) b.getState();
|
|
|
|
if (Utils.getAmount(c.getInventory(), shop.getProduct()) >= shop.getProduct().getAmount()) {
|
2017-03-21 15:40:23 +01:00
|
|
|
buy(p, shop, p.isSneaking());
|
2017-01-03 15:35:35 +01:00
|
|
|
} else {
|
|
|
|
if (config.auto_calculate_item_amount && Utils.getAmount(c.getInventory(), shop.getProduct()) > 0) {
|
2017-03-21 15:40:23 +01:00
|
|
|
buy(p, shop, p.isSneaking());
|
2016-08-16 15:06:26 +02:00
|
|
|
} else {
|
2017-01-03 15:35:35 +01:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OUT_OF_STOCK));
|
2017-01-04 18:46:50 +01:00
|
|
|
if (shop.getVendor().isOnline() && config.enable_vendor_messages) {
|
|
|
|
shop.getVendor().getPlayer().sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.VENDOR_OUT_OF_STOCK,
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(shop.getProduct().getAmount())),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(shop.getProduct()))));
|
|
|
|
}
|
2017-01-03 15:35:35 +01:00
|
|
|
plugin.debug("Shop is out of stock");
|
2016-08-16 15:06:26 +02:00
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
2017-01-03 15:35:35 +01:00
|
|
|
} else {
|
|
|
|
plugin.debug(p.getName() + " doesn't have worldguard permission");
|
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_WG_BUY));
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-01-03 15:35:35 +01:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_BUY));
|
|
|
|
plugin.debug(p.getName() + " is not permitted to buy");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-01-03 15:35:35 +01:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUYING_DISABLED));
|
|
|
|
plugin.debug("Buying is disabled");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 15:35:35 +01:00
|
|
|
} else if ((e.getAction() == Action.LEFT_CLICK_BLOCK && !inverted) || (e.getAction() == Action.RIGHT_CLICK_BLOCK && inverted)) {
|
|
|
|
e.setCancelled(true);
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2017-01-03 15:35:35 +01:00
|
|
|
if ((shop.getShopType() == ShopType.ADMIN) || (!shop.getVendor().getUniqueId().equals(p.getUniqueId()))) {
|
|
|
|
plugin.debug(p.getName() + " wants to sell");
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2017-01-03 15:35:35 +01:00
|
|
|
if (shop.getSellPrice() > 0) {
|
|
|
|
if (p.hasPermission(Permissions.SELL)) {
|
|
|
|
boolean worldGuardAllowed = true;
|
2016-11-13 13:43:03 +01:00
|
|
|
|
2017-01-03 15:35:35 +01:00
|
|
|
if (plugin.hasWorldGuard() && config.enable_worldguard_integration) {
|
|
|
|
RegionContainer container = worldGuard.getRegionContainer();
|
|
|
|
RegionQuery query = container.createQuery();
|
2016-11-13 13:43:03 +01:00
|
|
|
|
2017-01-03 15:35:35 +01:00
|
|
|
StateFlag flag = (shop.getShopType() == ShopType.ADMIN ? ShopFlag.USE_ADMIN_SHOP : ShopFlag.USE_SHOP);
|
|
|
|
worldGuardAllowed = query.testState(b.getLocation(), p, flag);
|
|
|
|
}
|
2016-11-13 13:43:03 +01:00
|
|
|
|
2017-01-03 15:35:35 +01:00
|
|
|
if (worldGuardAllowed || p.hasPermission(Permissions.WORLDGUARD_BYPASS)) {
|
|
|
|
if (Utils.getAmount(p.getInventory(), shop.getProduct()) >= shop.getProduct().getAmount()) {
|
2017-03-25 13:45:17 +01:00
|
|
|
sell(p, shop, p.isSneaking() && !Utils.hasAxeInHand(p));
|
2016-11-13 13:43:03 +01:00
|
|
|
} else {
|
2017-01-03 15:35:35 +01:00
|
|
|
if (config.auto_calculate_item_amount && Utils.getAmount(p.getInventory(), shop.getProduct()) > 0) {
|
2017-03-25 13:45:17 +01:00
|
|
|
sell(p, shop, p.isSneaking() && !Utils.hasAxeInHand(p));
|
2017-01-03 15:35:35 +01:00
|
|
|
} else {
|
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NOT_ENOUGH_ITEMS));
|
|
|
|
plugin.debug(p.getName() + " doesn't have enough items");
|
|
|
|
}
|
2016-11-13 13:43:03 +01:00
|
|
|
}
|
2017-01-03 15:35:35 +01:00
|
|
|
} else {
|
|
|
|
plugin.debug(p.getName() + " doesn't have worldguard permission");
|
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_WG_SELL));
|
2016-08-16 15:06:26 +02:00
|
|
|
}
|
2016-11-13 13:43:03 +01:00
|
|
|
} else {
|
2017-01-03 15:35:35 +01:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_SELL));
|
|
|
|
plugin.debug(p.getName() + " is not permitted to sell");
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
2016-06-08 16:06:36 +02:00
|
|
|
} else {
|
2017-01-03 15:35:35 +01:00
|
|
|
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SELLING_DISABLED));
|
|
|
|
plugin.debug("Selling is disabled");
|
2016-06-08 16:06:36 +02:00
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
} else {
|
2017-01-03 15:35:35 +01:00
|
|
|
e.setCancelled(false);
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-18 17:32:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onPlayerInteract(PlayerInteractEvent e) {
|
2017-03-17 14:36:24 +01:00
|
|
|
if (config.enable_authme_integration && plugin.hasAuthMe() && !AuthMe.getApi().isAuthenticated(e.getPlayer())) return;
|
2017-03-21 15:03:12 +01:00
|
|
|
handleInteractEvent(e);
|
2016-11-18 17:32:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent e) {
|
|
|
|
if (!plugin.getShopChestConfig().enable_hologram_interaction) return;
|
|
|
|
|
|
|
|
Entity entity = e.getRightClicked();
|
|
|
|
Player p = e.getPlayer();
|
2017-03-17 14:36:24 +01:00
|
|
|
if (config.enable_authme_integration && plugin.hasAuthMe() && !AuthMe.getApi().isAuthenticated(p)) return;
|
2016-11-18 17:32:52 +01:00
|
|
|
|
2016-11-19 21:34:29 +01:00
|
|
|
if (Utils.getMajorVersion() == 8 || e.getHand() == EquipmentSlot.HAND) {
|
2016-11-18 17:32:52 +01:00
|
|
|
if (entity instanceof ArmorStand) {
|
|
|
|
ArmorStand armorStand = (ArmorStand) entity;
|
|
|
|
if (Hologram.isPartOfHologram(armorStand)) {
|
|
|
|
Hologram hologram = Hologram.getHologram(armorStand);
|
|
|
|
if (hologram != null) {
|
|
|
|
Block b = null;
|
|
|
|
for (Shop shop : plugin.getShopUtils().getShops()) {
|
2017-01-04 17:07:00 +01:00
|
|
|
if (shop.getHologram() != null && shop.getHologram().equals(hologram)) {
|
2016-11-18 17:32:52 +01:00
|
|
|
b = shop.getLocation().getBlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b != null) {
|
2016-11-19 21:34:29 +01:00
|
|
|
PlayerInteractEvent interactEvent = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, Utils.getPreferredItemInHand(p), b, null);
|
2017-03-21 15:03:12 +01:00
|
|
|
handleInteractEvent(interactEvent);
|
2016-11-18 17:32:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onPlayerDamageEntity(EntityDamageByEntityEvent e) {
|
|
|
|
if (!plugin.getShopChestConfig().enable_hologram_interaction) return;
|
|
|
|
|
|
|
|
Entity entity = e.getEntity();
|
|
|
|
Entity damager = e.getDamager();
|
|
|
|
|
|
|
|
if (!(damager instanceof Player)) return;
|
|
|
|
Player p = (Player) damager;
|
2017-03-17 14:36:24 +01:00
|
|
|
if (config.enable_authme_integration && plugin.hasAuthMe() && !AuthMe.getApi().isAuthenticated(p)) return;
|
2016-11-18 17:32:52 +01:00
|
|
|
|
|
|
|
if (entity instanceof ArmorStand) {
|
|
|
|
ArmorStand armorStand = (ArmorStand) entity;
|
|
|
|
if (Hologram.isPartOfHologram(armorStand)) {
|
|
|
|
Hologram hologram = Hologram.getHologram(armorStand);
|
|
|
|
if (hologram != null) {
|
|
|
|
Block b = null;
|
|
|
|
for (Shop shop : plugin.getShopUtils().getShops()) {
|
2017-01-04 17:07:00 +01:00
|
|
|
if (shop.getHologram() != null && shop.getHologram().equals(hologram)) {
|
2016-11-18 17:32:52 +01:00
|
|
|
b = shop.getLocation().getBlock();
|
|
|
|
}
|
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-11-18 17:32:52 +01:00
|
|
|
if (b != null) {
|
2016-11-19 21:34:29 +01:00
|
|
|
PlayerInteractEvent interactEvent = new PlayerInteractEvent(p, Action.LEFT_CLICK_BLOCK, Utils.getPreferredItemInHand(p), b, null);
|
2017-03-21 15:03:12 +01:00
|
|
|
handleInteractEvent(interactEvent);
|
2016-11-19 21:34:29 +01:00
|
|
|
e.setCancelled(true);
|
2016-11-18 17:32:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
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-08-16 15:06:26 +02:00
|
|
|
double creationPrice = (shopType == ShopType.NORMAL) ? config.shop_creation_price_normal : config.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-11-23 16:11:07 +01:00
|
|
|
EconomyResponse r = plugin.getEconomy().withdrawPlayer(executor, location.getWorld().getName(), creationPrice);
|
2016-07-01 14:33:13 +02:00
|
|
|
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
|
|
|
|
2016-10-12 18:30:51 +02:00
|
|
|
for (Player p : location.getWorld().getPlayers()) {
|
2016-11-12 15:43:56 +01:00
|
|
|
if (p.getLocation().distanceSquared(location) <= Math.pow(config.maximal_distance, 2)) {
|
2016-11-12 15:17:13 +01:00
|
|
|
if (shop.getHologram() != null) {
|
|
|
|
shop.getHologram().showPlayer(p);
|
|
|
|
}
|
2016-10-12 18:30:51 +02:00
|
|
|
}
|
2016-11-12 15:43:56 +01:00
|
|
|
if (p.getLocation().distanceSquared(location) <= Math.pow(config.maximal_item_distance, 2)) {
|
2016-11-12 15:17:13 +01:00
|
|
|
if (shop.getItem() != null) {
|
|
|
|
shop.getItem().setVisible(p, true);
|
|
|
|
}
|
2016-10-12 18:30:51 +02:00
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-03-21 15:03:12 +01:00
|
|
|
plugin.debug(executor.getName() + " is removing " + shop.getVendor().getPlayer().getName() + "'s 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
|
|
|
}
|
|
|
|
|
2017-03-21 15:03:12 +01:00
|
|
|
/**
|
|
|
|
* Open a shop
|
|
|
|
* @param executor Player, who executed the command and will receive the message
|
|
|
|
* @param shop Shop to be opened
|
|
|
|
*/
|
|
|
|
private void open(Player executor, Shop shop, boolean message) {
|
|
|
|
plugin.debug(executor.getName() + " is opening " + shop.getVendor().getName() + "'s shop (#" + shop.getID() + ")");
|
|
|
|
ShopOpenEvent event = new ShopOpenEvent(executor, shop);
|
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
if (event.isCancelled()) {
|
|
|
|
plugin.debug("Open event cancelled (#" + shop.getID() + ")");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
executor.openInventory(shop.getInventoryHolder().getInventory());
|
|
|
|
plugin.debug("Opened shop (#" + shop.getID() + ")");
|
|
|
|
if (message) executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OPENED_SHOP, new LocalizedMessage.ReplacedRegex(Regex.VENDOR, shop.getVendor().getName())));
|
|
|
|
}
|
|
|
|
|
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-08-19 12:48:11 +02:00
|
|
|
String vendorName = (shop.getVendor().getName() == null ? shop.getVendor().getUniqueId().toString() : shop.getVendor().getName());
|
|
|
|
String vendor = LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_INFO_VENDOR, new LocalizedMessage.ReplacedRegex(Regex.VENDOR, vendorName));
|
2016-06-22 20:51:19 +02:00
|
|
|
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);
|
2016-09-06 11:43:01 +02:00
|
|
|
String disabled = LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_INFO_DISABLED);
|
|
|
|
String price = LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_INFO_PRICE, new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, (shop.getBuyPrice() > 0 ? String.valueOf(shop.getBuyPrice()) : disabled)),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, (shop.getSellPrice() > 0 ? String.valueOf(shop.getSellPrice()) : disabled)));
|
2016-06-22 20:51:19 +02:00
|
|
|
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
|
2017-03-21 15:40:23 +01:00
|
|
|
* @param stack Whether a whole stack should be bought
|
2016-06-30 21:59:06 +02:00
|
|
|
*/
|
2017-03-21 15:40:23 +01:00
|
|
|
private void buy(Player executor, Shop shop, boolean stack) {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug(executor.getName() + " is buying (#" + shop.getID() + ")");
|
2016-08-16 15:06:26 +02:00
|
|
|
|
2017-03-21 15:40:23 +01:00
|
|
|
int amount = shop.getProduct().getAmount();
|
|
|
|
if (stack) amount = shop.getProduct().getMaxStackSize();
|
|
|
|
|
|
|
|
double price = shop.getBuyPrice();
|
|
|
|
if (stack) price = (price / shop.getProduct().getAmount()) * amount;
|
|
|
|
|
|
|
|
if (econ.getBalance(executor) >= price || config.auto_calculate_item_amount) {
|
|
|
|
|
|
|
|
int amountForMoney = (int) (amount / price * econ.getBalance(executor));
|
2016-08-16 15:06:26 +02:00
|
|
|
|
|
|
|
if (amountForMoney == 0 && config.auto_calculate_item_amount) {
|
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NOT_ENOUGH_MONEY));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin.debug(executor.getName() + " has enough money for " + amountForMoney + " item(s) (#" + shop.getID() + ")");
|
2016-05-28 15:47:57 +02:00
|
|
|
|
|
|
|
Block b = shop.getLocation().getBlock();
|
|
|
|
Chest c = (Chest) b.getState();
|
|
|
|
|
2016-08-17 13:14:35 +02:00
|
|
|
int amountForChestItems = Utils.getAmount(c.getInventory(), shop.getProduct());
|
2016-08-16 15:06:26 +02:00
|
|
|
|
2017-03-21 15:40:23 +01:00
|
|
|
if (amountForChestItems == 0 && shop.getShopType() != ShopType.ADMIN) {
|
2016-08-16 15:06:26 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OUT_OF_STOCK));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
ItemStack product = new ItemStack(shop.getProduct());
|
2017-03-21 15:40:23 +01:00
|
|
|
if (stack) product.setAmount(amount);
|
|
|
|
|
2016-05-28 15:47:57 +02:00
|
|
|
Inventory inventory = executor.getInventory();
|
|
|
|
|
2016-08-16 15:06:26 +02:00
|
|
|
int freeSpace = Utils.getFreeSpaceForItem(inventory, product);
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2017-03-21 15:40:23 +01:00
|
|
|
if (freeSpace == 0) {
|
2016-08-16 15:06:26 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NOT_ENOUGH_INVENTORY_SPACE));
|
|
|
|
return;
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
2017-03-21 15:40:23 +01:00
|
|
|
int newAmount = amount;
|
2016-08-17 13:14:35 +02:00
|
|
|
|
|
|
|
if (config.auto_calculate_item_amount) {
|
|
|
|
if (shop.getShopType() == ShopType.ADMIN)
|
|
|
|
newAmount = Math.min(amountForMoney, freeSpace);
|
|
|
|
else
|
|
|
|
newAmount = Math.min(Math.min(amountForMoney, amountForChestItems), freeSpace);
|
|
|
|
}
|
|
|
|
|
2017-03-21 15:40:23 +01:00
|
|
|
if (newAmount > amount) newAmount = amount;
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2017-03-21 15:40:23 +01:00
|
|
|
double newPrice = (price / amount) * newAmount;
|
2016-08-16 15:06:26 +02:00
|
|
|
|
2017-03-21 15:40:23 +01:00
|
|
|
if (freeSpace >= newAmount) {
|
2016-08-16 15:06:26 +02:00
|
|
|
plugin.debug(executor.getName() + " has enough inventory space for " + freeSpace + " items (#" + shop.getID() + ")");
|
|
|
|
|
|
|
|
ItemStack newProduct = new ItemStack(product);
|
|
|
|
newProduct.setAmount(newAmount);
|
|
|
|
|
2016-11-23 16:11:07 +01:00
|
|
|
String worldName = shop.getLocation().getWorld().getName();
|
|
|
|
|
|
|
|
EconomyResponse r = econ.withdrawPlayer(executor, worldName, newPrice);
|
2016-05-28 15:47:57 +02:00
|
|
|
|
|
|
|
if (r.transactionSuccess()) {
|
2016-11-23 16:11:07 +01:00
|
|
|
EconomyResponse r2 = (shop.getShopType() != ShopType.ADMIN) ? econ.depositPlayer(shop.getVendor(), worldName, newPrice) : null;
|
2016-09-07 17:00:59 +02:00
|
|
|
|
2016-05-28 15:47:57 +02:00
|
|
|
if (r2 != null) {
|
|
|
|
if (r2.transactionSuccess()) {
|
2016-08-16 15:06:26 +02:00
|
|
|
ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.BUY, newAmount, newPrice);
|
2016-07-05 18:41:31 +02:00
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
if (event.isCancelled()) {
|
2016-08-16 15:06:26 +02:00
|
|
|
econ.depositPlayer(executor, newPrice);
|
|
|
|
econ.withdrawPlayer(shop.getVendor(), newPrice);
|
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-08-18 13:30:14 +02:00
|
|
|
database.logEconomy(executor, newProduct, shop.getVendor(), shop.getShopType(), shop.getLocation(), newPrice, ShopBuySellEvent.Type.BUY);
|
|
|
|
|
2016-08-16 15:06:26 +02:00
|
|
|
addToInventory(inventory, newProduct);
|
|
|
|
removeFromInventory(c.getInventory(), newProduct);
|
2016-05-31 18:07:38 +02:00
|
|
|
executor.updateInventory();
|
2016-08-19 12:48:11 +02:00
|
|
|
|
|
|
|
String vendorName = (shop.getVendor().getName() == null ? shop.getVendor().getUniqueId().toString() : shop.getVendor().getName());
|
2016-08-16 15:06:26 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUY_SUCCESS, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(newAmount)),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(product)), new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(newPrice)),
|
2016-08-19 12:48:11 +02:00
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.VENDOR, vendorName)));
|
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-11-26 17:23:02 +01:00
|
|
|
if (shop.getVendor().isOnline() && config.enable_vendor_messages) {
|
2016-08-16 15:06:26 +02:00
|
|
|
shop.getVendor().getPlayer().sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SOMEONE_BOUGHT, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(newAmount)),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(product)), new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(newPrice)),
|
2016-06-22 20:51:19 +02:00
|
|
|
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-09-07 17:00:59 +02:00
|
|
|
plugin.debug("Economy transaction failed (r2): " + 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-09-07 17:00:59 +02:00
|
|
|
econ.depositPlayer(executor, newPrice);
|
2016-05-28 15:47:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-08-16 15:06:26 +02:00
|
|
|
ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.BUY, newAmount, newPrice);
|
2016-07-05 18:41:31 +02:00
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
if (event.isCancelled()) {
|
2016-08-16 15:06:26 +02:00
|
|
|
econ.depositPlayer(executor, newPrice);
|
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-08-18 13:30:14 +02:00
|
|
|
database.logEconomy(executor, newProduct, shop.getVendor(), shop.getShopType(), shop.getLocation(), newPrice, ShopBuySellEvent.Type.BUY);
|
|
|
|
|
2016-08-16 15:06:26 +02:00
|
|
|
addToInventory(inventory, newProduct);
|
2016-05-31 18:07:38 +02:00
|
|
|
executor.updateInventory();
|
2016-08-16 15:06:26 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUY_SUCESS_ADMIN, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(newAmount)),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(product)), new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(newPrice))));
|
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-09-07 17:00:59 +02:00
|
|
|
plugin.debug("Economy transaction failed (r): " + 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
|
|
|
|
*/
|
2017-03-25 13:45:17 +01:00
|
|
|
private void sell(Player executor, Shop shop, boolean stack) {
|
2016-08-03 13:49:31 +02:00
|
|
|
plugin.debug(executor.getName() + " is selling (#" + shop.getID() + ")");
|
|
|
|
|
2017-03-25 13:45:17 +01:00
|
|
|
int amount = shop.getProduct().getAmount();
|
|
|
|
if (stack) amount = shop.getProduct().getMaxStackSize();
|
|
|
|
|
|
|
|
double price = shop.getSellPrice();
|
|
|
|
if (stack) price = (price / shop.getProduct().getAmount()) * amount;
|
|
|
|
|
|
|
|
if (econ.getBalance(shop.getVendor()) >= price || shop.getShopType() == ShopType.ADMIN || config.auto_calculate_item_amount) {
|
|
|
|
int amountForMoney = (int) (amount / price * econ.getBalance(shop.getVendor()));
|
2016-08-16 15:06:26 +02:00
|
|
|
|
|
|
|
plugin.debug("Vendor has enough money for " + amountForMoney + " item(s) (#" + shop.getID() + ")");
|
|
|
|
|
2016-08-17 13:14:35 +02:00
|
|
|
if (amountForMoney == 0 && config.auto_calculate_item_amount && shop.getShopType() != ShopType.ADMIN) {
|
2016-08-16 15:06:26 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.VENDOR_NOT_ENOUGH_MONEY));
|
|
|
|
return;
|
|
|
|
}
|
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-08-16 15:06:26 +02:00
|
|
|
int amountForItemCount = Utils.getAmount(executor.getInventory(), shop.getProduct());
|
|
|
|
|
2017-03-25 13:45:17 +01:00
|
|
|
if (amountForItemCount == 0) {
|
2016-08-16 15:06:26 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NOT_ENOUGH_ITEMS));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
ItemStack product = new ItemStack(shop.getProduct());
|
2017-03-25 13:45:17 +01:00
|
|
|
if (stack) product.setAmount(amount);
|
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
Inventory inventory = chest.getInventory();
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-08-16 15:06:26 +02:00
|
|
|
int freeSpace = Utils.getFreeSpaceForItem(inventory, product);
|
|
|
|
|
2017-03-25 13:45:17 +01:00
|
|
|
if (freeSpace == 0 && shop.getShopType() != ShopType.ADMIN) {
|
2016-08-16 15:06:26 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_NOT_ENOUGH_INVENTORY_SPACE));
|
|
|
|
return;
|
2016-05-31 18:07:38 +02:00
|
|
|
}
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2017-03-25 13:45:17 +01:00
|
|
|
int newAmount = amount;
|
2016-08-17 13:14:35 +02:00
|
|
|
|
|
|
|
if (config.auto_calculate_item_amount) {
|
|
|
|
if (shop.getShopType() == ShopType.ADMIN)
|
|
|
|
newAmount = amountForItemCount;
|
|
|
|
else
|
2017-03-25 13:45:17 +01:00
|
|
|
newAmount = Math.min(Math.min(amountForMoney, amountForItemCount), freeSpace);
|
2016-08-17 13:14:35 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 13:45:17 +01:00
|
|
|
if (newAmount > amount) newAmount = amount;
|
2016-08-16 15:06:26 +02:00
|
|
|
|
2017-03-25 13:45:17 +01:00
|
|
|
double newPrice = (price / amount) * newAmount;
|
2016-08-16 15:06:26 +02:00
|
|
|
|
2017-03-25 13:45:17 +01:00
|
|
|
if (freeSpace >= newAmount || shop.getShopType() == ShopType.ADMIN) {
|
2016-08-16 15:06:26 +02:00
|
|
|
plugin.debug("Chest has enough inventory space for " + freeSpace + " items (#" + shop.getID() + ")");
|
|
|
|
|
|
|
|
ItemStack newProduct = new ItemStack(product);
|
|
|
|
newProduct.setAmount(newAmount);
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-11-23 16:11:07 +01:00
|
|
|
String worldName = shop.getLocation().getWorld().getName();
|
|
|
|
|
|
|
|
EconomyResponse r = econ.depositPlayer(executor, worldName, newPrice);
|
2016-05-28 15:47:57 +02:00
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
if (r.transactionSuccess()) {
|
2016-11-23 16:11:07 +01:00
|
|
|
EconomyResponse r2 = (shop.getShopType() != ShopType.ADMIN) ? econ.withdrawPlayer(shop.getVendor(), worldName, newPrice) : null;
|
2016-09-07 17:00:59 +02:00
|
|
|
|
2016-05-31 18:07:38 +02:00
|
|
|
if (r2 != null) {
|
2016-05-28 15:47:57 +02:00
|
|
|
if (r2.transactionSuccess()) {
|
2016-08-16 15:06:26 +02:00
|
|
|
ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.SELL, newAmount, newPrice);
|
2016-07-05 18:41:31 +02:00
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
if (event.isCancelled()) {
|
2016-08-16 15:06:26 +02:00
|
|
|
econ.withdrawPlayer(executor, newPrice);
|
|
|
|
econ.depositPlayer(shop.getVendor(), newPrice);
|
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-08-18 13:30:14 +02:00
|
|
|
database.logEconomy(executor, newProduct, shop.getVendor(), shop.getShopType(), shop.getLocation(), newPrice, ShopBuySellEvent.Type.SELL);
|
|
|
|
|
2016-08-16 15:06:26 +02:00
|
|
|
addToInventory(inventory, newProduct);
|
|
|
|
removeFromInventory(executor.getInventory(), newProduct);
|
2016-05-31 18:07:38 +02:00
|
|
|
executor.updateInventory();
|
2016-08-19 12:48:11 +02:00
|
|
|
|
|
|
|
String vendorName = (shop.getVendor().getName() == null ? shop.getVendor().getUniqueId().toString() : shop.getVendor().getName());
|
2016-08-16 15:06:26 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SELL_SUCESS, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(newAmount)),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(product)), new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(newPrice)),
|
2016-08-19 12:48:11 +02:00
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.VENDOR, vendorName)));
|
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-11-26 17:23:02 +01:00
|
|
|
if (shop.getVendor().isOnline() && config.enable_vendor_messages) {
|
2016-08-16 15:06:26 +02:00
|
|
|
shop.getVendor().getPlayer().sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SOMEONE_SOLD, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(newAmount)),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(product)), new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(newPrice)),
|
2016-06-22 20:51:19 +02:00
|
|
|
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-09-07 17:00:59 +02:00
|
|
|
plugin.debug("Economy transaction failed (r2): " + 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-09-07 17:00:59 +02:00
|
|
|
econ.withdrawPlayer(executor, newPrice);
|
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-16 15:06:26 +02:00
|
|
|
ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.SELL, newAmount, newPrice);
|
2016-07-05 18:41:31 +02:00
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
|
|
|
|
if (event.isCancelled()) {
|
2016-08-16 15:06:26 +02:00
|
|
|
econ.withdrawPlayer(executor, newPrice);
|
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-08-18 13:30:14 +02:00
|
|
|
database.logEconomy(executor, newProduct, shop.getVendor(), shop.getShopType(), shop.getLocation(), newPrice, ShopBuySellEvent.Type.SELL);
|
|
|
|
|
2016-08-16 15:06:26 +02:00
|
|
|
removeFromInventory(executor.getInventory(), newProduct);
|
2016-05-31 18:07:38 +02:00
|
|
|
executor.updateInventory();
|
2016-08-16 15:06:26 +02:00
|
|
|
executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SELL_SUCESS_ADMIN, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(newAmount)),
|
|
|
|
new LocalizedMessage.ReplacedRegex(Regex.ITEM_NAME, LanguageUtils.getItemName(product)), new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(newPrice))));
|
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-09-07 17:00:59 +02:00
|
|
|
plugin.debug("Economy transaction failed (r): " + 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);
|
|
|
|
|
2016-11-17 17:39:09 +01:00
|
|
|
if (item != null && item.getType() != Material.AIR) {
|
2017-01-03 13:06:29 +01:00
|
|
|
if (Utils.isItemSimilar(item, itemStack)) {
|
2016-05-31 18:07:38 +02:00
|
|
|
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);
|
|
|
|
|
2016-11-17 17:39:09 +01:00
|
|
|
if (item != null && item.getType() != Material.AIR) {
|
2017-01-03 13:06:29 +01:00
|
|
|
if (Utils.isItemSimilar(item, itemStack)) {
|
2016-05-31 18:07:38 +02:00
|
|
|
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
|
|
|
|
|
|
|
}
|