Skip to content

Commit

Permalink
Move getAsBoolean() into ComponentStyleSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
2008Choco committed Dec 7, 2023
1 parent c2cd541 commit 2fe9d2b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -21,30 +20,6 @@
public class BaseComponentSerializer
{

static boolean getAsBoolean(JsonElement el)
{
if ( el.isJsonPrimitive() )
{
JsonPrimitive primitive = (JsonPrimitive) el;

if ( primitive.isBoolean() )
{
return primitive.getAsBoolean();
}

if ( primitive.isNumber() )
{
Number number = primitive.getAsNumber();
if ( number instanceof Byte )
{
return number.byteValue() != 0;
}
}
}

return false;
}

protected void deserialize(JsonObject object, BaseComponent component, JsonDeserializationContext context)
{
component.applyStyle( context.deserialize( object, ComponentStyle.class ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
Expand All @@ -15,6 +16,30 @@
public class ComponentStyleSerializer implements JsonSerializer<ComponentStyle>, JsonDeserializer<ComponentStyle>
{

private static boolean getAsBoolean(JsonElement el)
{
if ( el.isJsonPrimitive() )
{
JsonPrimitive primitive = (JsonPrimitive) el;

if ( primitive.isBoolean() )
{
return primitive.getAsBoolean();
}

if ( primitive.isNumber() )
{
Number number = primitive.getAsNumber();
if ( number instanceof Byte )
{
return number.byteValue() != 0;
}
}
}

return false;
}

static void serializeTo(ComponentStyle style, JsonObject object)
{
if ( style.isBoldRaw() != null )
Expand Down Expand Up @@ -54,23 +79,23 @@ public ComponentStyle deserialize(JsonElement json, Type typeOfT, JsonDeserializ
JsonObject object = json.getAsJsonObject();
if ( object.has( "bold" ) )
{
builder.bold( BaseComponentSerializer.getAsBoolean( object.get( "bold" ) ) );
builder.bold( getAsBoolean( object.get( "bold" ) ) );
}
if ( object.has( "italic" ) )
{
builder.italic( BaseComponentSerializer.getAsBoolean( object.get( "italic" ) ) );
builder.italic( getAsBoolean( object.get( "italic" ) ) );
}
if ( object.has( "underlined" ) )
{
builder.underlined( BaseComponentSerializer.getAsBoolean( object.get( "underlined" ) ) );
builder.underlined( getAsBoolean( object.get( "underlined" ) ) );
}
if ( object.has( "strikethrough" ) )
{
builder.strikethrough( BaseComponentSerializer.getAsBoolean( object.get( "strikethrough" ) ) );
builder.strikethrough( getAsBoolean( object.get( "strikethrough" ) ) );
}
if ( object.has( "obfuscated" ) )
{
builder.obfuscated( BaseComponentSerializer.getAsBoolean( object.get( "obfuscated" ) ) );
builder.obfuscated( getAsBoolean( object.get( "obfuscated" ) ) );
}
if ( object.has( "color" ) )
{
Expand Down

0 comments on commit 2fe9d2b

Please sign in to comment.