Minecraft 23w43b support
This commit is contained in:
@@ -160,6 +160,28 @@ public final class TextComponent extends BaseComponent
|
||||
return components.toArray( new BaseComponent[ 0 ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal compatibility method to transform an array of components to a
|
||||
* single component.
|
||||
*
|
||||
* @param components array
|
||||
* @return single component
|
||||
*/
|
||||
public static BaseComponent fromArray(BaseComponent... components)
|
||||
{
|
||||
if ( components == null )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if ( components.length == 1 )
|
||||
{
|
||||
return components[0];
|
||||
}
|
||||
|
||||
return new TextComponent( components );
|
||||
}
|
||||
|
||||
/**
|
||||
* The text of the component that will be displayed to the client
|
||||
*/
|
||||
|
@@ -8,6 +8,7 @@ import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Set;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
@@ -87,14 +88,43 @@ public class ComponentSerializer implements JsonDeserializer<BaseComponent>
|
||||
public static BaseComponent deserialize(String json)
|
||||
{
|
||||
JsonElement jsonElement = JsonParser.parseString( json );
|
||||
|
||||
return deserialize( jsonElement );
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize a JSON element as a single component. The input is expected
|
||||
* to be a JSON object that represents only one component.
|
||||
*
|
||||
* @param jsonElement the component json to parse
|
||||
* @return the deserialized component
|
||||
* @throws IllegalArgumentException if anything other than a JSON object is
|
||||
* passed as input
|
||||
*/
|
||||
public static BaseComponent deserialize(JsonElement jsonElement)
|
||||
{
|
||||
if ( jsonElement instanceof JsonPrimitive )
|
||||
{
|
||||
JsonPrimitive primitive = (JsonPrimitive) jsonElement;
|
||||
if ( primitive.isString() )
|
||||
{
|
||||
return new TextComponent( primitive.getAsString() );
|
||||
}
|
||||
}
|
||||
|
||||
if ( !jsonElement.isJsonObject() )
|
||||
{
|
||||
throw new IllegalArgumentException( "Malformatted JSON. Expected object, got array for input \"" + json + "\"." );
|
||||
throw new IllegalArgumentException( "Malformatted JSON. Expected object, got array for input \"" + jsonElement + "\"." );
|
||||
}
|
||||
|
||||
return gson.fromJson( jsonElement, BaseComponent.class );
|
||||
}
|
||||
|
||||
public static JsonElement toJson(BaseComponent component)
|
||||
{
|
||||
return gson.toJsonTree( component );
|
||||
}
|
||||
|
||||
public static String toString(Object object)
|
||||
{
|
||||
return gson.toJson( object );
|
||||
|
@@ -18,11 +18,10 @@ public class TextComponentSerializer extends BaseComponentSerializer implements
|
||||
{
|
||||
TextComponent component = new TextComponent();
|
||||
JsonObject object = json.getAsJsonObject();
|
||||
if ( !object.has( "text" ) )
|
||||
if ( object.has( "text" ) )
|
||||
{
|
||||
throw new JsonParseException( "Could not parse JSON: missing 'text' property" );
|
||||
component.setText( object.get( "text" ).getAsString() );
|
||||
}
|
||||
component.setText( object.get( "text" ).getAsString() );
|
||||
deserialize( object, component, context );
|
||||
return component;
|
||||
}
|
||||
|
Reference in New Issue
Block a user