Skip to content

Commit a8d1fb2

Browse files
authored
fancydialogs: Add requirements to buttons & inputs (#201)
* fancydialogs - add requirements to buttons & inputs * fancydialogs - update docs for requirements * fancydialogs - simplify return * fancydialogs - fix mutability of the map makes the map immutable since we are scared of it blowing up * fancydialogs: string match -> stringMatch
1 parent 175bb55 commit a8d1fb2

9 files changed

Lines changed: 120 additions & 28 deletions

File tree

docs/src/fancydialogs/tutorials/json-schema.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ Below is an example of a simple dialog defined using the FancyDialogs JSON schem
3636
"label": "<color:#ff7300>What is your favorite color?</color>",
3737
"placeholder": "gold",
3838
"maxLength": 50,
39-
"maxLines": 1
39+
"maxLines": 1,
40+
"requirements": {
41+
"type": ""
42+
}
4043
}
4144
]
4245
},
@@ -49,7 +52,10 @@ Below is an example of a simple dialog defined using the FancyDialogs JSON schem
4952
"name": "message",
5053
"data": "Your favorite color is: <color:{fav_color}>{fav_color}</color>"
5154
}
52-
]
55+
],
56+
"requirements": {
57+
"type": ""
58+
}
5359
}
5460
]
5561
}
@@ -85,6 +91,13 @@ Items will be supported in the body section in a future release.
8591

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

94+
`requirements`: The requirement for this field to display
95+
- `type`: Either `permission` or `stringMatch`.
96+
- `permission`: If type is `permission`, this is the permission to check for.
97+
- `input`: If type is `stringMatch`, this is the string being matched against `output`.
98+
- `output`: If type is `stringMatch`, this is the string being matched against `input`.
99+
100+
88101
!!!info
89102
More input types will be added in future releases, such as checkboxes and number sliders.
90103
!!!
@@ -103,6 +116,13 @@ More input types will be added in future releases, such as checkboxes and number
103116

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

119+
`requirements`: The requirement for this field to display
120+
- `type`: Either `permission` or `stringMatch`.
121+
- `permission`: If type is `permission`, this is the permission to check for.
122+
- `input`: If type is `stringMatch`, this is the string being matched against `output`.
123+
- `output`: If type is `stringMatch`, this is the string being matched against `input`.
124+
125+
106126
#### Select Fields
107127

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

139+
`requirements`: The requirement for this field to display
140+
- `type`: Either `permission` or `stringMatch`.
141+
- `permission`: If type is `permission`, this is the permission to check for.
142+
- `input`: If type is `stringMatch`, this is the string being matched against `output`.
143+
- `output`: If type is `stringMatch`, this is the string being matched against `input`.
144+
119145
### Button fields
120146

121147
- `label`: The text to display on the button (supports MiniMessage & PlaceholderAPI)
122148
- `tooltip`: The tooltip to display when hovering over the button (supports MiniMessage & PlaceholderAPI)
123149
- `actions`: A list of actions that will be executed when the button is clicked - see [Actions](#actions) for details
150+
151+
- `requirements`: The requirement for this field to display
152+
- `type`: Either `permission` or `stringMatch`.
153+
- `permission`: If type is `permission`, this is the permission to check for.
154+
- `input`: If type is `stringMatch`, this is the string being matched against `output`.
155+
- `output`: If type is `stringMatch`, this is the string being matched against `input`.
124156

125157
#### Actions
126158

plugins/fancydialogs/fd-api/src/main/java/com/fancyinnovations/fancydialogs/api/data/DialogButton.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fancyinnovations.fancydialogs.api.data;
22

33
import java.util.List;
4+
import java.util.Map;
45
import java.util.UUID;
56

67
public class DialogButton {
@@ -9,12 +10,14 @@ public class DialogButton {
910
private final String tooltip;
1011
private final List<DialogAction> actions;
1112
private transient String id;
13+
private final Map<String, String> requirements;
1214

13-
public DialogButton(String label, String tooltip, List<DialogAction> actions) {
15+
public DialogButton(String label, String tooltip, List<DialogAction> actions, Map<String, String> requirements) {
1416
this.id = UUID.randomUUID().toString();
1517
this.label = label;
1618
this.tooltip = tooltip;
1719
this.actions = actions;
20+
this.requirements = Map.copyOf(requirements);
1821
}
1922

2023
public String id() {
@@ -32,6 +35,8 @@ public String tooltip() {
3235
return tooltip;
3336
}
3437

38+
public Map<String, String> requirements() { return requirements; }
39+
3540
public List<DialogAction> actions() {
3641
return actions;
3742
}

plugins/fancydialogs/fd-api/src/main/java/com/fancyinnovations/fancydialogs/api/data/inputs/DialogCheckbox.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.fancyinnovations.fancydialogs.api.data.inputs;
22

3+
import java.util.Map;
4+
35
public class DialogCheckbox extends DialogInput {
46

57
private final boolean initial;
68

7-
public DialogCheckbox(String key, String label, int order, boolean initial) {
8-
super(key, label, order);
9+
public DialogCheckbox(String key, String label, int order, boolean initial, Map<String, String> requirements) {
10+
super(key, label, order, requirements);
911
this.initial = initial;
1012
}
1113

plugins/fancydialogs/fd-api/src/main/java/com/fancyinnovations/fancydialogs/api/data/inputs/DialogInput.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package com.fancyinnovations.fancydialogs.api.data.inputs;
22

3+
import java.util.Map;
4+
35
public abstract class DialogInput {
46

57
protected final String key;
68
protected final String label;
79
protected final int order;
10+
protected final Map<String, String> requirements;
811

9-
public DialogInput(String key, String label, int order) {
12+
public DialogInput(String key, String label, int order, Map<String, String> requirements) {
1013
this.key = key;
1114
this.label = label;
1215
this.order = order;
16+
this.requirements = Map.copyOf(requirements);
1317
}
1418

1519
public String getKey() {
@@ -23,4 +27,6 @@ public String getLabel() {
2327
public int getOrder() {
2428
return order;
2529
}
30+
31+
public Map<String, String> getRequirements() { return requirements; }
2632
}

plugins/fancydialogs/fd-api/src/main/java/com/fancyinnovations/fancydialogs/api/data/inputs/DialogSelect.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.fancyinnovations.fancydialogs.api.data.inputs;
22

33
import java.util.List;
4+
import java.util.Map;
45

56
public class DialogSelect extends DialogInput {
67

78
private final List<Entry> options;
89

9-
public DialogSelect(String key, String label, int order, List<Entry> options) {
10-
super(key, label, order);
10+
public DialogSelect(String key, String label, int order, List<Entry> options, Map<String, String> requirements) {
11+
super(key, label, order, requirements);
1112
this.options = options;
1213
}
1314

plugins/fancydialogs/fd-api/src/main/java/com/fancyinnovations/fancydialogs/api/data/inputs/DialogTextField.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.fancyinnovations.fancydialogs.api.data.inputs;
22

3+
import java.util.Map;
4+
35
public class DialogTextField extends DialogInput {
46

57
private final String placeholder;
68
private final int maxLength;
79
private final int maxLines;
810

9-
public DialogTextField(String key, String label, int order, String placeholder, int maxLength, int maxLines) {
10-
super(key, label, order);
11+
public DialogTextField(String key, String label, int order, String placeholder, int maxLength, int maxLines, Map<String, String> requirements) {
12+
super(key, label, order, requirements);
1113
this.placeholder = placeholder;
1214
this.maxLength = maxLength;
1315
this.maxLines = maxLines;

plugins/fancydialogs/fd-api/src/main/java/com/fancyinnovations/fancydialogs/api/dialogs/ConfirmationDialog.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ private void buildDialog() {
9999
confirmText,
100100
List.of(
101101
new DialogButton.DialogAction("confirm", "")
102-
)
102+
),
103+
Map.of("", "")
103104
);
104105
this.confirmButtonId = confirmBtn.id();
105106

@@ -108,14 +109,15 @@ private void buildDialog() {
108109
cancelText,
109110
List.of(
110111
new DialogButton.DialogAction("cancel", "")
111-
)
112+
),
113+
Map.of("", "")
112114
);
113115
this.cancelButtonId = cancelBtn.id();
114116

115117
List<DialogTextField> textFields = null;
116118
if (expectedUserInput != null && !expectedUserInput.isEmpty()) {
117119
textFields = List.of(
118-
new DialogTextField("confirmation_user_input", "Type '" + expectedUserInput + "' to confirm", 0, "", expectedUserInput.length(), 1)
120+
new DialogTextField("confirmation_user_input", "Type '" + expectedUserInput + "' to confirm", 0, "", expectedUserInput.length(), 1, Map.of("type", ""))
119121
);
120122
}
121123

plugins/fancydialogs/src/main/java/com/fancyinnovations/fancydialogs/dialog/DialogImpl.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import de.oliver.fancysitula.factories.FancySitula;
2727
import org.bukkit.entity.Player;
2828
import org.lushplugins.chatcolorhandler.ChatColorHandler;
29+
import org.lushplugins.chatcolorhandler.parsers.Parser;
2930
import org.lushplugins.chatcolorhandler.parsers.ParserTypes;
3031

3132
import java.util.ArrayList;
@@ -54,6 +55,29 @@ private String replaceArgs(String text, String[] args) {
5455
return result;
5556
}
5657

58+
private boolean checkPerm(Player player, String perm) {
59+
if (perm == null) {
60+
return true;
61+
}
62+
if (!perm.equals("") && !player.hasPermission(perm)) {
63+
return false;
64+
}
65+
return true;
66+
}
67+
68+
private boolean checkRequirements(Player player, Map<String, String> requirements) {
69+
if (requirements == null) { return true; }
70+
if (requirements.get("type") == null) { return true; }
71+
if (requirements.get("type").equals("permission")) {
72+
return checkPerm(player, requirements.get("permission"));
73+
}
74+
if (requirements.get("type").equals("stringMatch")) {
75+
if (requirements.get("input") == null || requirements.get("output") == null) { return true; }
76+
return ChatColorHandler.translate(requirements.get("input"), player, ParserTypes.placeholder()).equals(ChatColorHandler.translate(requirements.get("output"), player, ParserTypes.placeholder()));
77+
}
78+
return true;
79+
}
80+
5781
private FS_Dialog buildForPlayer(Player player, String[] args) {
5882
List<FS_DialogBody> body = new ArrayList<>();
5983
for (DialogBodyData bodyData : data.body()) {
@@ -73,6 +97,7 @@ private FS_Dialog buildForPlayer(Player player, String[] args) {
7397
List<FS_DialogInput> inputs = new ArrayList<>();
7498
if (data.inputs() != null) {
7599
for (DialogInput input : data.inputs().all()) {
100+
if (!checkRequirements(player, input.getRequirements())) { continue; }
76101
FS_DialogInputControl control = null;
77102
if (input instanceof DialogTextField textField) {
78103
String label = replaceArgs(textField.getLabel(), args);
@@ -122,6 +147,7 @@ private FS_Dialog buildForPlayer(Player player, String[] args) {
122147
List<FS_DialogActionButton> actions = new ArrayList<>();
123148
for (DialogButton button : data.buttons()) {
124149
FS_DialogActionButtonAction buttonAction;
150+
if (!checkRequirements(player, button.requirements())) { continue; }
125151

126152
if (button.actions().size() == 1 &&
127153
button.actions().get(0).name().equals("copy_to_clipboard")) {

0 commit comments

Comments
 (0)