Refactor dialog actions
This commit is contained in:
parent
23ba5141f1
commit
cd1ceb4c31
@ -30,7 +30,7 @@ Sample Plugin
|
||||
new TextInput( "second", new ComponentBuilder( "Second" ).build() )
|
||||
)
|
||||
) )
|
||||
.action( new DynamicAction( new ComponentBuilder( "Submit Button" ).build(), new Custom( "customform" ) ) );
|
||||
.action( new ActionButton( new ComponentBuilder( "Submit Button" ).build(), new CustomClickAction( "customform" ) ) );
|
||||
|
||||
player.sendMessage( new ComponentBuilder( "click me" ).event( new ShowDialogClickEvent( notice ) ).build() );
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
import net.md_5.bungee.api.dialog.action.DialogAction;
|
||||
import net.md_5.bungee.api.dialog.action.ActionButton;
|
||||
|
||||
/**
|
||||
* Represents a simple dialog with text and two actions at the bottom (default:
|
||||
@ -26,11 +26,11 @@ public final class ConfirmationDialog implements Dialog
|
||||
/**
|
||||
* The "yes" click action / bottom (appears on the left).
|
||||
*/
|
||||
private DialogAction yes;
|
||||
private ActionButton yes;
|
||||
/**
|
||||
* The "no" click action / bottom (appears on the right).
|
||||
*/
|
||||
private DialogAction no;
|
||||
private ActionButton no;
|
||||
|
||||
public ConfirmationDialog(@NonNull DialogBase base)
|
||||
{
|
||||
|
@ -9,7 +9,7 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
import net.md_5.bungee.api.dialog.action.DialogAction;
|
||||
import net.md_5.bungee.api.dialog.action.ActionButton;
|
||||
|
||||
/**
|
||||
* Represents a dialog which contains buttons that link to other dialogs.
|
||||
@ -29,10 +29,10 @@ public final class DialogListDialog implements Dialog
|
||||
*/
|
||||
private List<Dialog> dialogs;
|
||||
/**
|
||||
* The {@link DialogAction} activated when the dialog is exited.
|
||||
* The {@link ActionButton} activated when the dialog is exited.
|
||||
*/
|
||||
@SerializedName("exit_action")
|
||||
private DialogAction exitAction;
|
||||
private ActionButton exitAction;
|
||||
/**
|
||||
* The number of columns for the dialog buttons (default: 2).
|
||||
*/
|
||||
@ -48,7 +48,7 @@ public final class DialogListDialog implements Dialog
|
||||
this( base, Arrays.asList( dialogs ), null, null, null );
|
||||
}
|
||||
|
||||
public DialogListDialog(@NonNull DialogBase base, List<Dialog> dialogs, DialogAction exitAction, Integer columns, Integer buttonWidth)
|
||||
public DialogListDialog(@NonNull DialogBase base, List<Dialog> dialogs, ActionButton exitAction, Integer columns, Integer buttonWidth)
|
||||
{
|
||||
this.base = base;
|
||||
this.dialogs = dialogs;
|
||||
|
@ -9,7 +9,7 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
import net.md_5.bungee.api.dialog.action.DialogAction;
|
||||
import net.md_5.bungee.api.dialog.action.ActionButton;
|
||||
|
||||
/**
|
||||
* Represents a dialog with text a list of action buttons grouped into columns
|
||||
@ -29,23 +29,23 @@ public final class MultiActionDialog implements Dialog
|
||||
* The action buttons in the dialog. At least one must be provided.
|
||||
*/
|
||||
@NonNull
|
||||
private List<DialogAction> actions;
|
||||
private List<ActionButton> actions;
|
||||
/**
|
||||
* The number of columns for the dialog buttons (default: 2).
|
||||
*/
|
||||
private Integer columns;
|
||||
/**
|
||||
* The {@link DialogAction} activated when the dialog is exited.
|
||||
* The {@link ActionButton} activated when the dialog is exited.
|
||||
*/
|
||||
@SerializedName("exit_action")
|
||||
private DialogAction exitAction;
|
||||
private ActionButton exitAction;
|
||||
|
||||
public MultiActionDialog(@NonNull DialogBase base, @NonNull DialogAction... actions)
|
||||
public MultiActionDialog(@NonNull DialogBase base, @NonNull ActionButton... actions)
|
||||
{
|
||||
this( base, Arrays.asList( actions ), null, null );
|
||||
}
|
||||
|
||||
public MultiActionDialog(@NonNull DialogBase base, @NonNull List<DialogAction> actions, Integer columns, DialogAction exitAction)
|
||||
public MultiActionDialog(@NonNull DialogBase base, @NonNull List<ActionButton> actions, Integer columns, ActionButton exitAction)
|
||||
{
|
||||
Preconditions.checkArgument( !actions.isEmpty(), "At least one action must be provided" );
|
||||
|
||||
|
@ -6,7 +6,7 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
import net.md_5.bungee.api.dialog.action.DialogAction;
|
||||
import net.md_5.bungee.api.dialog.action.ActionButton;
|
||||
|
||||
/**
|
||||
* Represents a simple dialog with text and one action at the bottom (default:
|
||||
@ -26,7 +26,7 @@ public final class NoticeDialog implements Dialog
|
||||
/**
|
||||
* The "OK" action button for the dialog.
|
||||
*/
|
||||
private DialogAction action;
|
||||
private ActionButton action;
|
||||
|
||||
public NoticeDialog(DialogBase base)
|
||||
{
|
||||
|
@ -7,7 +7,7 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
import net.md_5.bungee.api.dialog.action.DialogAction;
|
||||
import net.md_5.bungee.api.dialog.action.ActionButton;
|
||||
|
||||
/**
|
||||
* Represents a dialog which shows the links configured/sent from the server.
|
||||
@ -23,15 +23,15 @@ public final class ServerLinksDialog implements Dialog
|
||||
@Accessors(fluent = false)
|
||||
private DialogBase base;
|
||||
/**
|
||||
* The optional {@link DialogAction} for this dialog.
|
||||
* The optional {@link ActionButton} for this dialog.
|
||||
*/
|
||||
@SerializedName("action")
|
||||
private DialogAction action;
|
||||
private ActionButton action;
|
||||
/**
|
||||
* The {@link DialogAction} activated when the dialog is exited.
|
||||
* The {@link ActionButton} activated when the dialog is exited.
|
||||
*/
|
||||
@SerializedName("exit_action")
|
||||
private DialogAction exitAction;
|
||||
private ActionButton exitAction;
|
||||
/**
|
||||
* The number of columns for the dialog buttons (default: 2).
|
||||
*/
|
||||
@ -47,7 +47,7 @@ public final class ServerLinksDialog implements Dialog
|
||||
this( base, null, null, null );
|
||||
}
|
||||
|
||||
public ServerLinksDialog(@NonNull DialogBase base, DialogAction action, Integer columns, Integer buttonWidth)
|
||||
public ServerLinksDialog(@NonNull DialogBase base, ActionButton action, Integer columns, Integer buttonWidth)
|
||||
{
|
||||
this.base = base;
|
||||
this.action = action;
|
||||
|
@ -0,0 +1,6 @@
|
||||
package net.md_5.bungee.api.dialog.action;
|
||||
|
||||
public interface Action
|
||||
{
|
||||
|
||||
}
|
@ -11,7 +11,7 @@ import net.md_5.bungee.api.chat.BaseComponent;
|
||||
*/
|
||||
@Data
|
||||
@Accessors(fluent = true)
|
||||
public abstract class DialogAction
|
||||
public class ActionButton
|
||||
{
|
||||
|
||||
/**
|
||||
@ -27,17 +27,23 @@ public abstract class DialogAction
|
||||
* The width of the button (default: 150, minimum: 1, maximum: 1024).
|
||||
*/
|
||||
private Integer width;
|
||||
/**
|
||||
* The action to take.
|
||||
*/
|
||||
@NonNull
|
||||
private Action action;
|
||||
|
||||
public DialogAction(@NonNull BaseComponent label, BaseComponent tooltip, Integer width)
|
||||
public ActionButton(@NonNull BaseComponent label, BaseComponent tooltip, Integer width, @NonNull Action action)
|
||||
{
|
||||
this.label = label;
|
||||
this.tooltip = tooltip;
|
||||
setWidth( width );
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public DialogAction(@NonNull BaseComponent label)
|
||||
public ActionButton(@NonNull BaseComponent label, @NonNull Action action)
|
||||
{
|
||||
this( label, null, null );
|
||||
this( label, null, null, action );
|
||||
}
|
||||
|
||||
public void setWidth(Integer width)
|
@ -1,8 +1,7 @@
|
||||
package net.md_5.bungee.api.dialog.dynamic;
|
||||
package net.md_5.bungee.api.dialog.action;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
@ -13,8 +12,7 @@ import lombok.experimental.Accessors;
|
||||
@Data
|
||||
@Accessors(fluent = true)
|
||||
@ToString(callSuper = true)
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class Custom extends DynamicType
|
||||
public class CustomClickAction implements Action
|
||||
{
|
||||
|
||||
/**
|
||||
@ -26,10 +24,4 @@ public class Custom extends DynamicType
|
||||
* Fields to be added to the submission payload.
|
||||
*/
|
||||
private JsonElement additions;
|
||||
|
||||
public Custom(@NonNull String id)
|
||||
{
|
||||
super( "dynamic/custom" );
|
||||
this.id = id;
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
package net.md_5.bungee.api.dialog.action;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.dialog.dynamic.DynamicType;
|
||||
|
||||
/**
|
||||
* Represents a static dialog action.
|
||||
*/
|
||||
@Data
|
||||
@Accessors(fluent = true)
|
||||
@ToString(callSuper = true)
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class DynamicAction extends DialogAction
|
||||
{
|
||||
|
||||
@NonNull
|
||||
private DynamicType action;
|
||||
|
||||
public DynamicAction(@NonNull BaseComponent label, BaseComponent tooltip, Integer width, @NonNull DynamicType action)
|
||||
{
|
||||
super( label, tooltip, width );
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public DynamicAction(@NonNull BaseComponent label, @NonNull DynamicType action)
|
||||
{
|
||||
super( label );
|
||||
this.action = action;
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package net.md_5.bungee.api.dialog.dynamic;
|
||||
package net.md_5.bungee.api.dialog.action;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
@ -13,8 +12,7 @@ import lombok.experimental.Accessors;
|
||||
@Data
|
||||
@Accessors(fluent = true)
|
||||
@ToString(callSuper = true)
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class RunCommand extends DynamicType
|
||||
public class RunCommandAction implements Action
|
||||
{
|
||||
|
||||
/**
|
||||
@ -24,10 +22,4 @@ public class RunCommand extends DynamicType
|
||||
*/
|
||||
@NonNull
|
||||
private String template;
|
||||
|
||||
public RunCommand(@NonNull String template)
|
||||
{
|
||||
super( "dynamic/run_command" );
|
||||
this.template = template;
|
||||
}
|
||||
}
|
@ -1,13 +1,10 @@
|
||||
package net.md_5.bungee.api.dialog.action;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
/**
|
||||
* Represents a static dialog action.
|
||||
@ -15,30 +12,9 @@ import org.jetbrains.annotations.ApiStatus;
|
||||
@Data
|
||||
@Accessors(fluent = true)
|
||||
@ToString(callSuper = true)
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class StaticAction extends DialogAction
|
||||
public class StaticAction implements Action
|
||||
{
|
||||
|
||||
@NonNull
|
||||
private ChatClickEventWrapper action;
|
||||
|
||||
public StaticAction(@NonNull BaseComponent label, BaseComponent tooltip, Integer width, @NonNull ClickEvent action)
|
||||
{
|
||||
super( label, tooltip, width );
|
||||
this.action = new ChatClickEventWrapper( action );
|
||||
}
|
||||
|
||||
public StaticAction(@NonNull BaseComponent label, @NonNull ClickEvent action)
|
||||
{
|
||||
super( label );
|
||||
this.action = new ChatClickEventWrapper( action );
|
||||
}
|
||||
|
||||
@Data
|
||||
@ApiStatus.Internal
|
||||
public static final class ChatClickEventWrapper
|
||||
{
|
||||
|
||||
private final ClickEvent event;
|
||||
}
|
||||
private ClickEvent clickEvent;
|
||||
}
|
||||
|
@ -1,22 +0,0 @@
|
||||
package net.md_5.bungee.api.dialog.dynamic;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
/**
|
||||
* Represents an action which may be taken on form dialog submission.
|
||||
*/
|
||||
@Data
|
||||
@Accessors(fluent = true)
|
||||
public class DynamicType
|
||||
{
|
||||
|
||||
/**
|
||||
* The internal submissions type.
|
||||
*/
|
||||
@NonNull
|
||||
@ApiStatus.Internal
|
||||
private final String type;
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
/**
|
||||
* Represents the various dynamic actions of dialogs.
|
||||
*/
|
||||
package net.md_5.bungee.api.dialog.dynamic;
|
@ -29,8 +29,9 @@ import net.md_5.bungee.api.chat.hover.content.ItemSerializer;
|
||||
import net.md_5.bungee.api.chat.hover.content.Text;
|
||||
import net.md_5.bungee.api.chat.hover.content.TextSerializer;
|
||||
import net.md_5.bungee.api.dialog.Dialog;
|
||||
import net.md_5.bungee.api.dialog.action.Action;
|
||||
import net.md_5.bungee.api.dialog.chat.ShowDialogClickEvent;
|
||||
import net.md_5.bungee.serializer.dialog.ChatClickEventWrapperSerializer;
|
||||
import net.md_5.bungee.serializer.dialog.DialogActionSerializer;
|
||||
import net.md_5.bungee.serializer.dialog.DialogSerializer;
|
||||
import net.md_5.bungee.serializer.dialog.ShowDialogClickEventSerializer;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
@ -68,7 +69,7 @@ public class VersionedComponentSerializer implements JsonDeserializer<BaseCompon
|
||||
// Dialogs
|
||||
registerTypeAdapter( Dialog.class, dialogSerializer ).
|
||||
registerTypeAdapter( ShowDialogClickEvent.class, new ShowDialogClickEventSerializer() ).
|
||||
registerTypeAdapter( ChatClickEventWrapperSerializer.class, new ChatClickEventWrapperSerializer() ).
|
||||
registerTypeAdapter( Action.class, new DialogActionSerializer() ).
|
||||
create();
|
||||
}
|
||||
|
||||
|
@ -1,27 +0,0 @@
|
||||
package net.md_5.bungee.serializer.dialog;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import java.lang.reflect.Type;
|
||||
import net.md_5.bungee.api.dialog.action.StaticAction;
|
||||
import net.md_5.bungee.chat.ClickEventSerializer;
|
||||
|
||||
public class ChatClickEventWrapperSerializer implements JsonDeserializer<StaticAction.ChatClickEventWrapper>, JsonSerializer<StaticAction.ChatClickEventWrapper>
|
||||
{
|
||||
|
||||
@Override
|
||||
public StaticAction.ChatClickEventWrapper deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
return new StaticAction.ChatClickEventWrapper( ClickEventSerializer.DIALOG.deserialize( json.getAsJsonObject(), context ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(StaticAction.ChatClickEventWrapper src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
return ClickEventSerializer.DIALOG.serialize( src.event(), context );
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package net.md_5.bungee.serializer.dialog;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.ImmutableBiMap;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonNull;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import java.lang.reflect.Type;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.dialog.action.Action;
|
||||
import net.md_5.bungee.api.dialog.action.CustomClickAction;
|
||||
import net.md_5.bungee.api.dialog.action.RunCommandAction;
|
||||
import net.md_5.bungee.api.dialog.action.StaticAction;
|
||||
import net.md_5.bungee.chat.ClickEventSerializer;
|
||||
|
||||
public class DialogActionSerializer implements JsonDeserializer<Action>, JsonSerializer<Action>
|
||||
{
|
||||
|
||||
private static final BiMap<String, Class<? extends Action>> DYNAMIC;
|
||||
|
||||
static
|
||||
{
|
||||
ImmutableBiMap.Builder<String, Class<? extends Action>> builder = ImmutableBiMap.builder();
|
||||
|
||||
builder.put( "minecraft:dynamic/custom", CustomClickAction.class );
|
||||
builder.put( "minecraft:dynamic/run_command", RunCommandAction.class );
|
||||
|
||||
DYNAMIC = builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
JsonObject object = json.getAsJsonObject();
|
||||
String type = object.get( "type" ).getAsString();
|
||||
|
||||
Class<? extends Action> realType = DYNAMIC.get( type );
|
||||
if ( realType == null )
|
||||
{
|
||||
ClickEvent click = ClickEventSerializer.DIALOG.deserialize( json.getAsJsonObject(), context );
|
||||
return new StaticAction( click );
|
||||
} else
|
||||
{
|
||||
return context.deserialize( json, realType );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(Action src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
if ( src == null )
|
||||
{
|
||||
return JsonNull.INSTANCE;
|
||||
}
|
||||
|
||||
if ( src instanceof StaticAction )
|
||||
{
|
||||
return ClickEventSerializer.DIALOG.serialize( ( (StaticAction) src ).clickEvent(), context );
|
||||
} else
|
||||
{
|
||||
Class<? extends Action> realType = src.getClass();
|
||||
String type = DYNAMIC.inverse().get( realType );
|
||||
Preconditions.checkArgument( type != null, "Unknown type %s", typeOfSrc );
|
||||
|
||||
JsonObject object = (JsonObject) context.serialize( src, realType );
|
||||
object.addProperty( "type", type );
|
||||
|
||||
return object;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user