Skip to content

Commit 29fdd1f

Browse files
committed
added chat input and updated README.md
1 parent ff8323f commit 29fdd1f

File tree

3 files changed

+98
-4
lines changed

3 files changed

+98
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SimpAPI v4.4.0
1+
# SimpAPI v4.6.1
22

33
## Table Of Contents:
44
****
@@ -39,7 +39,7 @@ JavaDocs: https://kodysimpson.github.io/SimpAPI/index.html
3939
<dependency>
4040
<groupId>com.github.KodySimpson</groupId>
4141
<artifactId>SimpAPI</artifactId>
42-
<version>4.3.2</version>
42+
<version>4.6.1</version>
4343
</dependency>
4444
```
4545

@@ -65,7 +65,7 @@ repositories {
6565
Groovy/Kotlin:
6666
```groovy
6767
dependencies {
68-
implementation 'com.github.KodySimpson:SimpAPI:4.3.2'
68+
implementation 'com.github.KodySimpson:SimpAPI:4.6.1'
6969
}
7070
```
7171

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>me.kodysimpson</groupId>
88
<artifactId>SimpAPI</artifactId>
9-
<version>4.6.0</version>
9+
<version>4.6.1</version>
1010
<packaging>jar</packaging>
1111

1212
<name>SimpAPI</name>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)