|
3 | 3 | import org.bukkit.block.Block; |
4 | 4 | import org.bukkit.block.Chest; |
5 | 5 | import org.bukkit.entity.Player; |
| 6 | +import org.bukkit.event.Event; |
6 | 7 | import org.bukkit.event.EventHandler; |
7 | 8 | import org.bukkit.event.Listener; |
8 | 9 | import org.bukkit.event.block.Action; |
|
19 | 20 |
|
20 | 21 | public final class PlayerInteract implements Listener { |
21 | 22 | @EventHandler |
22 | | - public void onPlayerInteractEvent(final @NotNull PlayerInteractEvent event) { |
| 23 | + public void openPOS(final @NotNull PlayerInteractEvent event) { |
23 | 24 | final @NotNull Player player = event.getPlayer(); |
24 | | - if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { |
25 | | - final @NotNull Optional<Block> block = Optional.ofNullable(event.getClickedBlock()); |
26 | | - if (block.isPresent() && block.get().getState() instanceof final @NotNull Chest chest && !chest.getInventory().isEmpty() && !(chest.getInventory() instanceof DoubleChestInventory)) { |
27 | | - final @NotNull Optional<POS> pos = POS.get(block.get()); |
28 | | - if (pos.isEmpty()) return; |
29 | | - event.setCancelled(true); |
30 | | - if (player.getUniqueId().equals(pos.get().seller.owner.getUniqueId())) { |
31 | | - POS.openOwnerGui(player, chest, pos.get()); |
32 | | - return; |
33 | | - } |
34 | | - if (!player.hasPermission(Permissions.POS_USE)) { |
35 | | - player.sendMessage(BankAccounts.getInstance().config().messagesErrorsPosNoPermission()); |
36 | | - return; |
37 | | - } |
38 | | - final @NotNull ItemStack heldItem = player.getInventory().getItemInMainHand(); |
39 | | - if (heldItem.getType() != BankAccounts.getInstance().config().instrumentsMaterial()) { |
40 | | - player.sendMessage(BankAccounts.getInstance().config().messagesErrorsNoCard()); |
41 | | - return; |
42 | | - } |
43 | | - final @NotNull Optional<@NotNull Account> account = Account.get(heldItem); |
44 | | - if (account.isEmpty()) |
45 | | - player.sendMessage(BankAccounts.getInstance().config().messagesErrorsPosInvalidCard()); |
46 | | - else { |
47 | | - if (!player.hasPermission(Permissions.POS_USE_OTHER) && !account.get().owner.getUniqueId().equals(player.getUniqueId())) { |
48 | | - player.sendMessage(BankAccounts.getInstance().config().messagesErrorsNotAccountOwner()); |
49 | | - return; |
50 | | - } |
51 | | - POS.openBuyGui(player, chest, pos.get(), account.get()); |
52 | | - } |
| 25 | + if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; |
| 26 | + final @NotNull Optional<Block> block = Optional.ofNullable(event.getClickedBlock()); |
| 27 | + if (block.isEmpty()) return; |
| 28 | + if (!(block.get().getState() instanceof final @NotNull Chest chest)) return; |
| 29 | + if (chest.getInventory().isEmpty()) return; |
| 30 | + if (chest.getInventory() instanceof DoubleChestInventory) return; |
| 31 | + |
| 32 | + final @NotNull Optional<POS> pos = POS.get(block.get()); |
| 33 | + if (pos.isEmpty()) return; |
| 34 | + |
| 35 | + event.setUseInteractedBlock(Event.Result.DENY); |
| 36 | + |
| 37 | + if (player.getUniqueId().equals(pos.get().seller.owner.getUniqueId())) { |
| 38 | + POS.openOwnerGui(player, chest, pos.get()); |
| 39 | + return; |
| 40 | + } |
| 41 | + if (!player.hasPermission(Permissions.POS_USE)) { |
| 42 | + player.sendMessage(BankAccounts.getInstance().config().messagesErrorsPosNoPermission()); |
| 43 | + return; |
| 44 | + } |
| 45 | + final @NotNull ItemStack heldItem = player.getInventory().getItemInMainHand(); |
| 46 | + if (heldItem.getType() != BankAccounts.getInstance().config().instrumentsMaterial()) { |
| 47 | + player.sendMessage(BankAccounts.getInstance().config().messagesErrorsNoCard()); |
| 48 | + return; |
| 49 | + } |
| 50 | + final @NotNull Optional<@NotNull Account> account = Account.get(heldItem); |
| 51 | + if (account.isEmpty()) player.sendMessage(BankAccounts.getInstance().config().messagesErrorsPosInvalidCard()); |
| 52 | + else { |
| 53 | + if (!player.hasPermission(Permissions.POS_USE_OTHER) && !account.get().owner.getUniqueId() |
| 54 | + .equals(player.getUniqueId())) { |
| 55 | + player.sendMessage(BankAccounts.getInstance().config().messagesErrorsNotAccountOwner()); |
| 56 | + return; |
53 | 57 | } |
| 58 | + POS.openBuyGui(player, chest, pos.get(), account.get()); |
54 | 59 | } |
55 | 60 | } |
56 | 61 | } |
0 commit comments