mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2024-11-23 18:45:31 +01:00
PlayerInteract now uses Pre/TransactionEvent
This commit is contained in:
parent
3a1b80d4ef
commit
f096e8eefd
@ -1,20 +1,22 @@
|
|||||||
package com.Acrobot.ChestShop.Listeners.Player;
|
package com.Acrobot.ChestShop.Listeners.Player;
|
||||||
|
|
||||||
import com.Acrobot.Breeze.Utils.BlockUtil;
|
import com.Acrobot.Breeze.Utils.BlockUtil;
|
||||||
|
import com.Acrobot.Breeze.Utils.MaterialUtil;
|
||||||
|
import com.Acrobot.Breeze.Utils.PriceUtil;
|
||||||
import com.Acrobot.ChestShop.Config.Config;
|
import com.Acrobot.ChestShop.Config.Config;
|
||||||
import com.Acrobot.ChestShop.Config.Language;
|
import com.Acrobot.ChestShop.Config.Language;
|
||||||
|
import com.Acrobot.ChestShop.Containers.AdminInventory;
|
||||||
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
|
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
|
||||||
|
import com.Acrobot.ChestShop.Events.TransactionEvent;
|
||||||
import com.Acrobot.ChestShop.Permission;
|
import com.Acrobot.ChestShop.Permission;
|
||||||
import com.Acrobot.ChestShop.Plugins.ChestShop;
|
import com.Acrobot.ChestShop.Plugins.ChestShop;
|
||||||
import com.Acrobot.ChestShop.Security;
|
import com.Acrobot.ChestShop.Security;
|
||||||
import com.Acrobot.ChestShop.Shop.Shop;
|
|
||||||
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
||||||
import com.Acrobot.ChestShop.Signs.RestrictedSign;
|
|
||||||
import com.Acrobot.ChestShop.Utils.uBlock;
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
||||||
|
import com.Acrobot.ChestShop.Utils.uName;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
import org.bukkit.GameMode;
|
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.Chest;
|
import org.bukkit.block.Chest;
|
||||||
import org.bukkit.block.Sign;
|
import org.bukkit.block.Sign;
|
||||||
@ -25,15 +27,14 @@ import org.bukkit.event.Listener;
|
|||||||
import org.bukkit.event.block.Action;
|
import org.bukkit.event.block.Action;
|
||||||
import org.bukkit.event.player.PlayerInteractEvent;
|
import org.bukkit.event.player.PlayerInteractEvent;
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import static com.Acrobot.ChestShop.Config.Language.ACCESS_DENIED;
|
import static com.Acrobot.ChestShop.Config.Language.ACCESS_DENIED;
|
||||||
import static com.Acrobot.ChestShop.Config.Property.*;
|
import static com.Acrobot.ChestShop.Config.Property.*;
|
||||||
import static com.Acrobot.ChestShop.Events.TransactionEvent.Type.BUY;
|
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType;
|
||||||
import static com.Acrobot.ChestShop.Events.TransactionEvent.Type.SELL;
|
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;
|
||||||
|
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.SELL;
|
||||||
|
import static com.Acrobot.ChestShop.Signs.ChestShopSign.*;
|
||||||
import static org.bukkit.event.block.Action.LEFT_CLICK_BLOCK;
|
import static org.bukkit.event.block.Action.LEFT_CLICK_BLOCK;
|
||||||
import static org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK;
|
import static org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK;
|
||||||
|
|
||||||
@ -41,45 +42,30 @@ import static org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK;
|
|||||||
* @author Acrobot
|
* @author Acrobot
|
||||||
*/
|
*/
|
||||||
public class PlayerInteract implements Listener {
|
public class PlayerInteract implements Listener {
|
||||||
private static final Map<UUID, Long> TIME_OF_THE_LATEST_CLICK = new HashMap<UUID, Long>();
|
|
||||||
private static final String ITEM_NOT_RECOGNISED = ChatColor.RED + "[Shop] The item is not recognised!";
|
|
||||||
|
|
||||||
private final int transactionBlockInterval;
|
|
||||||
|
|
||||||
public PlayerInteract(int transactionInterval) {
|
|
||||||
this.transactionBlockInterval = transactionInterval;
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.HIGHEST)
|
@EventHandler(priority = EventPriority.HIGHEST)
|
||||||
public void onPlayerInteract(PlayerInteractEvent event) {
|
public static void onPlayerInteract(PlayerInteractEvent event) {
|
||||||
Action action = event.getAction();
|
Block clickedBlock = event.getClickedBlock();
|
||||||
if (!playerClickedBlock(action)) {
|
|
||||||
|
if (clickedBlock == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Block block = event.getClickedBlock();
|
Action action = event.getAction();
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
|
|
||||||
if (Config.getBoolean(USE_BUILT_IN_PROTECTION) && block.getType() == Material.CHEST) {
|
if (Config.getBoolean(USE_BUILT_IN_PROTECTION) && clickedBlock.getType() == Material.CHEST) {
|
||||||
if (!canOpenOtherShops(player) && !ChestShop.canAccess(player, block)) {
|
if (!canOpenOtherShops(player) && !ChestShop.canAccess(player, clickedBlock)) {
|
||||||
player.sendMessage(Config.getLocal(ACCESS_DENIED));
|
player.sendMessage(Config.getLocal(ACCESS_DENIED));
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!BlockUtil.isSign(block)) return;
|
if (!BlockUtil.isSign(clickedBlock)) return;
|
||||||
Sign sign = (Sign) block.getState();
|
Sign sign = (Sign) clickedBlock.getState();
|
||||||
|
|
||||||
if (player.getItemInHand() != null && player.getItemInHand().getType() == Material.SIGN) return;
|
if (player.getItemInHand() != null && player.getItemInHand().getType() == Material.SIGN) return;
|
||||||
if (!ChestShopSign.isValid(sign) || !enoughTimeHasPassed(player) || player.isSneaking()) return;
|
if (!ChestShopSign.isValid(sign) || player.isSneaking()) return;
|
||||||
|
|
||||||
if (Config.getBoolean(IGNORE_CREATIVE_MODE) && player.getGameMode() == GameMode.CREATIVE) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
TIME_OF_THE_LATEST_CLICK.put(player.getUniqueId(), System.currentTimeMillis());
|
|
||||||
|
|
||||||
if (action == RIGHT_CLICK_BLOCK) {
|
if (action == RIGHT_CLICK_BLOCK) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
@ -91,50 +77,45 @@ public class PlayerInteract implements Listener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (action != LEFT_CLICK_BLOCK || !Config.getBoolean(ALLOW_LEFT_CLICK_DESTROYING)) {
|
if (action != LEFT_CLICK_BLOCK || !Config.getBoolean(ALLOW_LEFT_CLICK_DESTROYING)) {
|
||||||
showChestGUI(player, block);
|
showChestGUI(player, clickedBlock);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (RestrictedSign.isRestrictedShop(sign) && !RestrictedSign.canAccess(sign, player)) {
|
PreTransactionEvent pEvent = preparePreTransactionEvent(sign, player, action);
|
||||||
player.sendMessage(Config.getLocal(ACCESS_DENIED));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Action buy = (Config.getBoolean(REVERSE_BUTTONS) ? LEFT_CLICK_BLOCK : RIGHT_CLICK_BLOCK);
|
|
||||||
|
|
||||||
Shop shop = Shop.getShopFromSign(sign);
|
|
||||||
|
|
||||||
if (shop == null) {
|
|
||||||
player.sendMessage(ITEM_NOT_RECOGNISED);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
PreTransactionEvent pEvent = new PreTransactionEvent(shop, player, action == buy ? BUY : SELL);
|
|
||||||
Bukkit.getPluginManager().callEvent(pEvent);
|
Bukkit.getPluginManager().callEvent(pEvent);
|
||||||
|
|
||||||
if (pEvent.isCancelled()) {
|
if (pEvent.isCancelled()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action == buy) {
|
TransactionEvent tEvent = new TransactionEvent(pEvent, sign);
|
||||||
shop.sellToPlayer(player);
|
Bukkit.getPluginManager().callEvent(tEvent);
|
||||||
} else {
|
|
||||||
shop.buyFromPlayer(player);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean enoughTimeHasPassed(Player player) {
|
private static PreTransactionEvent preparePreTransactionEvent(Sign sign, Player player, Action action) {
|
||||||
UUID uniqueID = player.getUniqueId();
|
String ownerName = uName.getName(sign.getLine(NAME_LINE));
|
||||||
|
OfflinePlayer owner = Bukkit.getOfflinePlayer(ownerName);
|
||||||
|
|
||||||
return !TIME_OF_THE_LATEST_CLICK.containsKey(uniqueID) || (System.currentTimeMillis() - TIME_OF_THE_LATEST_CLICK.get(uniqueID)) >= transactionBlockInterval;
|
String priceLine = sign.getLine(PRICE_LINE);
|
||||||
|
|
||||||
|
Action buy = Config.getBoolean(REVERSE_BUTTONS) ? LEFT_CLICK_BLOCK : RIGHT_CLICK_BLOCK;
|
||||||
|
double price = (action == buy ? PriceUtil.getBuyPrice(priceLine) : PriceUtil.getSellPrice(priceLine));
|
||||||
|
|
||||||
|
Chest chest = uBlock.findConnectedChest(sign);
|
||||||
|
Inventory ownerInventory = (ChestShopSign.isAdminShop(sign) ? new AdminInventory() : chest != null ? chest.getInventory() : null);
|
||||||
|
|
||||||
|
ItemStack item = MaterialUtil.getItem(sign.getLine(ITEM_LINE));
|
||||||
|
|
||||||
|
int amount = Integer.parseInt(sign.getLine(QUANTITY_LINE));
|
||||||
|
item.setAmount(amount);
|
||||||
|
|
||||||
|
ItemStack[] items = {item};
|
||||||
|
|
||||||
|
TransactionType transactionType = (action == buy ? BUY : SELL);
|
||||||
|
return new PreTransactionEvent(ownerInventory, player.getInventory(), items, price, player, owner, sign, transactionType);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean playerClickedBlock(Action action) {
|
|
||||||
return action == LEFT_CLICK_BLOCK || action == RIGHT_CLICK_BLOCK;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static boolean canOpenOtherShops(Player player) {
|
private static boolean canOpenOtherShops(Player player) {
|
||||||
return Permission.has(player, Permission.ADMIN) || Permission.has(player, Permission.MOD);
|
return Permission.has(player, Permission.ADMIN) || Permission.has(player, Permission.MOD);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user