Minecraft 1.21.6-pre1 support
This commit is contained in:
@@ -12,12 +12,9 @@ import java.util.IdentityHashMap;
|
||||
import java.util.Locale;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.ClickEventCustom;
|
||||
import net.md_5.bungee.api.chat.ComponentStyle;
|
||||
import net.md_5.bungee.api.chat.HoverEvent;
|
||||
import net.md_5.bungee.api.chat.hover.content.Content;
|
||||
import net.md_5.bungee.api.dialog.chat.ShowDialogClickEvent;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class BaseComponentSerializer
|
||||
@@ -44,36 +41,12 @@ public class BaseComponentSerializer
|
||||
}
|
||||
if ( clickEvent != null )
|
||||
{
|
||||
ClickEvent.Action action = ClickEvent.Action.valueOf( clickEvent.get( "action" ).getAsString().toUpperCase( Locale.ROOT ) );
|
||||
if ( newClickEvent )
|
||||
{
|
||||
switch ( action )
|
||||
{
|
||||
case OPEN_URL:
|
||||
component.setClickEvent( new ClickEvent( action, clickEvent.get( "url" ).getAsString() ) );
|
||||
break;
|
||||
case RUN_COMMAND:
|
||||
case SUGGEST_COMMAND:
|
||||
component.setClickEvent( new ClickEvent( action, clickEvent.get( "command" ).getAsString() ) );
|
||||
break;
|
||||
case CHANGE_PAGE:
|
||||
int page = clickEvent.get( "page" ).getAsInt();
|
||||
Preconditions.checkArgument( page >= 0, "Page number has to be positive" );
|
||||
component.setClickEvent( new ClickEvent( action, Integer.toString( page ) ) );
|
||||
break;
|
||||
case SHOW_DIALOG:
|
||||
component.setClickEvent( context.deserialize( clickEvent.get( "dialog" ), ShowDialogClickEvent.class ) );
|
||||
break;
|
||||
case CUSTOM:
|
||||
component.setClickEvent( new ClickEventCustom( clickEvent.get( "id" ).getAsString(), ( clickEvent.has( "payload" ) ) ? clickEvent.get( "payload" ).getAsString() : null ) );
|
||||
break;
|
||||
default:
|
||||
component.setClickEvent( new ClickEvent( action, ( clickEvent.has( "value" ) ) ? clickEvent.get( "value" ).getAsString() : "" ) );
|
||||
break;
|
||||
}
|
||||
component.setClickEvent( ClickEventSerializer.NEW.deserialize( clickEvent, context ) );
|
||||
} else
|
||||
{
|
||||
component.setClickEvent( new ClickEvent( action, ( clickEvent.has( "value" ) ) ? clickEvent.get( "value" ).getAsString() : "" ) );
|
||||
component.setClickEvent( ClickEventSerializer.OLD.deserialize( clickEvent, context ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,50 +144,17 @@ public class BaseComponentSerializer
|
||||
//Events
|
||||
if ( component.getClickEvent() != null )
|
||||
{
|
||||
JsonObject clickEvent = new JsonObject();
|
||||
String actionName = component.getClickEvent().getAction().toString().toLowerCase( Locale.ROOT );
|
||||
clickEvent.addProperty( "action", actionName.toLowerCase( Locale.ROOT ) );
|
||||
switch ( serializer.getVersion() )
|
||||
{
|
||||
case V1_21_5:
|
||||
ClickEvent.Action action = ClickEvent.Action.valueOf( actionName.toUpperCase( Locale.ROOT ) );
|
||||
switch ( action )
|
||||
{
|
||||
case OPEN_URL:
|
||||
clickEvent.addProperty( "url", component.getClickEvent().getValue() );
|
||||
break;
|
||||
case RUN_COMMAND:
|
||||
case SUGGEST_COMMAND:
|
||||
clickEvent.addProperty( "command", component.getClickEvent().getValue() );
|
||||
break;
|
||||
case CHANGE_PAGE:
|
||||
clickEvent.addProperty( "page", Integer.parseInt( component.getClickEvent().getValue() ) );
|
||||
break;
|
||||
case SHOW_DIALOG:
|
||||
clickEvent.add( "dialog", context.serialize( component.getClickEvent() ) );
|
||||
break;
|
||||
case CUSTOM:
|
||||
ClickEventCustom custom = (ClickEventCustom) component.getClickEvent();
|
||||
clickEvent.addProperty( "id", custom.getValue() );
|
||||
if ( custom.getPayload() != null )
|
||||
{
|
||||
clickEvent.addProperty( "payload", custom.getPayload() );
|
||||
}
|
||||
break;
|
||||
default:
|
||||
clickEvent.addProperty( "value", component.getClickEvent().getValue() );
|
||||
break;
|
||||
}
|
||||
object.add( "click_event", clickEvent );
|
||||
object.add( "click_event", ClickEventSerializer.NEW.serialize( component.getClickEvent(), context ) );
|
||||
break;
|
||||
case V1_16:
|
||||
clickEvent.addProperty( "value", component.getClickEvent().getValue() );
|
||||
object.add( "clickEvent", clickEvent );
|
||||
object.add( "clickEvent", ClickEventSerializer.OLD.serialize( component.getClickEvent(), context ) );
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException( "Unknown version " + serializer.getVersion() );
|
||||
}
|
||||
|
||||
}
|
||||
if ( component.getHoverEvent() != null )
|
||||
{
|
||||
|
@@ -0,0 +1,108 @@
|
||||
package net.md_5.bungee.chat;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import java.util.Locale;
|
||||
import lombok.Data;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.ClickEventCustom;
|
||||
import net.md_5.bungee.api.dialog.chat.ShowDialogClickEvent;
|
||||
|
||||
@Data
|
||||
public class ClickEventSerializer
|
||||
{
|
||||
|
||||
public static final ClickEventSerializer OLD = new ClickEventSerializer( ClickType.OLD );
|
||||
public static final ClickEventSerializer NEW = new ClickEventSerializer( ClickType.NEW );
|
||||
public static final ClickEventSerializer DIALOG = new ClickEventSerializer( ClickType.DIALOG );
|
||||
//
|
||||
public enum ClickType
|
||||
{
|
||||
OLD, NEW, DIALOG;
|
||||
}
|
||||
private final ClickType type;
|
||||
|
||||
public ClickEvent deserialize(JsonObject clickEvent, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
ClickEvent.Action action = ClickEvent.Action.valueOf( clickEvent.get( ( type == ClickType.DIALOG ) ? "type" : "action" ).getAsString().toUpperCase( Locale.ROOT ) );
|
||||
switch ( type )
|
||||
{
|
||||
case NEW:
|
||||
case DIALOG:
|
||||
switch ( action )
|
||||
{
|
||||
case OPEN_URL:
|
||||
return new ClickEvent( action, clickEvent.get( "url" ).getAsString() );
|
||||
case RUN_COMMAND:
|
||||
case SUGGEST_COMMAND:
|
||||
return new ClickEvent( action, clickEvent.get( "command" ).getAsString() );
|
||||
case CHANGE_PAGE:
|
||||
int page = clickEvent.get( "page" ).getAsInt();
|
||||
Preconditions.checkArgument( page >= 0, "Page number has to be positive" );
|
||||
return new ClickEvent( action, Integer.toString( page ) );
|
||||
case SHOW_DIALOG:
|
||||
return context.deserialize( clickEvent.get( "dialog" ), ShowDialogClickEvent.class );
|
||||
case CUSTOM:
|
||||
return new ClickEventCustom( clickEvent.get( "id" ).getAsString(), ( clickEvent.has( "payload" ) ) ? clickEvent.get( "payload" ).getAsString() : null );
|
||||
default:
|
||||
return new ClickEvent( action, ( clickEvent.has( "value" ) ) ? clickEvent.get( "value" ).getAsString() : "" );
|
||||
}
|
||||
case OLD:
|
||||
return new ClickEvent( action, ( clickEvent.has( "value" ) ) ? clickEvent.get( "value" ).getAsString() : "" );
|
||||
default:
|
||||
throw new IllegalArgumentException( "Unknown serializer type" );
|
||||
}
|
||||
}
|
||||
|
||||
public JsonElement serialize(ClickEvent src, JsonSerializationContext context)
|
||||
{
|
||||
JsonObject clickEvent = new JsonObject();
|
||||
String actionName = src.getAction().toString().toLowerCase( Locale.ROOT );
|
||||
clickEvent.addProperty( ( type == ClickType.DIALOG ) ? "type" : "action", actionName.toLowerCase( Locale.ROOT ) );
|
||||
switch ( type )
|
||||
{
|
||||
case NEW:
|
||||
case DIALOG:
|
||||
ClickEvent.Action action = ClickEvent.Action.valueOf( actionName.toUpperCase( Locale.ROOT ) );
|
||||
switch ( action )
|
||||
{
|
||||
case OPEN_URL:
|
||||
clickEvent.addProperty( "url", src.getValue() );
|
||||
break;
|
||||
case RUN_COMMAND:
|
||||
case SUGGEST_COMMAND:
|
||||
clickEvent.addProperty( "command", src.getValue() );
|
||||
break;
|
||||
case CHANGE_PAGE:
|
||||
clickEvent.addProperty( "page", Integer.parseInt( src.getValue() ) );
|
||||
break;
|
||||
case SHOW_DIALOG:
|
||||
clickEvent.add( "dialog", context.serialize( src ) );
|
||||
break;
|
||||
case CUSTOM:
|
||||
ClickEventCustom custom = (ClickEventCustom) src;
|
||||
clickEvent.addProperty( "id", custom.getValue() );
|
||||
if ( custom.getPayload() != null )
|
||||
{
|
||||
clickEvent.addProperty( "payload", custom.getPayload() );
|
||||
}
|
||||
break;
|
||||
default:
|
||||
clickEvent.addProperty( "value", src.getValue() );
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case OLD:
|
||||
clickEvent.addProperty( "value", src.getValue() );
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException( "Unknown serializer type" );
|
||||
}
|
||||
|
||||
return clickEvent;
|
||||
}
|
||||
}
|
@@ -30,6 +30,7 @@ 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.chat.ShowDialogClickEvent;
|
||||
import net.md_5.bungee.dialog.ChatClickEventWrapperSerializer;
|
||||
import net.md_5.bungee.dialog.DialogSerializer;
|
||||
import net.md_5.bungee.dialog.ShowDialogClickEventSerializer;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
@@ -67,6 +68,7 @@ public class VersionedComponentSerializer implements JsonDeserializer<BaseCompon
|
||||
// Dialogs
|
||||
registerTypeAdapter( Dialog.class, dialogSerializer ).
|
||||
registerTypeAdapter( ShowDialogClickEvent.class, new ShowDialogClickEventSerializer() ).
|
||||
registerTypeAdapter( ChatClickEventWrapperSerializer.class, new ChatClickEventWrapperSerializer() ).
|
||||
create();
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,27 @@
|
||||
package net.md_5.bungee.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 );
|
||||
}
|
||||
}
|
@@ -22,16 +22,14 @@ import net.md_5.bungee.api.dialog.Dialog;
|
||||
import net.md_5.bungee.api.dialog.DialogBase;
|
||||
import net.md_5.bungee.api.dialog.DialogListDialog;
|
||||
import net.md_5.bungee.api.dialog.MultiActionDialog;
|
||||
import net.md_5.bungee.api.dialog.MultiActionInputFormDialog;
|
||||
import net.md_5.bungee.api.dialog.NoticeDialog;
|
||||
import net.md_5.bungee.api.dialog.ServerLinksDialog;
|
||||
import net.md_5.bungee.api.dialog.SimpleInputFormDialog;
|
||||
import net.md_5.bungee.chat.VersionedComponentSerializer;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class DialogSerializer implements JsonDeserializer<Dialog>, JsonSerializer<Dialog>
|
||||
{
|
||||
private static final ThreadLocal<Set<Dialog>> serializedDialogs = new ThreadLocal<Set<Dialog>>();
|
||||
private static final ThreadLocal<Set<Dialog>> serializedDialogs = new ThreadLocal<>();
|
||||
private static final BiMap<String, Class<? extends Dialog>> TYPES;
|
||||
private final VersionedComponentSerializer serializer;
|
||||
|
||||
@@ -44,8 +42,6 @@ public class DialogSerializer implements JsonDeserializer<Dialog>, JsonSerialize
|
||||
builder.put( "minecraft:multi_action", MultiActionDialog.class );
|
||||
builder.put( "minecraft:server_links", ServerLinksDialog.class );
|
||||
builder.put( "minecraft:dialog_list", DialogListDialog.class );
|
||||
builder.put( "minecraft:simple_input_form", SimpleInputFormDialog.class );
|
||||
builder.put( "minecraft:multi_action_input_form", MultiActionInputFormDialog.class );
|
||||
|
||||
TYPES = builder.build();
|
||||
}
|
||||
@@ -108,7 +104,7 @@ public class DialogSerializer implements JsonDeserializer<Dialog>, JsonSerialize
|
||||
boolean first = serializedDialogs.get() == null;
|
||||
if ( first )
|
||||
{
|
||||
serializedDialogs.set( Collections.newSetFromMap( new IdentityHashMap<Dialog, Boolean>() ) );
|
||||
serializedDialogs.set( Collections.newSetFromMap( new IdentityHashMap<>() ) );
|
||||
}
|
||||
|
||||
try
|
||||
|
Reference in New Issue
Block a user