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 ;
1923import java .util .ArrayList ;
2024import java .util .Arrays ;
2125import java .util .Date ;
26+ import java .util .List ;
2227import java .util .Optional ;
2328import java .util .Set ;
2429
@@ -71,6 +76,8 @@ public boolean execute(final @NotNull CommandSender sender, final @NotNull Strin
7176 if (block .isEmpty ()) return sendMessage (sender , BankAccounts .getInstance ().config ().messagesErrorsAsyncFailed ());
7277 if (!(block .get () instanceof final @ NotNull Chest chest ))
7378 return sendMessage (sender , BankAccounts .getInstance ().config ().messagesErrorsPosNotChest ());
79+ if (!canOpenChest (player , chest ))
80+ return true ;
7481 if (chest .getInventory () instanceof DoubleChestInventory )
7582 return sendMessage (sender , BankAccounts .getInstance ().config ().messagesErrorsPosDoubleChest ());
7683 if (chest .getInventory ().isEmpty ()) return sendMessage (sender , BankAccounts .getInstance ().config ().messagesErrorsPosEmpty ());
@@ -88,6 +95,31 @@ public boolean execute(final @NotNull CommandSender sender, final @NotNull Strin
8895 return sendMessage (sender , BankAccounts .getInstance ().config ().messagesPosCreated (pos ));
8996 }
9097
98+ private static boolean canOpenChest (final @ NotNull Player player , final @ NotNull Chest chest ) {
99+ return BankAccounts .runOnMain (() -> {
100+ final @ NotNull Block block = chest .getBlock ();
101+
102+ final @ NotNull PlayerInteractEvent event = new PlayerInteractEvent (
103+ player ,
104+ Action .RIGHT_CLICK_BLOCK ,
105+ player .getInventory ().getItemInMainHand (),
106+ block ,
107+ getTargetedFace (player ).orElse (BlockFace .NORTH )
108+ );
109+
110+ BankAccounts .getInstance ().getServer ().getPluginManager ().callEvent (event );
111+ return event .useInteractedBlock () != Event .Result .DENY ;
112+ }).orElse (false );
113+ }
114+
115+ private static @ NotNull Optional <@ NotNull BlockFace > getTargetedFace (final @ NotNull Player player ) {
116+ final @ NotNull List <@ NotNull Block > lastTwoTargetBlocks = player .getLastTwoTargetBlocks (null , 5 );
117+ if (lastTwoTargetBlocks .size () != 2 || !lastTwoTargetBlocks .get (1 ).getType ().isOccluding ()) return Optional .empty ();
118+ final @ NotNull Block targetBlock = lastTwoTargetBlocks .get (1 );
119+ final @ NotNull Block adjacentBlock = lastTwoTargetBlocks .get (0 );
120+ return Optional .ofNullable (targetBlock .getFace (adjacentBlock ));
121+ }
122+
91123 @ Override
92124 public @ NotNull ArrayList <@ NotNull String > tab (final @ NotNull CommandSender sender , final @ NotNull String @ NotNull [] args ) {
93125 final @ NotNull ArrayList <@ NotNull String > suggestions = new ArrayList <>();
0 commit comments