Skip to content

Commit a7dfa69

Browse files
committed
improve POS player interact event handler
1 parent 0c4e58d commit a7dfa69

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

src/main/java/pro/cloudnode/smp/bankaccounts/events/PlayerInteract.java

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.bukkit.block.Block;
44
import org.bukkit.block.Chest;
55
import org.bukkit.entity.Player;
6+
import org.bukkit.event.Event;
67
import org.bukkit.event.EventHandler;
78
import org.bukkit.event.Listener;
89
import org.bukkit.event.block.Action;
@@ -19,38 +20,42 @@
1920

2021
public final class PlayerInteract implements Listener {
2122
@EventHandler
22-
public void onPlayerInteractEvent(final @NotNull PlayerInteractEvent event) {
23+
public void openPOS(final @NotNull PlayerInteractEvent event) {
2324
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;
5357
}
58+
POS.openBuyGui(player, chest, pos.get(), account.get());
5459
}
5560
}
5661
}

0 commit comments

Comments
 (0)