Use the new result handlers instead of the deprecated response handlers

This commit is contained in:
Tim203 committed 2022-08-14 01:42:04 +02:00
1 parent 1b8da69966
commit 3f309e9035
1 file changed
+15 -69
+15 -69
View File
@@ -7,8 +7,8 @@ permalink: /floodgate/forms/
# What is Cumulus? # What is Cumulus?
Bedrock Edition has a cool exclusive feature called Forms.<br> Bedrock Edition has a cool exclusive feature called Forms.<br>
Cumulus is the new Forms API that we use in Geyser and Floodgate. The source code is available [here](/floodgate/forms/). Cumulus is the Forms API that we use in Geyser and Floodgate.<br>
You can access the Cumulus API through the [Floodgate API](/floodgate/api/). The source code is available [here](/floodgate/forms/). You can access the Cumulus API through the [Floodgate API](/floodgate/api/).
Bedrock knows three types of Forms: Bedrock knows three types of Forms:
* ModalForm * ModalForm
@@ -72,31 +72,6 @@ CustomForm.builder()
.slider("Text", 0, 10, 1, 5) .slider("Text", 0, 10, 1, 5)
``` ```
## Components
Forms have different component types, most of them are limited to the CustomForm and some are available to everything but CustomForm.
### Button
Limited to ModalForm and SimpleForm. With SimpleForm, buttons can have image icons and there can be more than 2.
### Dropdown
Limited to CustomForm
### Input
Limited to CustomForm.
### Label
Limited to CustomForm.
### Slider
Limited to CustomForm.
### Stepslider
Limited to CustomForm.
### Toggle
Limited to CustomForm.
## Sending a form ## Sending a form
After you decided which form type you want to use and finished filling in the actual content, it's time to send the Form to the Bedrock player.<br> After you decided which form type you want to use and finished filling in the actual content, it's time to send the Form to the Bedrock player.<br>
@@ -114,7 +89,7 @@ So you can make and send forms in a pretty compact way by doing something like t
FloodgatePlayer player = FloodgateApi.getInstance().getPlayer(uuid); FloodgatePlayer player = FloodgateApi.getInstance().getPlayer(uuid);
... ...
player.sendForm( player.sendForm(
new CustomForm.Builder() CustomForm.builder()
.title("My cool title") .title("My cool title")
.content("10/10 content") .content("10/10 content")
)); ));
@@ -122,46 +97,17 @@ player.sendForm(
## Receiving a response from the client ## Receiving a response from the client
It's nice and all that we can send forms to a client, but we also want to be able to get a response from a client and being able to handle them.<be> It's nice and all that we can send forms to a client, but we also want to be able to get a response from a client and being able to handle them.<br>
We can do that using the `responseHandler(BiConsumer<? extends Form, String>)` method in the FormBuilder: We can do that using one (or multiple) result handers. The most used result handlers are: `validResultHandler(BiConsumer<Form, ValidFormResponseResult> | Consumer<ValidFormResponseResult>)`, `invalidResultHandler`, `closedResultHandler` and `closedOrInvalidResultHandler`.<br>
Here follows an example that uses result handlers:
```java ```java
_Modal_Form.builder() CustomForm.builder()
.title("Feedback form") .title("geyser.auth.login.form.details.title")
.content("We're asking for feedback, are you willing to enter some feedback to improve our server?") .label("geyser.auth.login.form.details.desc")
.button1("Yes") // id = 0 .input("geyser.auth.login.form.details.email", "account@geysermc.org", "")
.button2("No") // id = 1 .input("geyser.auth.login.form.details.pass", "123456", "")
.responseHandler((form, responseData) -> { .closedOrInvalidResultHandler(() -> buildAndShowLoginDetailsWindow(session))
_Modal_FormResponse response = form.parseResponse(responseData); .validResultHandler(response -> session.authenticate(response.next(), response.next())));
if (!response.isCorrect()) {
// player closed the form or returned invalid info (see FormResponse)
return;
}
// short version of getClickedButtonId == 0
if (response.getResult()) {
System.out.println("Yay, he wants to give us feedback");
return;
}
System.out.println("No feedback for us :(");
});
```
Or using the response handler setter of the Form (not a FormBuilder):
```java
_Modal_Form form = ...;
form.setResponseHandler(responseData -> {
_Modal_FormResponse response = form.parseResponse(responseData);
if (!response.isCorrect()) {
// player closed the form or returned invalid info (see FormResponse)
return;
}
// short version of getClickedButtonId == 0
if (response.getResult()) {
System.out.println("Yay, he wants to give us feedback");
return;
}
System.out.println("No feedback for us :(");
});
``` ```
## Advanced stuff ## Advanced stuff
@@ -169,7 +115,7 @@ form.setResponseHandler(responseData -> {
The FormBuilder also has support for translating the data used in the builder.<br> The FormBuilder also has support for translating the data used in the builder.<br>
To add a translator, you can use the `translator(BiFunction<String, String, String>)` or the `translator(BiFunction<String, String, String>, String)` method: To add a translator, you can use the `translator(BiFunction<String, String, String>)` or the `translator(BiFunction<String, String, String>, String)` method:
```java ```java
_Modal_Form form = _Modal_Form.builder() ModalForm form = ModalForm.builder()
.translator(this::translate, userLanguage) .translator(this::translate, userLanguage)
.title("Title") .title("Title")
.content("Content") .content("Content")
@@ -186,7 +132,7 @@ public String translate(String key, String locale) {
``` ```
Or you can have the translate method directly in the FormBuilder instead of a separate method: Or you can have the translate method directly in the FormBuilder instead of a separate method:
```java ```java
_Modal_Form form = _Modal_Form.builder() ModalForm form = ModalForm.builder()
.translator((key, unused) -> { .translator((key, unused) -> {
// this method will be called for every string, in this case, 4 times: // this method will be called for every string, in this case, 4 times:
// Title, Content, translate.button1, translate.button2 // Title, Content, translate.button1, translate.button2