2012-06-08 15:28:36 +02:00
|
|
|
package com.Acrobot.ChestShop.Listeners.Player;
|
|
|
|
|
2013-02-11 16:55:04 +01:00
|
|
|
import com.Acrobot.Breeze.Utils.*;
|
2012-11-23 21:04:13 +01:00
|
|
|
import com.Acrobot.ChestShop.Configuration.Messages;
|
|
|
|
import com.Acrobot.ChestShop.Configuration.Properties;
|
2012-08-10 19:01:34 +02:00
|
|
|
import com.Acrobot.ChestShop.Containers.AdminInventory;
|
2017-07-01 18:14:41 +02:00
|
|
|
import com.Acrobot.ChestShop.Database.Account;
|
2018-07-11 19:09:07 +02:00
|
|
|
import com.Acrobot.ChestShop.Events.Economy.AccountCheckEvent;
|
2019-01-17 19:50:47 +01:00
|
|
|
import com.Acrobot.ChestShop.Events.ItemParseEvent;
|
2012-07-07 19:24:06 +02:00
|
|
|
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
|
2012-08-10 19:01:34 +02:00
|
|
|
import com.Acrobot.ChestShop.Events.TransactionEvent;
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.ChestShop.Permission;
|
|
|
|
import com.Acrobot.ChestShop.Plugins.ChestShop;
|
|
|
|
import com.Acrobot.ChestShop.Security;
|
|
|
|
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
2014-04-12 13:57:39 +02:00
|
|
|
import com.Acrobot.ChestShop.UUIDs.NameManager;
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
2012-07-07 19:24:06 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2018-01-07 02:11:59 +01:00
|
|
|
import org.bukkit.ChatColor;
|
2013-02-10 15:29:40 +01:00
|
|
|
import org.bukkit.GameMode;
|
2012-06-08 15:28:36 +02:00
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.block.Block;
|
2018-10-23 20:04:18 +02:00
|
|
|
import org.bukkit.block.Container;
|
2012-06-08 15:28:36 +02:00
|
|
|
import org.bukkit.block.Sign;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
import org.bukkit.event.block.Action;
|
2018-01-07 02:11:59 +01:00
|
|
|
import org.bukkit.event.block.SignChangeEvent;
|
2012-06-08 15:28:36 +02:00
|
|
|
import org.bukkit.event.player.PlayerInteractEvent;
|
|
|
|
import org.bukkit.inventory.Inventory;
|
2012-08-10 19:01:34 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2012-06-08 15:28:36 +02:00
|
|
|
|
2013-01-24 22:35:28 +01:00
|
|
|
import static com.Acrobot.Breeze.Utils.BlockUtil.isSign;
|
2012-08-10 19:01:34 +02:00
|
|
|
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType;
|
2013-01-24 22:35:28 +01:00
|
|
|
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;
|
|
|
|
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.SELL;
|
2018-11-19 21:10:48 +01:00
|
|
|
import static com.Acrobot.ChestShop.Permission.OTHER_NAME_ACCESS;
|
|
|
|
import static com.Acrobot.ChestShop.Permission.OTHER_NAME_CREATE;
|
|
|
|
import static com.Acrobot.ChestShop.Permission.OTHER_NAME_DESTROY;
|
2012-08-10 19:01:34 +02:00
|
|
|
import static com.Acrobot.ChestShop.Signs.ChestShopSign.*;
|
2012-06-08 15:28:36 +02:00
|
|
|
import static org.bukkit.event.block.Action.LEFT_CLICK_BLOCK;
|
|
|
|
import static org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class PlayerInteract implements Listener {
|
2012-12-26 21:42:22 +01:00
|
|
|
|
2014-03-20 16:57:48 +01:00
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
2018-05-07 16:32:51 +02:00
|
|
|
public static void onInteract(PlayerInteractEvent event) {
|
2012-12-26 21:42:22 +01:00
|
|
|
Block block = event.getClickedBlock();
|
2016-10-13 18:11:23 +02:00
|
|
|
if (block == null)
|
2012-12-26 21:42:22 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
Action action = event.getAction();
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
|
2018-10-23 20:04:18 +02:00
|
|
|
if (Properties.USE_BUILT_IN_PROTECTION && isShopBlock(block)) {
|
2012-12-26 21:42:22 +01:00
|
|
|
if (Properties.TURN_OFF_DEFAULT_PROTECTION_WHEN_PROTECTED_EXTERNALLY) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-19 21:10:48 +01:00
|
|
|
if (!ChestShop.canAccess(player, block)) {
|
2012-12-26 21:42:22 +01:00
|
|
|
player.sendMessage(Messages.prefix(Messages.ACCESS_DENIED));
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-19 21:10:48 +01:00
|
|
|
if (!isSign(block) || player.getInventory().getItemInMainHand().getType() == Material.SIGN) // Blocking accidental sign edition
|
2012-12-26 21:42:22 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
Sign sign = (Sign) block.getState();
|
|
|
|
if (!ChestShopSign.isValid(sign)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-07 02:11:59 +01:00
|
|
|
if (Properties.ALLOW_AUTO_ITEM_FILL && ChatColor.stripColor(sign.getLine(ITEM_LINE)).equals(AUTOFILL_CODE)) {
|
2018-11-19 21:10:48 +01:00
|
|
|
if (ChestShopSign.hasPermission(player, OTHER_NAME_CREATE, sign)) {
|
2018-01-07 02:11:59 +01:00
|
|
|
ItemStack item = player.getInventory().getItemInMainHand();
|
2018-04-02 18:41:45 +02:00
|
|
|
if (!MaterialUtil.isEmpty(item)) {
|
2018-01-07 02:11:59 +01:00
|
|
|
String itemCode = MaterialUtil.getSignName(item);
|
|
|
|
String[] lines = sign.getLines();
|
|
|
|
lines[ITEM_LINE] = itemCode;
|
2018-05-07 16:32:51 +02:00
|
|
|
|
2018-01-07 02:11:59 +01:00
|
|
|
SignChangeEvent changeEvent = new SignChangeEvent(block, player, lines);
|
|
|
|
com.Acrobot.ChestShop.ChestShop.callEvent(changeEvent);
|
|
|
|
if (!changeEvent.isCancelled()) {
|
|
|
|
for (byte i = 0; i < changeEvent.getLines().length; ++i) {
|
|
|
|
sign.setLine(i, changeEvent.getLine(i));
|
|
|
|
}
|
|
|
|
sign.update();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
player.sendMessage(Messages.prefix(Messages.NO_ITEM_IN_HAND));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
player.sendMessage(Messages.prefix(Messages.ACCESS_DENIED));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2018-05-07 16:32:51 +02:00
|
|
|
|
2019-03-07 17:33:47 +01:00
|
|
|
|
2018-11-19 21:10:48 +01:00
|
|
|
if (ChestShopSign.hasPermission(player, OTHER_NAME_ACCESS, sign) && !ChestShopSign.isAdminShop(sign)) {
|
2019-03-07 17:33:47 +01:00
|
|
|
if (Properties.ALLOW_SIGN_CHEST_OPEN && !(Properties.IGNORE_CREATIVE_MODE && player.getGameMode() == GameMode.CREATIVE)) {
|
2018-11-19 21:10:48 +01:00
|
|
|
if (player.isSneaking() || player.isInsideVehicle()
|
2019-03-07 17:33:47 +01:00
|
|
|
|| (Properties.ALLOW_LEFT_CLICK_DESTROYING && action == LEFT_CLICK_BLOCK && ChestShopSign.hasPermission(player, OTHER_NAME_DESTROY, sign))) {
|
2018-11-19 21:10:48 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-12-26 21:42:22 +01:00
|
|
|
event.setCancelled(true);
|
2018-11-19 21:10:48 +01:00
|
|
|
showChestGUI(player, block, sign);
|
|
|
|
return;
|
2019-03-07 17:33:47 +01:00
|
|
|
}
|
|
|
|
if (Properties.IGNORE_ACCESS_PERMS || ChestShopSign.isOwner(player, sign)) {
|
2018-11-19 21:10:48 +01:00
|
|
|
// don't allow owners or people with access to buy/sell at this shop
|
|
|
|
return;
|
2012-12-26 21:42:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == RIGHT_CLICK_BLOCK) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:11:23 +02:00
|
|
|
//Bukkit.getLogger().info("ChestShop - DEBUG - "+block.getWorld().getName()+": "+block.getLocation().getBlockX()+", "+block.getLocation().getBlockY()+", "+block.getLocation().getBlockZ());
|
2012-12-26 21:42:22 +01:00
|
|
|
PreTransactionEvent pEvent = preparePreTransactionEvent(sign, player, action);
|
2016-10-13 18:11:23 +02:00
|
|
|
if (pEvent == null)
|
2013-01-29 21:01:30 +01:00
|
|
|
return;
|
|
|
|
|
2012-12-26 21:42:22 +01:00
|
|
|
Bukkit.getPluginManager().callEvent(pEvent);
|
2016-10-13 18:11:23 +02:00
|
|
|
if (pEvent.isCancelled())
|
2012-12-26 21:42:22 +01:00
|
|
|
return;
|
|
|
|
|
2012-08-10 19:01:34 +02:00
|
|
|
TransactionEvent tEvent = new TransactionEvent(pEvent, sign);
|
|
|
|
Bukkit.getPluginManager().callEvent(tEvent);
|
|
|
|
}
|
2012-06-08 15:28:36 +02:00
|
|
|
|
2012-08-10 19:01:34 +02:00
|
|
|
private static PreTransactionEvent preparePreTransactionEvent(Sign sign, Player player, Action action) {
|
2013-02-11 16:55:04 +01:00
|
|
|
String name = sign.getLine(NAME_LINE);
|
|
|
|
String quantity = sign.getLine(QUANTITY_LINE);
|
|
|
|
String prices = sign.getLine(PRICE_LINE);
|
|
|
|
String material = sign.getLine(ITEM_LINE);
|
2012-06-08 15:28:36 +02:00
|
|
|
|
2018-01-27 17:12:34 +01:00
|
|
|
Account account = NameManager.getLastAccountFromShortName(name);
|
|
|
|
if (account == null) {
|
|
|
|
player.sendMessage(Messages.prefix(Messages.PLAYER_NOT_FOUND));
|
2016-09-20 21:45:18 +02:00
|
|
|
return null;
|
2018-01-27 17:12:34 +01:00
|
|
|
}
|
2014-04-12 17:27:16 +02:00
|
|
|
|
2017-07-26 15:11:46 +02:00
|
|
|
boolean adminShop = ChestShopSign.isAdminShop(sign);
|
|
|
|
|
2016-10-13 18:11:23 +02:00
|
|
|
// check if player exists in economy
|
2018-07-11 19:09:07 +02:00
|
|
|
if (!adminShop) {
|
|
|
|
AccountCheckEvent event = new AccountCheckEvent(account.getUuid(), player.getWorld());
|
|
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
if(!event.hasAccount()) {
|
|
|
|
player.sendMessage(Messages.prefix(Messages.NO_ECONOMY_ACCOUNT));
|
|
|
|
return null;
|
|
|
|
}
|
2018-01-27 17:12:34 +01:00
|
|
|
}
|
2016-10-13 18:11:23 +02:00
|
|
|
|
2012-11-23 21:04:13 +01:00
|
|
|
Action buy = Properties.REVERSE_BUTTONS ? LEFT_CLICK_BLOCK : RIGHT_CLICK_BLOCK;
|
2013-02-11 16:55:04 +01:00
|
|
|
double price = (action == buy ? PriceUtil.getBuyPrice(prices) : PriceUtil.getSellPrice(prices));
|
2012-07-07 19:24:06 +02:00
|
|
|
|
2018-10-23 20:04:18 +02:00
|
|
|
Container shopBlock = uBlock.findConnectedContainer(sign);
|
|
|
|
Inventory ownerInventory = (adminShop ? new AdminInventory() : shopBlock != null ? shopBlock.getInventory() : null);
|
2012-07-07 19:24:06 +02:00
|
|
|
|
2019-01-17 19:50:47 +01:00
|
|
|
ItemParseEvent parseEvent = new ItemParseEvent(material);
|
|
|
|
Bukkit.getPluginManager().callEvent(parseEvent);
|
|
|
|
ItemStack item = parseEvent.getItem();
|
2013-02-11 16:55:04 +01:00
|
|
|
if (item == null || !NumberUtil.isInteger(quantity)) {
|
2013-01-29 21:01:30 +01:00
|
|
|
player.sendMessage(Messages.prefix(Messages.INVALID_SHOP_DETECTED));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-02-11 16:55:04 +01:00
|
|
|
int amount = Integer.parseInt(quantity);
|
2012-09-12 12:35:48 +02:00
|
|
|
|
2012-10-16 17:03:45 +02:00
|
|
|
if (amount < 1) {
|
|
|
|
amount = 1;
|
|
|
|
}
|
|
|
|
|
2013-04-05 14:17:34 +02:00
|
|
|
if (Properties.SHIFT_SELLS_IN_STACKS && player.isSneaking() && price != PriceUtil.NO_PRICE && isAllowedForShift(action == buy)) {
|
|
|
|
int newAmount = getStackAmount(item, ownerInventory, player, action);
|
2012-09-12 12:35:48 +02:00
|
|
|
if (newAmount > 0) {
|
|
|
|
price = (price / amount) * newAmount;
|
|
|
|
amount = newAmount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-10 19:01:34 +02:00
|
|
|
item.setAmount(amount);
|
2012-06-08 15:28:36 +02:00
|
|
|
|
2017-09-06 15:19:34 +02:00
|
|
|
ItemStack[] items = InventoryUtil.getItemsStacked(item);
|
2012-06-08 15:28:36 +02:00
|
|
|
|
2012-08-10 19:01:34 +02:00
|
|
|
TransactionType transactionType = (action == buy ? BUY : SELL);
|
2017-10-29 23:50:24 +01:00
|
|
|
return new PreTransactionEvent(ownerInventory, player.getInventory(), items, price, player, account, sign, transactionType);
|
2012-06-08 15:28:36 +02:00
|
|
|
}
|
|
|
|
|
2013-03-30 15:58:06 +01:00
|
|
|
private static boolean isAllowedForShift(boolean buyTransaction) {
|
|
|
|
String allowed = Properties.SHIFT_ALLOWS;
|
|
|
|
|
|
|
|
if (allowed.equalsIgnoreCase("ALL")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return allowed.equalsIgnoreCase(buyTransaction ? "BUY" : "SELL");
|
|
|
|
}
|
|
|
|
|
2013-04-05 14:17:34 +02:00
|
|
|
private static int getStackAmount(ItemStack item, Inventory inventory, Player player, Action action) {
|
2012-11-23 21:04:13 +01:00
|
|
|
Action buy = Properties.REVERSE_BUTTONS ? LEFT_CLICK_BLOCK : RIGHT_CLICK_BLOCK;
|
2013-04-05 14:17:34 +02:00
|
|
|
Inventory checkedInventory = (action == buy ? inventory : player.getInventory());
|
2012-09-12 12:35:48 +02:00
|
|
|
|
2017-09-06 15:19:34 +02:00
|
|
|
if (checkedInventory.containsAtLeast(item, InventoryUtil.getMaxStackSize(item))) {
|
|
|
|
return InventoryUtil.getMaxStackSize(item);
|
2012-09-12 12:35:48 +02:00
|
|
|
} else {
|
2013-04-05 14:17:34 +02:00
|
|
|
return InventoryUtil.getAmount(item, checkedInventory);
|
2012-09-12 12:35:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 21:10:48 +01:00
|
|
|
/**
|
|
|
|
* @deprecated Use {@link ChestShopSign#hasPermission(Player, Permission, Sign)} with {@link Permission#OTHER_NAME_ACCESS}
|
|
|
|
*/
|
|
|
|
@Deprecated
|
2012-09-12 12:35:48 +02:00
|
|
|
public static boolean canOpenOtherShops(Player player) {
|
2018-11-19 21:10:48 +01:00
|
|
|
return Permission.has(player, Permission.OTHER_NAME_ACCESS + ".*");
|
2012-06-08 15:28:36 +02:00
|
|
|
}
|
|
|
|
|
2018-11-19 21:10:48 +01:00
|
|
|
private static void showChestGUI(Player player, Block signBlock, Sign sign) {
|
|
|
|
Container container = uBlock.findConnectedContainer(sign);
|
2012-06-08 15:28:36 +02:00
|
|
|
|
2018-10-23 20:04:18 +02:00
|
|
|
if (container == null) {
|
2012-11-23 21:04:13 +01:00
|
|
|
player.sendMessage(Messages.prefix(Messages.NO_CHEST_DETECTED));
|
2012-06-08 15:28:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-19 21:10:48 +01:00
|
|
|
if (!Security.canAccess(player, signBlock)) {
|
2012-06-08 15:28:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-23 20:04:18 +02:00
|
|
|
BlockUtil.openBlockGUI(container, player);
|
2012-06-08 15:28:36 +02:00
|
|
|
}
|
|
|
|
}
|