Fix dialog preconditions (#12895)

This commit is contained in:
Lulu13022002
2025-07-26 21:35:19 +02:00
committed by GitHub
parent 6bb9dc6b73
commit 840dd9e1f8
24 changed files with 317 additions and 378 deletions
@@ -10,6 +10,7 @@ import net.kyori.adventure.dialog.DialogLike;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.KeyPattern;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.jetbrains.annotations.ApiStatus;
@@ -44,4 +45,22 @@ public interface Dialog extends Keyed, DialogLike {
final Registry<Dialog> registry = RegistryAccess.registryAccess().getRegistry(RegistryKey.DIALOG);
return registry.getOrThrow(Key.key(Key.MINECRAFT_NAMESPACE, value));
}
/**
* @deprecated use {@link Registry#getKey(Keyed)}, {@link io.papermc.paper.registry.RegistryAccess#getRegistry(io.papermc.paper.registry.RegistryKey)},
* and {@link io.papermc.paper.registry.RegistryKey#DIALOG}. Dialogs can exist without a key.
*/
@Deprecated(since = "1.21.8", forRemoval = true)
@Override
NamespacedKey getKey();
/**
* @deprecated use {@link Registry#getKey(Keyed)}, {@link io.papermc.paper.registry.RegistryAccess#getRegistry(io.papermc.paper.registry.RegistryKey)},
* and {@link io.papermc.paper.registry.RegistryKey#DIALOG}. Dialogs can exist without a key.
*/
@Deprecated(since = "1.21.8", forRemoval = true)
@Override
default Key key() {
return Keyed.super.key();
}
}
@@ -63,9 +63,9 @@ public interface DialogRegistryEntry {
Builder base(DialogBase dialogBase);
/**
* Sets the specialty dialog for this entry.
* Sets the type of dialog for this entry.
*
* @param dialogType the specialty dialog
* @param dialogType the type of dialog
* @return this builder instance
* @see DialogRegistryEntry#type()
*/
@@ -45,7 +45,7 @@ public sealed interface DialogAction permits DialogAction.CommandTemplateAction,
*
* @param id the identifier of the custom action
* @param additions additional data to be sent with the action, or null if not needed
* @return a new custom all action instance
* @return a new custom click action instance
*/
@Contract(pure = true, value = "_, _ -> new")
static CustomClickAction customClick(final Key id, final @Nullable BinaryTagHolder additions) {
@@ -57,7 +57,7 @@ public sealed interface DialogAction permits DialogAction.CommandTemplateAction,
*
* @param callback the custom action to execute
* @param options the options for the custom action
* @return a new custom all action instance
* @return a new custom click action instance
*/
@Contract(pure = true, value = "_, _ -> new")
static CustomClickAction customClick(final DialogActionCallback callback, final ClickCallback.Options options) {
@@ -94,7 +94,6 @@ public sealed interface DialogAction permits DialogAction.CommandTemplateAction,
ClickEvent value();
}
/**
* Represents an action that executes a custom action with additional data.
*/
@@ -1,8 +1,8 @@
/**
* This package contains classes related to dialog actions in the Paper API.
*/
@ApiStatus.Experimental
@NullMarked
@ApiStatus.Experimental
package io.papermc.paper.registry.data.dialog.action;
import org.jetbrains.annotations.ApiStatus;
@@ -58,7 +58,7 @@ public sealed interface DialogBody permits ItemDialogBody, PlainMessageDialogBod
* @param contents the contents of the message
* @return a new plain message body instance
*/
@Contract(pure = true, value = "_, -> new")
@Contract(pure = true, value = "_ -> new")
static PlainMessageDialogBody plainMessage(final Component contents) {
return DialogInstancesProvider.instance().plainMessageDialogBody(contents);
}
@@ -1,8 +1,8 @@
/**
* This package contains classes related to dialog bodies in the Paper API.
*/
@ApiStatus.Experimental
@NullMarked
@ApiStatus.Experimental
package io.papermc.paper.registry.data.dialog.body;
import org.jetbrains.annotations.ApiStatus;
@@ -80,7 +80,7 @@ public sealed interface DialogInput permits BooleanDialogInput, NumberRangeDialo
* @param end the end of the range
* @return a new builder instance
*/
@Contract(value = "_, _, _, _ -> new", pure = true)
@Contract(pure = true, value = "_, _, _, _ -> new")
static NumberRangeDialogInput.Builder numberRange(final String key, final Component label, final float start, final float end) {
return DialogInstancesProvider.instance().numberRangeBuilder(key, label, start, end);
}
@@ -114,7 +114,7 @@ public sealed interface DialogInput permits BooleanDialogInput, NumberRangeDialo
* @param entries the list of options for the input
* @return a new builder instance
*/
@Contract(value = "_, _, _ -> new", pure = true)
@Contract(pure = true, value = "_, _, _ -> new")
static SingleOptionDialogInput.Builder singleOption(final String key, final Component label, final List<SingleOptionDialogInput.OptionEntry> entries) {
return DialogInstancesProvider.instance().singleOptionBuilder(key, label, entries);
}
@@ -128,7 +128,7 @@ public sealed interface DialogInput permits BooleanDialogInput, NumberRangeDialo
* @param labelVisible whether the label should be visible
* @param initial the initial value of the input
* @param maxLength the maximum length of the input
* @param multilineOptions the multiline options
* @param multilineOptions the multiline options, or null if not set
* @return a new text dialog input instance
*/
@Contract(pure = true, value = "_, _, _, _, _, _, _ -> new")
@@ -138,7 +138,7 @@ public sealed interface DialogInput permits BooleanDialogInput, NumberRangeDialo
final Component label,
final boolean labelVisible,
final String initial,
final @Range(from = 1, to = Integer.MAX_VALUE) int maxLength,
final @Positive int maxLength,
final TextDialogInput.@Nullable MultilineOptions multilineOptions
) {
return text(key, label).width(width).labelVisible(labelVisible).initial(initial).maxLength(maxLength).multiline(multilineOptions).build();
@@ -162,6 +162,6 @@ public sealed interface DialogInput permits BooleanDialogInput, NumberRangeDialo
*
* @return the key
*/
@Contract(pure = true, value = " -> new")
@Contract(pure = true)
String key();
}
@@ -4,8 +4,8 @@ import net.kyori.adventure.text.Component;
import org.checkerframework.checker.index.qual.Positive;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import org.jspecify.annotations.Nullable;
/**
* A number range dialog input.
@@ -103,7 +103,7 @@ public non-sealed interface NumberRangeDialogInput extends DialogInput {
* @param initial the initial value, or null if not set
* @return this builder
*/
@Contract(value = "_ -> this", pure = true)
@Contract(value = "_ -> this", mutates = "this")
Builder initial(@Nullable Float initial);
/**
@@ -112,7 +112,7 @@ public non-sealed interface NumberRangeDialogInput extends DialogInput {
* @param step the step size, or null if not set
* @return this builder
*/
@Contract(value = "_ -> this", pure = true)
@Contract(value = "_ -> this", mutates = "this")
Builder step(@Positive @Nullable Float step);
/**
@@ -5,9 +5,9 @@ import java.util.List;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import org.jetbrains.annotations.Unmodifiable;
import org.jspecify.annotations.Nullable;
/**
* A single option dialog input.
@@ -2,6 +2,7 @@ package io.papermc.paper.registry.data.dialog.input;
import io.papermc.paper.registry.data.dialog.DialogInstancesProvider;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.index.qual.Positive;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Range;
@@ -52,7 +53,7 @@ public non-sealed interface TextDialogInput extends DialogInput {
* @return the label format
*/
@Contract(pure = true)
@Range(from = 1, to = Integer.MAX_VALUE) int maxLength();
@Positive int maxLength();
/**
* The multiline options for the input, or null if not set.
@@ -76,7 +77,7 @@ public non-sealed interface TextDialogInput extends DialogInput {
* @return a new MultilineOptions instance
*/
static MultilineOptions create(
final @Range(from = 1, to = Integer.MAX_VALUE) @Nullable Integer maxLines,
final @Positive @Nullable Integer maxLines,
final @Range(from = 1, to = 512) @Nullable Integer height
) {
return DialogInstancesProvider.instance().multilineOptions(maxLines, height);
@@ -88,7 +89,7 @@ public non-sealed interface TextDialogInput extends DialogInput {
* @return the maximum number of lines, or null if not set
*/
@Contract(pure = true)
@Nullable Integer maxLines();
@Positive @Nullable Integer maxLines();
/**
* Gets the height of the input.
@@ -96,7 +97,7 @@ public non-sealed interface TextDialogInput extends DialogInput {
* @return the height of the input, or null if not set
*/
@Contract(pure = true)
@Nullable Integer height();
@Range(from = 1, to = 512) @Nullable Integer height();
}
/**
@@ -140,12 +141,12 @@ public non-sealed interface TextDialogInput extends DialogInput {
* @return this builder
*/
@Contract(value = "_ -> this", mutates = "this")
Builder maxLength(final @Range(from = 1, to = Integer.MAX_VALUE) int maxLength);
Builder maxLength(final @Positive int maxLength);
/**
* Sets the multiline options for the input.
*
* @param multiline the multiline options
* @param multiline the multiline options, or null if not set
* @return this builder
*/
@Contract(value = "_ -> this", mutates = "this")
@@ -1,8 +1,8 @@
/**
* Dialog inputs for Paper API.
*/
@ApiStatus.Experimental
@NullMarked
@ApiStatus.Experimental
package io.papermc.paper.registry.data.dialog.input;
import org.jetbrains.annotations.ApiStatus;
@@ -3,6 +3,7 @@ package io.papermc.paper.registry.data.dialog.type;
import io.papermc.paper.dialog.Dialog;
import io.papermc.paper.registry.data.dialog.ActionButton;
import io.papermc.paper.registry.set.RegistrySet;
import org.checkerframework.checker.index.qual.Positive;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Range;
@@ -37,7 +38,7 @@ public non-sealed interface DialogListType extends DialogType {
* @return the number of columns
*/
@Contract(pure = true)
@Range(from = 1, to = Integer.MAX_VALUE) int columns();
@Positive int columns();
/**
* Returns the width of each button in the dialog list.
@@ -69,7 +70,7 @@ public non-sealed interface DialogListType extends DialogType {
* @return the builder
*/
@Contract(value = "_ -> this", mutates = "this")
Builder columns(final @Range(from = 1, to = Integer.MAX_VALUE) int columns);
Builder columns(final @Positive int columns);
/**
* Sets the width of each button in the dialog list.
@@ -5,6 +5,7 @@ import io.papermc.paper.registry.data.dialog.ActionButton;
import io.papermc.paper.registry.data.dialog.DialogInstancesProvider;
import io.papermc.paper.registry.set.RegistrySet;
import java.util.List;
import org.checkerframework.checker.index.qual.Positive;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Range;
import org.jspecify.annotations.Nullable;
@@ -39,7 +40,7 @@ public sealed interface DialogType permits ConfirmationType, DialogListType, Mul
static DialogListType dialogList(
final RegistrySet<Dialog> dialogs,
final @Nullable ActionButton exitAction,
final @Range(from = 1, to = Integer.MAX_VALUE) int columns,
final @Positive int columns,
final @Range(from = 1, to = 1024) int buttonWidth
) {
return dialogList(dialogs).exitAction(exitAction).columns(columns).buttonWidth(buttonWidth).build();
@@ -68,7 +69,7 @@ public sealed interface DialogType permits ConfirmationType, DialogListType, Mul
static MultiActionType multiAction(
final List<ActionButton> actions,
final @Nullable ActionButton exitAction,
final @Range(from = 1, to = Integer.MAX_VALUE) int columns
final @Positive int columns
) {
return multiAction(actions).exitAction(exitAction).columns(columns).build();
}
@@ -116,7 +117,7 @@ public sealed interface DialogType permits ConfirmationType, DialogListType, Mul
@Contract(value = "_, _, _ -> new", pure = true)
static ServerLinksType serverLinks(
final @Nullable ActionButton exitAction,
final @Range(from = 1, to = Integer.MAX_VALUE) int columns,
final @Positive int columns,
final @Range(from = 1, to = 1024) int buttonWidth
) {
return DialogInstancesProvider.instance().serverLinks(exitAction, columns, buttonWidth);
@@ -2,11 +2,11 @@ package io.papermc.paper.registry.data.dialog.type;
import io.papermc.paper.registry.data.dialog.ActionButton;
import java.util.List;
import org.checkerframework.checker.index.qual.Positive;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import org.jetbrains.annotations.Unmodifiable;
import org.jspecify.annotations.Nullable;
/**
* Represents a dialog that allows multiple actions to be performed.
@@ -38,7 +38,7 @@ public non-sealed interface MultiActionType extends DialogType {
* @return the number of columns
*/
@Contract(pure = true)
@Range(from = 1, to = Integer.MAX_VALUE) int columns();
@Positive int columns();
/**
* A builder for creating a multi-action dialog.
@@ -62,7 +62,7 @@ public non-sealed interface MultiActionType extends DialogType {
* @return the builder
*/
@Contract(value = "_ -> this", mutates = "this")
Builder columns(final @Range(from = 1, to = Integer.MAX_VALUE) int columns);
Builder columns(final @Positive int columns);
/**
* Builds the multi-action dialog.
@@ -2,6 +2,7 @@ package io.papermc.paper.registry.data.dialog.type;
import io.papermc.paper.registry.data.dialog.ActionButton;
import org.checkerframework.checker.index.qual.Positive;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Range;
@@ -28,7 +29,7 @@ public non-sealed interface ServerLinksType extends DialogType {
* @return the number of columns
*/
@Contract(pure = true)
@Range(from = 1, to = Integer.MAX_VALUE) int columns();
@Positive int columns();
/**
* Returns the width of each button in the server links dialog.
@@ -2,6 +2,8 @@
* Dialog types for the Paper API.
*/
@NullMarked
@ApiStatus.Experimental
package io.papermc.paper.registry.data.dialog.type;
import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.NullMarked;
@@ -30,7 +30,7 @@ public class PaperDialogRegistryEntry implements DialogRegistryEntry {
final CommonDialogData common = internal.common();
this.dialogBase = conversions.convert(common, PaperDialogCodecs.DIALOG_BASE_CODEC, CommonDialogData.MAP_CODEC.codec());
this.dialogType = PaperDialogs.extractSpecialty(internal, conversions);
this.dialogType = PaperDialogs.extractType(internal, conversions);
}
@Override
@@ -40,7 +40,7 @@ public class PaperDialogRegistryEntry implements DialogRegistryEntry {
@Override
public DialogType type() {
return asConfigured(this.dialogType, "dialogSpecialty");
return asConfigured(this.dialogType, "dialogType");
}
public static final class PaperBuilder extends PaperDialogRegistryEntry implements Builder, PaperRegistryBuilder<Dialog, io.papermc.paper.dialog.Dialog> {
@@ -62,7 +62,7 @@ public class PaperDialogRegistryEntry implements DialogRegistryEntry {
@Override
public Builder type(final DialogType dialogType) {
this.dialogType = asArgument(dialogType, "dialogSpecialty");
this.dialogType = asArgument(dialogType, "dialogType");
return this;
}
@@ -26,7 +26,7 @@ public final class PaperDialogs {
private PaperDialogs() {
}
public static DialogType extractSpecialty(final Dialog nmsDialog, final Conversions conversions) {
public static DialogType extractType(final Dialog nmsDialog, final Conversions conversions) {
final Function<net.minecraft.server.dialog.ActionButton, ActionButton> convertButton = button -> conversions.convert(button, PaperDialogCodecs.ACTION_BUTTON_CODEC, net.minecraft.server.dialog.ActionButton.CODEC);
return switch (nmsDialog) {
case final ConfirmationDialog conf ->
@@ -16,8 +16,8 @@ public record CustomClickActionImpl(Key id, @Nullable BinaryTagHolder additions)
try {
final Tag tag = additions.get(PaperAdventure.NBT_CODEC);
Preconditions.checkArgument(tag instanceof CompoundTag, "Additions must be a compound tag");
} catch (final CommandSyntaxException e) {
throw new RuntimeException(e);
} catch (final CommandSyntaxException ex) {
throw new RuntimeException(ex);
}
}
}
@@ -6,6 +6,6 @@ import net.kyori.adventure.text.event.ClickEvent;
public record StaticActionImpl(ClickEvent value) implements DialogAction.StaticAction {
public StaticActionImpl {
Preconditions.checkArgument(value.action().readable(), "action must be readable");
Preconditions.checkArgument(value.action().readable() || value.action() == ClickEvent.Action.SHOW_DIALOG, "action must be readable or show_dialog");
}
}
@@ -5,7 +5,7 @@ import java.util.List;
import net.kyori.adventure.text.Component;
import net.minecraft.commands.functions.StringTemplate;
import net.minecraft.server.dialog.body.PlainMessage;
import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.Nullable;
public record SingleOptionDialogInputImpl(
String key,
@@ -32,9 +32,10 @@ public record SingleOptionDialogInputImpl(
public BuilderImpl(final String key, final List<OptionEntry> entries, final Component label) {
Preconditions.checkArgument(StringTemplate.isValidVariableName(key), "key must be a valid input name");
this.key = key;
Preconditions.checkArgument(!entries.isEmpty(), "entries must not be empty");
Preconditions.checkArgument(entries.stream().filter(OptionEntry::initial).count() <= 1, "only 1 option can be initially selected");
this.key = key;
this.entries = entries;
this.label = label;
}
@@ -26,7 +26,7 @@ public record MultiActionTypeImpl(
}
@Override
public Builder exitAction(final @org.jetbrains.annotations.Nullable ActionButton exitAction) {
public Builder exitAction(final @Nullable ActionButton exitAction) {
this.exitAction = exitAction;
return this;
}
@@ -1,13 +1,12 @@
package io.papermc.paper.registry.data.dialog.type;
import io.papermc.paper.registry.data.dialog.ActionButton;
import net.minecraft.server.dialog.CommonButtonData;
import static net.kyori.adventure.text.Component.translatable;
public record NoticeTypeImpl(ActionButton action) implements NoticeType {
public static final ActionButton DEFAULT_ACTION = ActionButton.builder(translatable("gui.ok")).width(CommonButtonData.DEFAULT_WIDTH).build();
public static final ActionButton DEFAULT_ACTION = ActionButton.builder(translatable("gui.ok")).build();
public NoticeTypeImpl() {
this(DEFAULT_ACTION);
File diff suppressed because it is too large Load Diff