Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions docs/src/fancydialogs/tutorials/json-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ Below is an example of a simple dialog defined using the FancyDialogs JSON schem
"label": "<color:#ff7300>What is your favorite color?</color>",
"placeholder": "gold",
"maxLength": 50,
"maxLines": 1
"maxLines": 1,
"requirements": {
"type": ""
}
}
]
},
Expand All @@ -49,7 +52,10 @@ Below is an example of a simple dialog defined using the FancyDialogs JSON schem
"name": "message",
"data": "Your favorite color is: <color:{fav_color}>{fav_color}</color>"
}
]
],
"requirements": {
"type": ""
}
}
]
}
Expand Down Expand Up @@ -85,6 +91,13 @@ Items will be supported in the body section in a future release.

`selects`: A list of select fields - see [Select Fields](#select-fields) for details

`requirements`: The requirement for this field to display
- `type`: Either `permission` or `stringMatch`.
- `permission`: If type is `permission`, this is the permission to check for.
- `input`: If type is `stringMatch`, this is the string being matched against `output`.
- `output`: If type is `stringMatch`, this is the string being matched against `input`.


!!!info
More input types will be added in future releases, such as checkboxes and number sliders.
!!!
Expand All @@ -103,6 +116,13 @@ More input types will be added in future releases, such as checkboxes and number

`maxLines`: The maximum number of lines for the input (greater than 1 will create a multiline text field)

`requirements`: The requirement for this field to display
- `type`: Either `permission` or `stringMatch`.
- `permission`: If type is `permission`, this is the permission to check for.
- `input`: If type is `stringMatch`, this is the string being matched against `output`.
- `output`: If type is `stringMatch`, this is the string being matched against `input`.


#### Select Fields

`key`: The key to use to store the input value (can be used as a placeholder in actions)
Expand All @@ -116,11 +136,23 @@ More input types will be added in future releases, such as checkboxes and number
- `display`: The text to display in the select field (supports MiniMessage & PlaceholderAPI)
- `initial`: Whether this option is selected by default (default: false)

`requirements`: The requirement for this field to display
- `type`: Either `permission` or `stringMatch`.
- `permission`: If type is `permission`, this is the permission to check for.
- `input`: If type is `stringMatch`, this is the string being matched against `output`.
- `output`: If type is `stringMatch`, this is the string being matched against `input`.

### Button fields

- `label`: The text to display on the button (supports MiniMessage & PlaceholderAPI)
- `tooltip`: The tooltip to display when hovering over the button (supports MiniMessage & PlaceholderAPI)
- `actions`: A list of actions that will be executed when the button is clicked - see [Actions](#actions) for details

- `requirements`: The requirement for this field to display
- `type`: Either `permission` or `stringMatch`.
- `permission`: If type is `permission`, this is the permission to check for.
- `input`: If type is `stringMatch`, this is the string being matched against `output`.
- `output`: If type is `stringMatch`, this is the string being matched against `input`.

#### Actions

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fancyinnovations.fancydialogs.api.data;

import java.util.List;
import java.util.Map;
import java.util.UUID;

public class DialogButton {
Expand All @@ -9,12 +10,14 @@ public class DialogButton {
private final String tooltip;
private final List<DialogAction> actions;
private transient String id;
private final Map<String, String> requirements;

public DialogButton(String label, String tooltip, List<DialogAction> actions) {
public DialogButton(String label, String tooltip, List<DialogAction> actions, Map<String, String> requirements) {
this.id = UUID.randomUUID().toString();
this.label = label;
this.tooltip = tooltip;
this.actions = actions;
this.requirements = Map.copyOf(requirements);
}

public String id() {
Expand All @@ -32,6 +35,8 @@ public String tooltip() {
return tooltip;
}

public Map<String, String> requirements() { return requirements; }

public List<DialogAction> actions() {
return actions;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.fancyinnovations.fancydialogs.api.data.inputs;

import java.util.Map;

public class DialogCheckbox extends DialogInput {

private final boolean initial;

public DialogCheckbox(String key, String label, int order, boolean initial) {
super(key, label, order);
public DialogCheckbox(String key, String label, int order, boolean initial, Map<String, String> requirements) {
super(key, label, order, requirements);
this.initial = initial;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.fancyinnovations.fancydialogs.api.data.inputs;

import java.util.Map;

public abstract class DialogInput {

protected final String key;
protected final String label;
protected final int order;
protected final Map<String, String> requirements;

public DialogInput(String key, String label, int order) {
public DialogInput(String key, String label, int order, Map<String, String> requirements) {
this.key = key;
this.label = label;
this.order = order;
this.requirements = Map.copyOf(requirements);
}

public String getKey() {
Expand All @@ -23,4 +27,6 @@ public String getLabel() {
public int getOrder() {
return order;
}

public Map<String, String> getRequirements() { return requirements; }
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.fancyinnovations.fancydialogs.api.data.inputs;

import java.util.List;
import java.util.Map;

public class DialogSelect extends DialogInput {

private final List<Entry> options;

public DialogSelect(String key, String label, int order, List<Entry> options) {
super(key, label, order);
public DialogSelect(String key, String label, int order, List<Entry> options, Map<String, String> requirements) {
super(key, label, order, requirements);
this.options = options;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.fancyinnovations.fancydialogs.api.data.inputs;

import java.util.Map;

public class DialogTextField extends DialogInput {

private final String placeholder;
private final int maxLength;
private final int maxLines;

public DialogTextField(String key, String label, int order, String placeholder, int maxLength, int maxLines) {
super(key, label, order);
public DialogTextField(String key, String label, int order, String placeholder, int maxLength, int maxLines, Map<String, String> requirements) {
super(key, label, order, requirements);
this.placeholder = placeholder;
this.maxLength = maxLength;
this.maxLines = maxLines;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ private void buildDialog() {
confirmText,
List.of(
new DialogButton.DialogAction("confirm", "")
)
),
Map.of("", "")
);
this.confirmButtonId = confirmBtn.id();

Expand All @@ -108,14 +109,15 @@ private void buildDialog() {
cancelText,
List.of(
new DialogButton.DialogAction("cancel", "")
)
),
Map.of("", "")
);
this.cancelButtonId = cancelBtn.id();

List<DialogTextField> textFields = null;
if (expectedUserInput != null && !expectedUserInput.isEmpty()) {
textFields = List.of(
new DialogTextField("confirmation_user_input", "Type '" + expectedUserInput + "' to confirm", 0, "", expectedUserInput.length(), 1)
new DialogTextField("confirmation_user_input", "Type '" + expectedUserInput + "' to confirm", 0, "", expectedUserInput.length(), 1, Map.of("type", ""))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import de.oliver.fancysitula.factories.FancySitula;
import org.bukkit.entity.Player;
import org.lushplugins.chatcolorhandler.ChatColorHandler;
import org.lushplugins.chatcolorhandler.parsers.Parser;
import org.lushplugins.chatcolorhandler.parsers.ParserTypes;

import java.util.ArrayList;
Expand Down Expand Up @@ -54,6 +55,29 @@ private String replaceArgs(String text, String[] args) {
return result;
}

private boolean checkPerm(Player player, String perm) {
if (perm == null) {
return true;
}
if (!perm.equals("") && !player.hasPermission(perm)) {
return false;
}
return true;
}

private boolean checkRequirements(Player player, Map<String, String> requirements) {
if (requirements == null) { return true; }
if (requirements.get("type") == null) { return true; }
if (requirements.get("type").equals("permission")) {
return checkPerm(player, requirements.get("permission"));
}
if (requirements.get("type").equals("stringMatch")) {
if (requirements.get("input") == null || requirements.get("output") == null) { return true; }
return ChatColorHandler.translate(requirements.get("input"), player, ParserTypes.placeholder()).equals(ChatColorHandler.translate(requirements.get("output"), player, ParserTypes.placeholder()));
}
return true;
}

private FS_Dialog buildForPlayer(Player player, String[] args) {
List<FS_DialogBody> body = new ArrayList<>();
for (DialogBodyData bodyData : data.body()) {
Expand All @@ -73,6 +97,7 @@ private FS_Dialog buildForPlayer(Player player, String[] args) {
List<FS_DialogInput> inputs = new ArrayList<>();
if (data.inputs() != null) {
for (DialogInput input : data.inputs().all()) {
if (!checkRequirements(player, input.getRequirements())) { continue; }
FS_DialogInputControl control = null;
if (input instanceof DialogTextField textField) {
String label = replaceArgs(textField.getLabel(), args);
Expand Down Expand Up @@ -122,6 +147,7 @@ private FS_Dialog buildForPlayer(Player player, String[] args) {
List<FS_DialogActionButton> actions = new ArrayList<>();
for (DialogButton button : data.buttons()) {
FS_DialogActionButtonAction buttonAction;
if (!checkRequirements(player, button.requirements())) { continue; }

if (button.actions().size() == 1 &&
button.actions().get(0).name().equals("copy_to_clipboard")) {
Expand Down
Loading