66import com .xinecraft .minetrax .common .responses .GenericApiResponse ;
77import com .xinecraft .minetrax .common .utils .MinetraxHttpUtil ;
88import de .themoep .minedown .adventure .MineDown ;
9+ import net .kyori .adventure .title .Title ;
910import org .bukkit .Bukkit ;
1011import org .bukkit .command .Command ;
1112import org .bukkit .command .CommandExecutor ;
1213import org .bukkit .command .CommandSender ;
1314import org .bukkit .entity .Player ;
1415import org .jetbrains .annotations .NotNull ;
1516
17+ import java .time .Duration ;
1618import java .util .List ;
19+ import java .util .concurrent .ConcurrentHashMap ;
1720
1821public class AccountLinkCommand implements CommandExecutor {
22+ private final boolean isConfirmationEnabled = MinetraxBukkit .getPlugin ().getIsPlayerLinkConfirmationEnabled ();
23+
1924 @ Override
2025 public boolean onCommand (@ NotNull CommandSender commandSender , @ NotNull Command command , @ NotNull String s , String [] strings ) {
2126 if (!(commandSender instanceof Player player )) {
@@ -25,9 +30,19 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
2530
2631 PlayerData playerData = MinetraxBukkit .getPlugin ().playersDataMap .get (player .getUniqueId ().toString ());
2732
28- List <String > withoutParamsMessage = playerData != null && playerData .is_verified ? MinetraxBukkit .getPlugin ().getPlayerLinkInitAlreadyLinkedMessage () : MinetraxBukkit .getPlugin ().getPlayerLinkInitMessage ();
33+ // Already linked
34+ if (playerData != null && playerData .is_verified ) {
35+ for (String line : MinetraxBukkit .getPlugin ().getPlayerLinkInitAlreadyLinkedMessage ()) {
36+ line = line .replace ("{WEB_URL}" , MinetraxBukkit .getPlugin ().getApiHost ());
37+ line = line .replace ("{LINK_URL}" , MinetraxHttpUtil .getUrl (MinetraxHttpUtil .ACCOUNT_LINK_ROUTE ));
38+ MinetraxBukkit .getPlugin ().adventure ().player (player ).sendMessage (MineDown .parse (line ));
39+ }
40+ return true ;
41+ }
42+
2943 // Send Init message if only /link
3044 if (strings .length == 0 ) {
45+ List <String > withoutParamsMessage = MinetraxBukkit .getPlugin ().getPlayerLinkInitMessage ();
3146 for (String line : withoutParamsMessage ) {
3247 line = line .replace ("{LINK_URL}" , MinetraxHttpUtil .getUrl (MinetraxHttpUtil .ACCOUNT_LINK_ROUTE ));
3348 line = line .replace ("{WEB_URL}" , MinetraxBukkit .getPlugin ().getApiHost ());
@@ -38,6 +53,49 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
3853
3954 // Send Linking message if /link <otp>
4055 String otpCode = strings [0 ];
56+ if (isConfirmationEnabled ) {
57+ ConcurrentHashMap <String , String > pendingVerifications = MinetraxBukkit .getPlugin ().getPlayerLinkPendingVerificationMap ();
58+ if (otpCode .equalsIgnoreCase ("confirm" )) {
59+ String pendingOtp = pendingVerifications .get (player .getUniqueId ().toString ());
60+ if (pendingOtp == null ) {
61+ MinetraxBukkit .getPlugin ().adventure ().player (player ).sendMessage (
62+ MineDown .parse ("&cNo OTP pending confirmation. Please enter your OTP first." )
63+ );
64+ return true ;
65+ }
66+ String processingMessage = MinetraxBukkit .getPlugin ().getProcessingMessage ();
67+ MinetraxBukkit .getPlugin ().adventure ().player (player ).sendMessage (MineDown .parse (processingMessage ));
68+ linkAccount (player , pendingOtp );
69+ pendingVerifications .remove (player .getUniqueId ().toString ());
70+ } else if (otpCode .equalsIgnoreCase ("deny" ) || otpCode .equalsIgnoreCase ("cancel" )) {
71+ pendingVerifications .remove (player .getUniqueId ().toString ());
72+ String cancelledMessage = MinetraxBukkit .getPlugin ().getCancelledMessage ();
73+ MinetraxBukkit .getPlugin ().adventure ().player (player ).sendMessage (MineDown .parse (cancelledMessage ));
74+ } else {
75+ pendingVerifications .put (player .getUniqueId ().toString (), otpCode );
76+
77+ String playerLinkConfirmationTitle = MinetraxBukkit .getPlugin ().getPlayerLinkConfirmationTitle ();
78+ String playerLinkConfirmationSubtitle = MinetraxBukkit .getPlugin ().getPlayerLinkConfirmationSubtitle ();
79+ if (!playerLinkConfirmationSubtitle .isBlank () || !playerLinkConfirmationTitle .isBlank ()) {
80+ final Title .Times times = Title .Times .times (Duration .ofMillis (500 ), Duration .ofMillis (6000 ), Duration .ofMillis (1000 ));
81+ final Title title = Title .title (MineDown .parse (playerLinkConfirmationTitle ), MineDown .parse (playerLinkConfirmationSubtitle ), times );
82+ MinetraxBukkit .getPlugin ().adventure ().player (player ).showTitle (title );
83+ }
84+
85+ for (String line : MinetraxBukkit .getPlugin ().getPlayerLinkConfirmationMessage ()) {
86+ line = line .replace ("{LINK_URL}" , MinetraxHttpUtil .getUrl (MinetraxHttpUtil .ACCOUNT_LINK_ROUTE ));
87+ line = line .replace ("{WEB_URL}" , MinetraxBukkit .getPlugin ().getApiHost ());
88+ MinetraxBukkit .getPlugin ().adventure ().player (player ).sendMessage (MineDown .parse (line ));
89+ }
90+ }
91+ } else {
92+ linkAccount (player , otpCode );
93+ }
94+
95+ return true ;
96+ }
97+
98+ private void linkAccount (Player player , String otpCode ) {
4199 Bukkit .getScheduler ().runTaskAsynchronously (MinetraxBukkit .getPlugin (), () -> {
42100 try {
43101 GenericApiResponse response = LinkAccount .link (
@@ -51,7 +109,6 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
51109 line = line .replace ("{ERROR_MESSAGE}" , response .getMessage ());
52110 MinetraxBukkit .getPlugin ().adventure ().player (player ).sendMessage (MineDown .parse (line ));
53111 }
54- return ;
55112 } else {
56113 for (String line : MinetraxBukkit .getPlugin ().getPlayerLinkSuccessMessage ()) {
57114 line = line .replace ("{LINK_URL}" , MinetraxHttpUtil .getUrl (MinetraxHttpUtil .ACCOUNT_LINK_ROUTE ));
@@ -67,7 +124,5 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
67124 }
68125 }
69126 });
70-
71- return true ;
72127 }
73128}
0 commit comments