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;
|
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;
|
2016-10-13 18:11:23 +02:00
|
|
|
import com.Acrobot.ChestShop.Listeners.Economy.Plugins.VaultListener;
|
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;
|
2013-02-10 15:29:40 +01:00
|
|
|
import org.bukkit.GameMode;
|
2012-06-08 15:28:36 +02:00
|
|
|
import org.bukkit.Material;
|
2012-08-10 19:01:34 +02:00
|
|
|
import org.bukkit.OfflinePlayer;
|
2012-06-08 15:28:36 +02:00
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.block.Chest;
|
|
|
|
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;
|
|
|
|
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
|
|
|
|
2014-04-12 17:27:16 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2013-01-24 22:35:28 +01:00
|
|
|
import static com.Acrobot.Breeze.Utils.BlockUtil.isChest;
|
|
|
|
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;
|
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)
|
2016-10-13 18:11:23 +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();
|
|
|
|
|
2013-08-05 02:05:43 +02:00
|
|
|
if (Properties.USE_BUILT_IN_PROTECTION && isChest(block)) {
|
2012-12-26 21:42:22 +01:00
|
|
|
if (Properties.TURN_OFF_DEFAULT_PROTECTION_WHEN_PROTECTED_EXTERNALLY) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!canOpenOtherShops(player) && !ChestShop.canAccess(player, block)) {
|
|
|
|
player.sendMessage(Messages.prefix(Messages.ACCESS_DENIED));
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:11:23 +02:00
|
|
|
if (!isSign(block) || player.getItemInHand().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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ChestShopSign.canAccess(player, sign)) {
|
2013-07-13 23:14:23 +02:00
|
|
|
if (!Properties.ALLOW_SIGN_CHEST_OPEN || player.isSneaking() || player.isInsideVehicle() || player.getGameMode() == GameMode.CREATIVE) {
|
2012-12-26 21:42:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Properties.ALLOW_LEFT_CLICK_DESTROYING || action != LEFT_CLICK_BLOCK) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
showChestGUI(player, block);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-07-01 18:14:41 +02:00
|
|
|
Account account = NameManager.getAccountFromShortName(name);
|
|
|
|
if (account == null)
|
2016-09-20 21:45:18 +02:00
|
|
|
return null;
|
2014-04-12 17:27:16 +02:00
|
|
|
|
2017-07-01 18:14:41 +02:00
|
|
|
OfflinePlayer owner = Bukkit.getOfflinePlayer(account.getUuid());
|
2012-06-08 15:28:36 +02:00
|
|
|
|
2016-10-13 18:11:23 +02:00
|
|
|
// check if player exists in economy
|
2016-11-10 03:17:27 +01:00
|
|
|
if(!ChestShopSign.isAdminShop(sign) && (owner == null || owner.getName() == null || !VaultListener.getProvider().hasAccount(owner)))
|
2016-10-13 18:11:23 +02:00
|
|
|
return null;
|
|
|
|
|
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
|
|
|
|
2012-08-10 19:01:34 +02:00
|
|
|
Chest chest = uBlock.findConnectedChest(sign);
|
|
|
|
Inventory ownerInventory = (ChestShopSign.isAdminShop(sign) ? new AdminInventory() : chest != null ? chest.getInventory() : null);
|
2012-07-07 19:24:06 +02:00
|
|
|
|
2013-02-11 16:55:04 +01:00
|
|
|
ItemStack item = MaterialUtil.getItem(material);
|
|
|
|
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
|
|
|
|
2012-08-10 19:01:34 +02:00
|
|
|
ItemStack[] items = {item};
|
2012-06-08 15:28:36 +02:00
|
|
|
|
2012-08-10 19:01:34 +02:00
|
|
|
TransactionType transactionType = (action == buy ? BUY : SELL);
|
|
|
|
return new PreTransactionEvent(ownerInventory, player.getInventory(), items, price, player, owner, 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
|
|
|
|
2013-04-05 14:17:34 +02:00
|
|
|
if (checkedInventory.containsAtLeast(item, item.getMaxStackSize())) {
|
|
|
|
return item.getMaxStackSize();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canOpenOtherShops(Player player) {
|
2012-06-08 15:28:36 +02:00
|
|
|
return Permission.has(player, Permission.ADMIN) || Permission.has(player, Permission.MOD);
|
|
|
|
}
|
|
|
|
|
2012-12-26 21:42:22 +01:00
|
|
|
private static void showChestGUI(Player player, Block signBlock) {
|
|
|
|
Chest chest = uBlock.findConnectedChest(signBlock);
|
2012-06-08 15:28:36 +02:00
|
|
|
|
|
|
|
if (chest == 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;
|
|
|
|
}
|
|
|
|
|
2012-12-26 21:42:22 +01:00
|
|
|
if (!canOpenOtherShops(player) && !Security.canAccess(player, signBlock)) {
|
2012-06-08 15:28:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-31 02:34:16 +01:00
|
|
|
BlockUtil.openBlockGUI(chest, player);
|
2012-06-08 15:28:36 +02:00
|
|
|
}
|
|
|
|
}
|