1+ package me .kodysimpson .simpapi .input ;
2+
3+ import org .bukkit .conversations .*;
4+ import org .bukkit .entity .Player ;
5+ import org .bukkit .plugin .Plugin ;
6+
7+ import java .util .function .Consumer ;
8+ import java .util .function .Predicate ;
9+
10+ public class ChatInput {
11+
12+ /**
13+ * Request input from a player with custom validation
14+ * @param plugin Your plugin instance
15+ * @param player The player to request input from
16+ * @param prompt The message to show the player
17+ * @param validator Custom validation function that returns true if input is valid
18+ * @param onInput Callback function to handle the validated input
19+ * @param onCancel Optional callback for when input is cancelled
20+ */
21+ public static void requestInput (Plugin plugin , Player player , String prompt ,
22+ Predicate <String > validator , Consumer <String > onInput , Runnable onCancel ) {
23+
24+ ConversationFactory factory = new ConversationFactory (plugin )
25+ .withModality (true )
26+ .withLocalEcho (false )
27+ .withPrefix (context -> "§6§l[Input] §r" )
28+ .withFirstPrompt (new StringPrompt () {
29+ @ Override
30+ public String getPromptText (ConversationContext context ) {
31+ return prompt ;
32+ }
33+
34+ @ Override
35+ public Prompt acceptInput (ConversationContext context , String input ) {
36+ if (input .equalsIgnoreCase ("cancel" )) {
37+ context .getForWhom ().sendRawMessage ("§cInput cancelled." );
38+ if (onCancel != null ) {
39+ plugin .getServer ().getScheduler ().runTask (plugin , onCancel );
40+ }
41+ return END_OF_CONVERSATION ;
42+ }
43+
44+ if (validator .test (input )) {
45+ plugin .getServer ().getScheduler ().runTask (plugin , () -> onInput .accept (input ));
46+ return END_OF_CONVERSATION ;
47+ }
48+ return this ;
49+ }
50+ })
51+ .withEscapeSequence ("cancel" )
52+ .thatExcludesNonPlayersWithMessage ("Only players can provide input!" )
53+ .addConversationAbandonedListener (event -> {
54+ if (!event .gracefulExit () && onCancel != null ) {
55+ plugin .getServer ().getScheduler ().runTask (plugin , onCancel );
56+ }
57+ });
58+
59+ factory .buildConversation (player ).begin ();
60+ }
61+
62+ /**
63+ * Request simple text input from a player
64+ * @param plugin Your plugin instance
65+ * @param player The player to request input from
66+ * @param prompt The message to show the player
67+ * @param onInput Callback function to handle the input
68+ */
69+ public static void requestInput (Plugin plugin , Player player , String prompt , Consumer <String > onInput ) {
70+ requestInput (plugin , player , prompt , input -> true , onInput , null );
71+ }
72+
73+ /**
74+ * Request numeric input from a player
75+ * @param plugin Your plugin instance
76+ * @param player The player to request input from
77+ * @param prompt The message to show the player
78+ * @param onInput Callback function to handle the validated numeric input
79+ */
80+ public static void requestNumericInput (Plugin plugin , Player player , String prompt , Consumer <Double > onInput ) {
81+ requestInput (plugin , player , prompt ,
82+ input -> {
83+ try {
84+ Double .parseDouble (input );
85+ return true ;
86+ } catch (NumberFormatException e ) {
87+ return false ;
88+ }
89+ },
90+ input -> onInput .accept (Double .parseDouble (input )),
91+ null
92+ );
93+ }
94+ }
0 commit comments