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;
|
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;
|
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
2012-08-10 19:01:34 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uName;
|
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
|
|
|
|
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
|
|
|
|
2012-12-31 02:34:16 +01:00
|
|
|
@EventHandler(priority = EventPriority.HIGHEST)
|
2012-12-26 21:42:22 +01:00
|
|
|
public static void onInteract(PlayerInteractEvent event) {
|
|
|
|
Block block = event.getClickedBlock();
|
|
|
|
|
|
|
|
if (block == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Action action = event.getAction();
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
|
|
|
|
if (isChest(block) && Properties.USE_BUILT_IN_PROTECTION) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isSign(block) || player.getItemInHand().getType() == Material.SIGN) { // Blocking accidental sign edition
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sign sign = (Sign) block.getState();
|
|
|
|
|
|
|
|
if (!ChestShopSign.isValid(sign)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ChestShopSign.canAccess(player, sign)) {
|
2013-02-10 15:29:40 +01:00
|
|
|
if (!Properties.ALLOW_SIGN_CHEST_OPEN || player.isSneaking() || 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
PreTransactionEvent pEvent = preparePreTransactionEvent(sign, player, action);
|
2013-01-29 21:01:30 +01:00
|
|
|
|
|
|
|
if (pEvent == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-26 21:42:22 +01:00
|
|
|
Bukkit.getPluginManager().callEvent(pEvent);
|
|
|
|
|
|
|
|
if (pEvent.isCancelled()) {
|
|
|
|
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
|
|
|
|
2013-02-11 16:55:04 +01:00
|
|
|
String ownerName = uName.getName(name);
|
|
|
|
OfflinePlayer owner = Bukkit.getOfflinePlayer(ownerName);
|
2012-06-08 15:28:36 +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
|
|
|
|
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);
|
2012-06-08 15:28:36 +02:00
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|