From 22e1a25d9e326dcdfc1bc17403942a330213dd23 Mon Sep 17 00:00:00 2001 From: Tim203 Date: Wed, 26 Jun 2024 12:48:47 +0200 Subject: [PATCH] Allow individual buttons to have a callback Fixes #12 --- .../org/geysermc/cumulus/form/SimpleForm.java | 70 ++++++++++++------ .../geysermc/cumulus/form/impl/FormImpl.java | 10 ++- .../form/impl/simple/SimpleFormImpl.java | 71 +++++++++++++------ 3 files changed, 107 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/geysermc/cumulus/form/SimpleForm.java b/src/main/java/org/geysermc/cumulus/form/SimpleForm.java index 72599d7..5123a56 100644 --- a/src/main/java/org/geysermc/cumulus/form/SimpleForm.java +++ b/src/main/java/org/geysermc/cumulus/form/SimpleForm.java @@ -1,30 +1,12 @@ /* - * Copyright (c) 2020-2023 GeyserMC - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - * @author GeyserMC + * Copyright (c) 2020-2024 GeyserMC + * Licensed under the MIT license * @link https://github.com/GeyserMC/Cumulus */ package org.geysermc.cumulus.form; import java.util.List; +import java.util.function.Consumer; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.common.returnsreceiver.qual.This; @@ -90,6 +72,15 @@ interface Builder extends FormBuilder { */ @This Builder button(@NonNull ButtonComponent button); + /** + * Adds a button with callback directly to the form. + * + * @param button the button to add + * @param callback the handler when the button is clicked + * @return the form builder + */ + @This Builder button(@NonNull ButtonComponent button, @NonNull Consumer callback); + /** * Adds a button with image to the Form. * @@ -100,6 +91,21 @@ interface Builder extends FormBuilder { */ @This Builder button(@NonNull String text, FormImage.@NonNull Type type, @NonNull String data); + /** + * Adds a button with image and callback to the Form. + * + * @param text text of the button + * @param type type of image + * @param data the data for the image type + * @param callback the handler when the button is clicked + * @return the form builder + */ + @This Builder button( + @NonNull String text, + FormImage.@NonNull Type type, + @NonNull String data, + @NonNull Consumer callback); + /** * Adds a button with image to the Form. * @@ -109,6 +115,19 @@ interface Builder extends FormBuilder { */ @This Builder button(@NonNull String text, @Nullable FormImage image); + /** + * Adds a button with image and callback to the Form. + * + * @param text the text of the button + * @param image the image + * @param callback the handler when the button is clicked + * @return the form builder + */ + @This Builder button( + @NonNull String text, + @Nullable FormImage image, + @NonNull Consumer callback); + /** * Adds a button to the Form. * @@ -117,6 +136,15 @@ interface Builder extends FormBuilder { */ @This Builder button(@NonNull String text); + /** + * Adds a button with callback to the Form. + * + * @param text the text of the button + * @param callback the handler when the button is clicked + * @return the form builder + */ + @This Builder button(@NonNull String text, @NonNull Consumer callback); + /** * Adds a button with image to the Form, but only when shouldAdd is true. * diff --git a/src/main/java/org/geysermc/cumulus/form/impl/FormImpl.java b/src/main/java/org/geysermc/cumulus/form/impl/FormImpl.java index d41da16..ddbcf60 100644 --- a/src/main/java/org/geysermc/cumulus/form/impl/FormImpl.java +++ b/src/main/java/org/geysermc/cumulus/form/impl/FormImpl.java @@ -187,7 +187,12 @@ public B resultHandler( @Override public abstract @NonNull F build(); - protected void setResponseHandler(FormImpl impl, F form) { + protected void setResponseHandler(@NonNull FormImpl impl, @NonNull F form) { + setResponseHandler(impl, form, null); + } + + protected void setResponseHandler( + @NonNull FormImpl impl, @NonNull F form, @Nullable Consumer validHandler) { impl.resultHandler( result -> { if (selectedResultHandler != null) { @@ -217,6 +222,9 @@ protected void setResponseHandler(FormImpl impl, F form) { if (validResultHandler != null) { validResultHandler.accept(form, response); } + if (validHandler != null) { + validHandler.accept(response); + } } }); } diff --git a/src/main/java/org/geysermc/cumulus/form/impl/simple/SimpleFormImpl.java b/src/main/java/org/geysermc/cumulus/form/impl/simple/SimpleFormImpl.java index ea5c2ac..912980e 100644 --- a/src/main/java/org/geysermc/cumulus/form/impl/simple/SimpleFormImpl.java +++ b/src/main/java/org/geysermc/cumulus/form/impl/simple/SimpleFormImpl.java @@ -1,35 +1,20 @@ /* - * Copyright (c) 2020-2023 GeyserMC - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - * @author GeyserMC + * Copyright (c) 2020-2024 GeyserMC + * Licensed under the MIT license * @link https://github.com/GeyserMC/Cumulus */ package org.geysermc.cumulus.form.impl.simple; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Objects; +import java.util.function.Consumer; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; +import org.checkerframework.common.returnsreceiver.qual.This; import org.geysermc.cumulus.component.ButtonComponent; import org.geysermc.cumulus.form.SimpleForm; import org.geysermc.cumulus.form.impl.FormImpl; @@ -62,6 +47,7 @@ public static final class Builder implements SimpleForm.Builder { private final List buttons = new ArrayList<>(); + private final Map> callbacks = new HashMap<>(); private String content = ""; @Override @@ -76,6 +62,13 @@ public Builder button(@NonNull ButtonComponent button) { return this; } + @Override + public SimpleForm.@This Builder button( + @NonNull ButtonComponent button, @NonNull Consumer callback) { + callbacks.put(buttons.size(), Objects.requireNonNull(callback)); + return button(button); + } + @Override public Builder button( @NonNull String text, FormImage.@NonNull Type type, @NonNull String data) { @@ -83,18 +76,44 @@ public Builder button( return this; } + @Override + public SimpleForm.@This Builder button( + @NonNull String text, + FormImage.@NonNull Type type, + @NonNull String data, + @NonNull Consumer callback) { + callbacks.put(buttons.size(), Objects.requireNonNull(callback)); + return button(text, type, data); + } + @Override public Builder button(@NonNull String text, @Nullable FormImage image) { buttons.add(ButtonComponent.of(translate(text), image)); return this; } + @Override + public SimpleForm.@This Builder button( + @NonNull String text, + @Nullable FormImage image, + @NonNull Consumer callback) { + callbacks.put(buttons.size(), Objects.requireNonNull(callback)); + return button(text, image); + } + @Override public Builder button(@NonNull String text) { buttons.add(ButtonComponent.of(translate(text))); return this; } + @Override + public SimpleForm.@This Builder button( + @NonNull String text, @NonNull Consumer callback) { + callbacks.put(buttons.size(), Objects.requireNonNull(callback)); + return button(text); + } + @Override public Builder optionalButton( @NonNull String text, @@ -127,7 +146,15 @@ public Builder optionalButton(@NonNull String text, boolean shouldAdd) { @Override public @NonNull SimpleForm build() { SimpleFormImpl form = new SimpleFormImpl(title, content, buttons); - setResponseHandler(form, form); + setResponseHandler( + form, + form, + valid -> { + Consumer callback = callbacks.get(valid.clickedButtonId()); + if (callback != null) { + callback.accept(valid); + } + }); return form; }