Skip to content

Commit

Permalink
Allow individual buttons to have a callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim203 committed Jun 26, 2024
1 parent b79463c commit e6422c7
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 44 deletions.
70 changes: 49 additions & 21 deletions src/main/java/org/geysermc/cumulus/form/SimpleForm.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -90,6 +72,15 @@ interface Builder extends FormBuilder<Builder, SimpleForm, SimpleFormResponse> {
*/
@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<SimpleFormResponse> callback);

/**
* Adds a button with image to the Form.
*
Expand All @@ -100,6 +91,21 @@ interface Builder extends FormBuilder<Builder, SimpleForm, SimpleFormResponse> {
*/
@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<SimpleFormResponse> callback);

/**
* Adds a button with image to the Form.
*
Expand All @@ -109,6 +115,19 @@ interface Builder extends FormBuilder<Builder, SimpleForm, SimpleFormResponse> {
*/
@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<SimpleFormResponse> callback);

/**
* Adds a button to the Form.
*
Expand All @@ -117,6 +136,15 @@ interface Builder extends FormBuilder<Builder, SimpleForm, SimpleFormResponse> {
*/
@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<SimpleFormResponse> callback);

/**
* Adds a button with image to the Form, but only when shouldAdd is true.
*
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/geysermc/cumulus/form/impl/FormImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ public B resultHandler(
@Override
public abstract @NonNull F build();

protected void setResponseHandler(FormImpl<R> impl, F form) {
protected void setResponseHandler(@NonNull FormImpl<R> impl, @NonNull F form) {
setResponseHandler(impl, form, null);
}

protected void setResponseHandler(
@NonNull FormImpl<R> impl, @NonNull F form, @Nullable Consumer<R> validHandler) {
impl.resultHandler(
result -> {
if (selectedResultHandler != null) {
Expand Down Expand Up @@ -217,6 +222,9 @@ protected void setResponseHandler(FormImpl<R> impl, F form) {
if (validResultHandler != null) {
validResultHandler.accept(form, response);
}
if (validHandler != null) {
validHandler.accept(response);
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -62,6 +47,7 @@ public static final class Builder
implements SimpleForm.Builder {

private final List<ButtonComponent> buttons = new ArrayList<>();
private final Map<Integer, Consumer<SimpleFormResponse>> callbacks = new HashMap<>();
private String content = "";

@Override
Expand All @@ -76,25 +62,58 @@ public Builder button(@NonNull ButtonComponent button) {
return this;
}

@Override
public SimpleForm.@This Builder button(
@NonNull ButtonComponent button, @NonNull Consumer<SimpleFormResponse> 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) {
buttons.add(ButtonComponent.of(translate(text), type, data));
return this;
}

@Override
public SimpleForm.@This Builder button(
@NonNull String text,
FormImage.@NonNull Type type,
@NonNull String data,
@NonNull Consumer<SimpleFormResponse> 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<SimpleFormResponse> 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<SimpleFormResponse> callback) {
callbacks.put(buttons.size(), Objects.requireNonNull(callback));
return button(text);
}

@Override
public Builder optionalButton(
@NonNull String text,
Expand Down Expand Up @@ -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<SimpleFormResponse> callback = callbacks.get(valid.clickedButtonId());
if (callback != null) {
callback.accept(valid);
}
});
return form;
}

Expand Down

0 comments on commit e6422c7

Please sign in to comment.