Skip to content

Commit a124621

Browse files
committed
Add another builder
1 parent ccd137d commit a124621

File tree

6 files changed

+163
-7
lines changed

6 files changed

+163
-7
lines changed

paper-api/src/main/java/io/papermc/paper/registry/data/dialog/DialogBase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ static DialogBase create(
3333
final boolean canCloseWithEscape,
3434
final boolean pause,
3535
final DialogAfterAction afterAction,
36-
final List<DialogBody> body,
36+
final List<? extends DialogBody> body,
3737
final List<DialogInput> inputs
3838
) {
39-
return new DialogBaseImpl(title, externalTitle, canCloseWithEscape, pause, afterAction, body, inputs);
39+
return new DialogBaseImpl(title, externalTitle, canCloseWithEscape, pause, afterAction, List.copyOf(body), inputs);
4040
}
4141

4242
/**
@@ -186,7 +186,7 @@ sealed interface Builder permits DialogBaseImpl.BuilderImpl {
186186
* @return this builder
187187
*/
188188
@Contract(value = "_ -> this", mutates = "this")
189-
Builder body(List<DialogBody> body);
189+
Builder body(List<? extends DialogBody> body);
190190

191191
/**
192192
* Sets the inputs of the dialog.

paper-api/src/main/java/io/papermc/paper/registry/data/dialog/DialogBaseImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public BuilderImpl afterAction(final DialogAfterAction afterAction) {
6262
}
6363

6464
@Override
65-
public BuilderImpl body(final List<DialogBody> body) {
65+
public BuilderImpl body(final List<? extends DialogBody> body) {
6666
this.body = List.copyOf(body);
6767
return this;
6868
}

paper-api/src/main/java/io/papermc/paper/registry/data/dialog/input/type/NumberRangeDialogInputConfig.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@
99
*/
1010
public sealed interface NumberRangeDialogInputConfig extends DialogInputConfig permits NumberRangeDialogInputConfigImpl {
1111

12+
/**
13+
* Creates a new builder for a number range dialog input configuration.
14+
*
15+
* @param label the label for the input
16+
* @param start the start of the range
17+
* @param end the end of the range
18+
* @return a new builder instance
19+
*/
20+
@Contract(value = "_, _, _ -> new", pure = true)
21+
static Builder builder(final Component label, final float start, final float end) {
22+
return new NumberRangeDialogInputConfigImpl.BuilderImpl(label, start, end);
23+
}
24+
1225
/**
1326
* The width of the input.
1427
*
@@ -65,4 +78,56 @@ public sealed interface NumberRangeDialogInputConfig extends DialogInputConfig p
6578
*/
6679
@Contract(pure = true)
6780
@Nullable Float step();
81+
82+
/**
83+
* A builder for creating instances of {@link NumberRangeDialogInputConfig}.
84+
*/
85+
sealed interface Builder permits NumberRangeDialogInputConfigImpl.BuilderImpl {
86+
87+
/**
88+
* Sets the width of the input.
89+
*
90+
* @param width the width
91+
* @return this builder
92+
*/
93+
@Contract(value = "_ -> this", mutates = "this")
94+
Builder width(int width);
95+
96+
/**
97+
* Sets the format for the label.
98+
* <p>Example: {@code "%s: %s"} or {@code "options.generic_value"}</p>
99+
*
100+
* @param labelFormat the label format
101+
* @return this builder
102+
*/
103+
@Contract(value = "_ -> this", mutates = "this")
104+
Builder labelFormat(String labelFormat);
105+
106+
107+
/**
108+
* Sets the initial value for the input.
109+
*
110+
* @param initial the initial value, or null if not set
111+
* @return this builder
112+
*/
113+
@Contract(value = "_ -> this", pure = true)
114+
Builder initial(@Nullable Float initial);
115+
116+
/**
117+
* Sets the step of the range.
118+
*
119+
* @param step the step size, or null if not set
120+
* @return this builder
121+
*/
122+
@Contract(value = "_ -> this", pure = true)
123+
Builder step(@Nullable Float step);
124+
125+
/**
126+
* Builds the instance with the configured values.
127+
*
128+
* @return a new instance
129+
*/
130+
@Contract(pure = true, value = "-> new")
131+
NumberRangeDialogInputConfig build();
132+
}
68133
}

paper-api/src/main/java/io/papermc/paper/registry/data/dialog/input/type/NumberRangeDialogInputConfigImpl.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,59 @@
33
import net.kyori.adventure.text.Component;
44
import org.jspecify.annotations.Nullable;
55

6-
record NumberRangeDialogInputConfigImpl(int width, Component label, String labelFormat, float start, float end, @Nullable Float initial, @Nullable Float step) implements NumberRangeDialogInputConfig {
6+
record NumberRangeDialogInputConfigImpl(
7+
int width,
8+
Component label,
9+
String labelFormat,
10+
float start,
11+
float end,
12+
@Nullable Float initial,
13+
@Nullable Float step
14+
) implements NumberRangeDialogInputConfig {
15+
16+
static final class BuilderImpl implements NumberRangeDialogInputConfig.Builder {
17+
18+
private final Component label;
19+
private final float start;
20+
private final float end;
21+
private int width = 200;
22+
private String labelFormat = "%s: %s";
23+
private @Nullable Float initial = null;
24+
private @Nullable Float step = null;
25+
26+
BuilderImpl(final Component label, final float start, final float end) {
27+
this.label = label;
28+
this.start = start;
29+
this.end = end;
30+
}
31+
32+
@Override
33+
public BuilderImpl width(final int width) {
34+
this.width = width;
35+
return this;
36+
}
37+
38+
@Override
39+
public BuilderImpl labelFormat(final String labelFormat) {
40+
this.labelFormat = labelFormat;
41+
return this;
42+
}
43+
44+
@Override
45+
public BuilderImpl initial(final @Nullable Float initial) {
46+
this.initial = initial;
47+
return this;
48+
}
49+
50+
@Override
51+
public BuilderImpl step(final @Nullable Float step) {
52+
this.step = step;
53+
return this;
54+
}
55+
56+
@Override
57+
public NumberRangeDialogInputConfig build() {
58+
return new NumberRangeDialogInputConfigImpl(this.width, this.label, this.labelFormat, this.start, this.end, this.initial, this.step);
59+
}
60+
}
761
}

paper-api/src/main/java/org/bukkit/entity/Player.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3927,7 +3927,15 @@ default PlayerGiveResult give(final Collection<ItemStack> items) {
39273927
*/
39283928
void setDeathScreenScore(int score);
39293929

3930+
/**
3931+
* @deprecated JUST FOR DEV TESTING
3932+
*/
3933+
@Deprecated(forRemoval = true)
39303934
void tempShowDialog(Dialog dialog);
39313935

3936+
/**
3937+
* @deprecated JUST FOR DEV TESTING
3938+
*/
3939+
@Deprecated(forRemoval = true)
39323940
void tempClearDialog();
39333941
}
Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,51 @@
11
package io.papermc.testplugin;
22

3+
import io.papermc.paper.dialog.Dialog;
4+
import io.papermc.paper.event.player.ChatEvent;
5+
import io.papermc.paper.registry.data.dialog.ActionButton;
6+
import io.papermc.paper.registry.data.dialog.DialogBase;
7+
import io.papermc.paper.registry.data.dialog.action.DialogAction;
8+
import io.papermc.paper.registry.data.dialog.body.DialogBody;
9+
import io.papermc.paper.registry.data.dialog.specialty.DialogSpecialty;
310
import java.net.URI;
4-
import net.kyori.adventure.text.Component;
11+
import java.util.List;
512
import net.kyori.adventure.text.TextComponent;
13+
import net.kyori.adventure.text.event.ClickEvent;
614
import net.kyori.adventure.text.format.NamedTextColor;
15+
import org.bukkit.event.EventHandler;
716
import org.bukkit.event.Listener;
817
import org.bukkit.plugin.java.JavaPlugin;
918

19+
import static net.kyori.adventure.text.Component.text;
20+
1021
public final class TestPlugin extends JavaPlugin implements Listener {
1122

1223
@Override
1324
public void onEnable() {
1425
this.getServer().getPluginManager().registerEvents(this, this);
1526

16-
final TextComponent display = Component.text("Test Link", NamedTextColor.GOLD);
27+
final TextComponent display = text("Test Link", NamedTextColor.GOLD);
1728
final URI link = URI.create("https://www.google.com");
1829
this.getServer().getServerLinks().addLink(display, link);
1930

2031
// io.papermc.testplugin.brigtests.Registration.registerViaOnEnable(this);
2132
}
33+
34+
@EventHandler
35+
public void onChat(final ChatEvent event) {
36+
final Dialog dialog = Dialog.create(factory -> factory.empty()
37+
.dialogBase(DialogBase
38+
.builder(text("Test Dialog"))
39+
.body(List.of(DialogBody.plainMessage(text("Teleport 10 blocks up"), 100)))
40+
.build()
41+
).dialogSpecialty(DialogSpecialty.confirmation(
42+
ActionButton.create(
43+
text("TELEPORT", NamedTextColor.GREEN),
44+
null,
45+
100,
46+
DialogAction.staticAction(ClickEvent.runCommand("tp @s ~ ~10 ~"))),
47+
ActionButton.create(text("CANCEL", NamedTextColor.RED), null, 100, null)
48+
)));
49+
event.getPlayer().tempShowDialog(dialog);
50+
}
2251
}

0 commit comments

Comments
 (0)