#2621: Fix score component serialization

This commit is contained in:
md_5 2019-04-06 19:53:26 +11:00
parent 771f1735e5
commit 5aaccd2e9e
12 changed files with 37 additions and 9 deletions

View File

@ -8,11 +8,13 @@ import net.md_5.bungee.api.ChatColor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.ToString; import lombok.ToString;
import net.md_5.bungee.api.chat.ComponentBuilder.FormatRetention; import net.md_5.bungee.api.chat.ComponentBuilder.FormatRetention;
@Setter @Setter
@ToString(exclude = "parent") @ToString(exclude = "parent")
@EqualsAndHashCode
@NoArgsConstructor @NoArgsConstructor
public abstract class BaseComponent public abstract class BaseComponent
{ {

View File

@ -1,11 +1,13 @@
package net.md_5.bungee.api.chat; package net.md_5.bungee.api.chat;
import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.ToString; import lombok.ToString;
@Getter @Getter
@ToString @ToString
@EqualsAndHashCode
@RequiredArgsConstructor @RequiredArgsConstructor
public final class ClickEvent public final class ClickEvent
{ {

View File

@ -1,11 +1,13 @@
package net.md_5.bungee.api.chat; package net.md_5.bungee.api.chat;
import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.ToString; import lombok.ToString;
@Getter @Getter
@ToString @ToString
@EqualsAndHashCode
@RequiredArgsConstructor @RequiredArgsConstructor
public final class HoverEvent public final class HoverEvent
{ {

View File

@ -1,5 +1,6 @@
package net.md_5.bungee.api.chat; package net.md_5.bungee.api.chat;
import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
@ -10,6 +11,7 @@ import net.md_5.bungee.api.ChatColor;
@Setter @Setter
@ToString @ToString
@NoArgsConstructor @NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public final class KeybindComponent extends BaseComponent public final class KeybindComponent extends BaseComponent
{ {

View File

@ -1,6 +1,7 @@
package net.md_5.bungee.api.chat; package net.md_5.bungee.api.chat;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.ToString; import lombok.ToString;
@ -26,6 +27,7 @@ import lombok.ToString;
@Setter @Setter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public final class ScoreComponent extends BaseComponent public final class ScoreComponent extends BaseComponent
{ {

View File

@ -1,6 +1,7 @@
package net.md_5.bungee.api.chat; package net.md_5.bungee.api.chat;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.ToString; import lombok.ToString;
@ -22,6 +23,7 @@ import lombok.ToString;
@Setter @Setter
@ToString @ToString
@AllArgsConstructor @AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public final class SelectorComponent extends BaseComponent public final class SelectorComponent extends BaseComponent
{ {

View File

@ -9,10 +9,12 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import lombok.EqualsAndHashCode;
@Getter @Getter
@Setter @Setter
@AllArgsConstructor @AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public final class TextComponent extends BaseComponent public final class TextComponent extends BaseComponent
{ {

View File

@ -8,6 +8,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import lombok.EqualsAndHashCode;
import lombok.ToString; import lombok.ToString;
import net.md_5.bungee.chat.TranslationRegistry; import net.md_5.bungee.chat.TranslationRegistry;
@ -15,6 +16,7 @@ import net.md_5.bungee.chat.TranslationRegistry;
@Setter @Setter
@ToString @ToString
@NoArgsConstructor @NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public final class TranslatableComponent extends BaseComponent public final class TranslatableComponent extends BaseComponent
{ {

View File

@ -10,7 +10,8 @@ import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.HoverEvent;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Locale; import java.util.Locale;
public class BaseComponentSerializer public class BaseComponentSerializer
@ -83,7 +84,7 @@ public class BaseComponentSerializer
if ( ComponentSerializer.serializedComponents.get() == null ) if ( ComponentSerializer.serializedComponents.get() == null )
{ {
first = true; first = true;
ComponentSerializer.serializedComponents.set( new HashSet<BaseComponent>() ); ComponentSerializer.serializedComponents.set( Collections.newSetFromMap( new IdentityHashMap<BaseComponent, Boolean>() ) );
} }
try try
{ {

View File

@ -16,7 +16,7 @@ import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.TranslatableComponent; import net.md_5.bungee.api.chat.TranslatableComponent;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.HashSet; import java.util.Set;
public class ComponentSerializer implements JsonDeserializer<BaseComponent> public class ComponentSerializer implements JsonDeserializer<BaseComponent>
{ {
@ -31,7 +31,7 @@ public class ComponentSerializer implements JsonDeserializer<BaseComponent>
registerTypeAdapter( SelectorComponent.class, new SelectorComponentSerializer() ). registerTypeAdapter( SelectorComponent.class, new SelectorComponentSerializer() ).
create(); create();
public final static ThreadLocal<HashSet<BaseComponent>> serializedComponents = new ThreadLocal<HashSet<BaseComponent>>(); public final static ThreadLocal<Set<BaseComponent>> serializedComponents = new ThreadLocal<Set<BaseComponent>>();
public static BaseComponent[] parse(String json) public static BaseComponent[] parse(String json)
{ {

View File

@ -17,17 +17,18 @@ public class ScoreComponentSerializer extends BaseComponentSerializer implements
public ScoreComponent deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException public ScoreComponent deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException
{ {
JsonObject json = element.getAsJsonObject(); JsonObject json = element.getAsJsonObject();
if ( !json.has( "name" ) || !json.has( "objective" ) ) JsonObject score = json.get( "score" ).getAsJsonObject();
if ( !score.has( "name" ) || !score.has( "objective" ) )
{ {
throw new JsonParseException( "A score component needs at least a name and an objective" ); throw new JsonParseException( "A score component needs at least a name and an objective" );
} }
String name = json.get( "name" ).getAsString(); String name = score.get( "name" ).getAsString();
String objective = json.get( "objective" ).getAsString(); String objective = score.get( "objective" ).getAsString();
ScoreComponent component = new ScoreComponent( name, objective ); ScoreComponent component = new ScoreComponent( name, objective );
if ( json.has( "value" ) && !json.get( "value" ).getAsString().isEmpty() ) if ( score.has( "value" ) && !score.get( "value" ).getAsString().isEmpty() )
{ {
component.setValue( json.get( "value" ).getAsString() ); component.setValue( score.get( "value" ).getAsString() );
} }
deserialize( json, component, context ); deserialize( json, component, context );

View File

@ -75,6 +75,16 @@ public class ComponentsTest
Assert.assertEquals( scoreComponent.toPlainText(), components[3].toPlainText() ); Assert.assertEquals( scoreComponent.toPlainText(), components[3].toPlainText() );
} }
@Test
public void testScore()
{
BaseComponent[] component = ComponentSerializer.parse( "{\"score\":{\"name\":\"@p\",\"objective\":\"TEST\",\"value\":\"hello\"}}" );
String text = ComponentSerializer.toString( component );
BaseComponent[] reparsed = ComponentSerializer.parse( text );
Assert.assertArrayEquals( component, reparsed );
}
@Test @Test
public void testBuilderAppend() public void testBuilderAppend()
{ {