From b177e9b095f1cea43eab6e9c966b3cf81ae2d1fa Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Fri, 9 Aug 2019 11:03:23 -0500 Subject: [PATCH] optimize voucher check + only check permissions for valid voucher items --- .../listeners/PlayerInteractListener.java | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/songoda/epicvouchers/listeners/PlayerInteractListener.java b/src/main/java/com/songoda/epicvouchers/listeners/PlayerInteractListener.java index e457f3d..28d2170 100644 --- a/src/main/java/com/songoda/epicvouchers/listeners/PlayerInteractListener.java +++ b/src/main/java/com/songoda/epicvouchers/listeners/PlayerInteractListener.java @@ -23,38 +23,36 @@ public class PlayerInteractListener implements Listener { @EventHandler public void voucherListener(PlayerInteractEvent event) { - if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK || !event.hasItem()) { + final ItemStack item = event.getItem(); + if (item == null || (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK)) return; - } + final Player player = event.getPlayer(); for (Voucher voucher : instance.getVouchers().values()) { - Player player = event.getPlayer(); - if (!player.hasPermission(voucher.getPermission()) && !voucher.getPermission().isEmpty()) { + final ItemStack voucherItem = voucher.getItemStack(); + + // does the item they're holding match this voucher? + if (voucherItem != null && !voucher.getItemStack().isSimilar(item)) continue; + else if (item.getType() != voucher.getMaterial() || item.getDurability() != voucher.getData()) + continue; + else { + // material matches - verify the name + lore + final ItemMeta meta = item.getItemMeta(); + if (meta == null || !meta.hasDisplayName() || !meta.hasLore() || !meta.getDisplayName().equals(voucher.getName(true)) || !meta.getLore().equals(voucher.getLore(true))) + continue; } - ItemStack item = event.getItem(); + event.setCancelled(true); - if (voucher.getItemStack() != null) { - if (!voucher.getItemStack().isSimilar(item)) { - continue; - } - } else { - if (item.getType() != voucher.getMaterial() || item.getDurability() != voucher.getData()) { - continue; - } - - ItemMeta meta = item.getItemMeta(); - - if (!item.hasItemMeta() || !meta.hasDisplayName() || !meta.getDisplayName().equals(voucher.getName(true)) || !meta.getLore().equals(voucher.getLore(true))) { - continue; - } + // does the player have permission to redeem this voucher? + if (!voucher.getPermission().isEmpty() && !player.hasPermission(voucher.getPermission())) { + // todo: probably should send a message to the player... + return; } UUID uuid = player.getUniqueId(); - event.setCancelled(true); - if (instance.getCoolDowns().isOnCoolDown(uuid)) { instance.getLocale().getMessage("event.general.cooldown") .processPlaceholder("time", instance.getCoolDowns().getTime(uuid))