11package pro .cloudnode .smp .bankaccounts .commands ;
22
33import org .bukkit .block .Block ;
4+ import org .bukkit .block .BlockFace ;
45import org .bukkit .block .BlockState ;
56import org .bukkit .block .Chest ;
67import org .bukkit .command .CommandSender ;
78import org .bukkit .entity .Player ;
9+ import org .bukkit .event .Event ;
10+ import org .bukkit .event .block .Action ;
11+ import org .bukkit .event .player .PlayerInteractEvent ;
812import org .bukkit .inventory .DoubleChestInventory ;
913import org .jetbrains .annotations .NotNull ;
1014import org .jetbrains .annotations .Nullable ;
2125import java .util .ArrayList ;
2226import java .util .Arrays ;
2327import java .util .Date ;
28+ import java .util .List ;
2429import java .util .Optional ;
2530import java .util .Set ;
2631
@@ -73,6 +78,8 @@ public final class POSCommand extends Command {
7378 if (block .isEmpty ()) return new Message (sender , BankAccounts .getInstance ().config ().messagesErrorsAsyncFailed ());
7479 if (!(block .get () instanceof final @ NotNull Chest chest ))
7580 return new Message (sender , BankAccounts .getInstance ().config ().messagesErrorsPosNotChest ());
81+ if (!canOpenChest (player , chest ))
82+ return CommandResult .DO_NOTHING ;
7683 if (chest .getInventory () instanceof DoubleChestInventory )
7784 return new Message (sender , BankAccounts .getInstance ().config ().messagesErrorsPosDoubleChest ());
7885 if (chest .getInventory ().isEmpty ()) return new Message (sender , BankAccounts .getInstance ().config ().messagesErrorsPosEmpty ());
@@ -90,6 +97,31 @@ public final class POSCommand extends Command {
9097 return new Message (sender , BankAccounts .getInstance ().config ().messagesPosCreated (pos ));
9198 }
9299
100+ private static boolean canOpenChest (final @ NotNull Player player , final @ NotNull Chest chest ) {
101+ return BankAccounts .runOnMain (() -> {
102+ final @ NotNull Block block = chest .getBlock ();
103+
104+ final @ NotNull PlayerInteractEvent event = new PlayerInteractEvent (
105+ player ,
106+ Action .RIGHT_CLICK_BLOCK ,
107+ player .getInventory ().getItemInMainHand (),
108+ block ,
109+ getTargetedFace (player ).orElse (BlockFace .NORTH )
110+ );
111+
112+ BankAccounts .getInstance ().getServer ().getPluginManager ().callEvent (event );
113+ return event .useInteractedBlock () != Event .Result .DENY ;
114+ }).orElse (false );
115+ }
116+
117+ private static @ NotNull Optional <@ NotNull BlockFace > getTargetedFace (final @ NotNull Player player ) {
118+ final @ NotNull List <@ NotNull Block > lastTwoTargetBlocks = player .getLastTwoTargetBlocks (null , 5 );
119+ if (lastTwoTargetBlocks .size () != 2 || !lastTwoTargetBlocks .get (1 ).getType ().isOccluding ()) return Optional .empty ();
120+ final @ NotNull Block targetBlock = lastTwoTargetBlocks .get (1 );
121+ final @ NotNull Block adjacentBlock = lastTwoTargetBlocks .get (0 );
122+ return Optional .ofNullable (targetBlock .getFace (adjacentBlock ));
123+ }
124+
93125 @ Override
94126 public @ NotNull ArrayList <@ NotNull String > tab (final @ NotNull CommandSender sender , final @ NotNull String @ NotNull [] args ) {
95127 final @ NotNull ArrayList <@ NotNull String > suggestions = new ArrayList <>();
0 commit comments