Skip to content

Commit

Permalink
feat: add Form#onClose(Consumer<ModalFormCancelReason>) method that…
Browse files Browse the repository at this point in the history
… can be used to set a callback which will be called with the close reason when the form is closed. The old `Form#onClose` method is still available that just ignores the reason.
  • Loading branch information
smartcmd committed Jan 24, 2025
1 parent a9704d1 commit 096025b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Unless otherwise specified, any version comparison below is the comparison of se
room by plugin and will be deleted when shutdown.
- (API) Added `PluginManager#registerCustomSource` and `PluginManager#registerCustomLoaderFactory`, custom plugin loaders
and sources can be registered by plugin now.
- (API) Added `Form#onClose(Consumer<ModalFormCancelReason>)` method that can be used to set a callback which will be called
with the close reason when the form is closed. The old `Form#onClose` method is still available that just ignores the reason.

### Changed

Expand Down
17 changes: 11 additions & 6 deletions api/src/main/java/org/allaymc/api/form/type/CustomForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.form.element.*;
import org.cloudburstmc.protocol.bedrock.data.ModalFormCancelReason;
import org.jetbrains.annotations.ApiStatus;

import java.util.ArrayList;
Expand Down Expand Up @@ -329,13 +330,17 @@ public CustomForm onClose(Runnable onClose) {
*/
@Override
public void handleResponse(String data) {
if (data == null) {
onClose.run();
return;
}
List<String> responses = GSON.fromJson(data, new TypeToken<List<String>>() {}.getType());
onResponse.accept(responses);
response = responses;
this.onResponse.accept(responses);
this.response = responses;
}

/**
* {@inheritDoc}
*/
@Override
public void handleClose(ModalFormCancelReason reason) {
onClose.accept(reason);
}

/**
Expand Down
28 changes: 23 additions & 5 deletions api/src/main/java/org/allaymc/api/form/type/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.google.gson.Gson;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.cloudburstmc.protocol.bedrock.data.ModalFormCancelReason;

import java.util.function.Consumer;

/**
* The base class for form.
Expand All @@ -12,7 +15,7 @@ public abstract sealed class Form permits SimpleForm, ModalForm, CustomForm {

protected static final Gson GSON = new Gson();

protected transient Runnable onClose = () -> {};
protected transient Consumer<ModalFormCancelReason> onClose = reason -> {};
protected transient Object response;

/**
Expand All @@ -27,25 +30,40 @@ public void sendTo(EntityPlayer player) {
/**
* Handle the response from the player.
* <p>
* This method will be called when server receive the response packet from client.
* Usually the user won't need to use this method.
* If the player just close the form due to some reason (e.g. the player is opening his inventory), {@link #handleClose(ModalFormCancelReason)}
* will be called instead of this method.
*
* @param data the response data from the player. Can be {@code null} if the player only close the form without other actions.
* @param data the response data from the player.
*/
public abstract void handleResponse(String data);

/**
* Handle the close of the form. This method will be called when the player close the form.
*
* @param reason the reason why the form is closed.
*/
public abstract void handleClose(ModalFormCancelReason reason);

/**
* @see #onClose(Consumer)
*/
public Form onClose(Runnable onClose) {
return onClose(reason -> onClose.run());
}

/**
* Add a callback that will be called when player only close the form without other actions.
*
* @param onClose the callback.
*
* @return the form.
*/
public Form onClose(Runnable onClose) {
public Form onClose(Consumer<ModalFormCancelReason> onClose) {
this.onClose = onClose;
return this;
}


/**
* Convert this form to a JSON string.
* <p>
Expand Down
10 changes: 6 additions & 4 deletions api/src/main/java/org/allaymc/api/form/type/ModalForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import org.cloudburstmc.protocol.bedrock.data.ModalFormCancelReason;

import java.util.function.Consumer;

Expand Down Expand Up @@ -144,15 +145,16 @@ public ModalForm onResponse(Consumer<String> onResponse) {
@Override
public void handleResponse(String data) {
response = data;
if (data == null) {
onClose.run();
return;
}
onResponse.accept(data);
if (data.equals("true")) onTrue.run();
else onFalse.run();
}

@Override
public void handleClose(ModalFormCancelReason reason) {
onClose.accept(reason);
}

/**
* {@inheritDoc}
*/
Expand Down
14 changes: 9 additions & 5 deletions api/src/main/java/org/allaymc/api/form/type/SimpleForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.form.element.Button;
import org.allaymc.api.form.element.ImageData;
import org.cloudburstmc.protocol.bedrock.data.ModalFormCancelReason;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -138,11 +139,6 @@ public SimpleForm onClose(Runnable onClose) {
*/
@Override
public void handleResponse(String data) {
if (data == null) {
response = null;
onClose.run();
return;
}
int buttonIndex;
try {
buttonIndex = Integer.parseInt(data);
Expand All @@ -160,6 +156,14 @@ public void handleResponse(String data) {
response = button;
}

/**
* {@inheritDoc}
*/
@Override
public void handleClose(ModalFormCancelReason reason) {
onClose.accept(reason);
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ public void handleSync(EntityPlayer player, ModalFormResponsePacket packet, long
isServerSettingsForm = true;
}

form.handleResponse(packet.getFormData() != null ? packet.getFormData().trim() : null);
var formData = packet.getFormData();
if (formData != null) {
form.handleResponse(formData.trim());
} else {
form.handleClose(packet.getCancelReason().orElseThrow());
}
if (isServerSettingsForm) {
((CustomForm) form).syncDefaultValueToResponse();
}
Expand Down

0 comments on commit 096025b

Please sign in to comment.