From 5aaccd2e9ec554f8f769aa0eb11a7393236aad85 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 6 Apr 2019 19:53:26 +1100 Subject: [PATCH] #2621: Fix score component serialization --- .../java/net/md_5/bungee/api/chat/BaseComponent.java | 2 ++ .../java/net/md_5/bungee/api/chat/ClickEvent.java | 2 ++ .../java/net/md_5/bungee/api/chat/HoverEvent.java | 2 ++ .../net/md_5/bungee/api/chat/KeybindComponent.java | 2 ++ .../java/net/md_5/bungee/api/chat/ScoreComponent.java | 2 ++ .../net/md_5/bungee/api/chat/SelectorComponent.java | 2 ++ .../java/net/md_5/bungee/api/chat/TextComponent.java | 2 ++ .../md_5/bungee/api/chat/TranslatableComponent.java | 2 ++ .../net/md_5/bungee/chat/BaseComponentSerializer.java | 5 +++-- .../net/md_5/bungee/chat/ComponentSerializer.java | 4 ++-- .../md_5/bungee/chat/ScoreComponentSerializer.java | 11 ++++++----- .../java/net/md_5/bungee/api/chat/ComponentsTest.java | 10 ++++++++++ 12 files changed, 37 insertions(+), 9 deletions(-) diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java index 5aa7e68ea..b26e8429c 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java @@ -8,11 +8,13 @@ import net.md_5.bungee.api.ChatColor; import java.util.ArrayList; import java.util.List; +import lombok.EqualsAndHashCode; import lombok.ToString; import net.md_5.bungee.api.chat.ComponentBuilder.FormatRetention; @Setter @ToString(exclude = "parent") +@EqualsAndHashCode @NoArgsConstructor public abstract class BaseComponent { diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/ClickEvent.java b/chat/src/main/java/net/md_5/bungee/api/chat/ClickEvent.java index 1fb311128..2705ce586 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/ClickEvent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/ClickEvent.java @@ -1,11 +1,13 @@ package net.md_5.bungee.api.chat; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.ToString; @Getter @ToString +@EqualsAndHashCode @RequiredArgsConstructor public final class ClickEvent { diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/HoverEvent.java b/chat/src/main/java/net/md_5/bungee/api/chat/HoverEvent.java index 0f6ec77d5..253ee734b 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/HoverEvent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/HoverEvent.java @@ -1,11 +1,13 @@ package net.md_5.bungee.api.chat; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.ToString; @Getter @ToString +@EqualsAndHashCode @RequiredArgsConstructor public final class HoverEvent { diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/KeybindComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/KeybindComponent.java index 0bcb06e44..d0fc17933 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/KeybindComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/KeybindComponent.java @@ -1,5 +1,6 @@ package net.md_5.bungee.api.chat; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -10,6 +11,7 @@ import net.md_5.bungee.api.ChatColor; @Setter @ToString @NoArgsConstructor +@EqualsAndHashCode(callSuper = true) public final class KeybindComponent extends BaseComponent { diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/ScoreComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/ScoreComponent.java index 48ef24aa2..6a4080a03 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/ScoreComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/ScoreComponent.java @@ -1,6 +1,7 @@ package net.md_5.bungee.api.chat; import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import lombok.ToString; @@ -26,6 +27,7 @@ import lombok.ToString; @Setter @ToString @AllArgsConstructor +@EqualsAndHashCode(callSuper = true) public final class ScoreComponent extends BaseComponent { diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/SelectorComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/SelectorComponent.java index 3140b3228..4ef2d8cb1 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/SelectorComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/SelectorComponent.java @@ -1,6 +1,7 @@ package net.md_5.bungee.api.chat; import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import lombok.ToString; @@ -22,6 +23,7 @@ import lombok.ToString; @Setter @ToString @AllArgsConstructor +@EqualsAndHashCode(callSuper = true) public final class SelectorComponent extends BaseComponent { diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java index e3ef9b7e3..77bdea5de 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java @@ -9,10 +9,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; +import lombok.EqualsAndHashCode; @Getter @Setter @AllArgsConstructor +@EqualsAndHashCode(callSuper = true) public final class TextComponent extends BaseComponent { diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java index 54da44672..a6ee3ff38 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import lombok.EqualsAndHashCode; import lombok.ToString; import net.md_5.bungee.chat.TranslationRegistry; @@ -15,6 +16,7 @@ import net.md_5.bungee.chat.TranslationRegistry; @Setter @ToString @NoArgsConstructor +@EqualsAndHashCode(callSuper = true) public final class TranslatableComponent extends BaseComponent { diff --git a/chat/src/main/java/net/md_5/bungee/chat/BaseComponentSerializer.java b/chat/src/main/java/net/md_5/bungee/chat/BaseComponentSerializer.java index 7f6475129..9f27f1d69 100644 --- a/chat/src/main/java/net/md_5/bungee/chat/BaseComponentSerializer.java +++ b/chat/src/main/java/net/md_5/bungee/chat/BaseComponentSerializer.java @@ -10,7 +10,8 @@ import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.HoverEvent; import java.util.Arrays; -import java.util.HashSet; +import java.util.Collections; +import java.util.IdentityHashMap; import java.util.Locale; public class BaseComponentSerializer @@ -83,7 +84,7 @@ public class BaseComponentSerializer if ( ComponentSerializer.serializedComponents.get() == null ) { first = true; - ComponentSerializer.serializedComponents.set( new HashSet() ); + ComponentSerializer.serializedComponents.set( Collections.newSetFromMap( new IdentityHashMap() ) ); } try { diff --git a/chat/src/main/java/net/md_5/bungee/chat/ComponentSerializer.java b/chat/src/main/java/net/md_5/bungee/chat/ComponentSerializer.java index 0b7cf3b49..eaf9f79fd 100644 --- a/chat/src/main/java/net/md_5/bungee/chat/ComponentSerializer.java +++ b/chat/src/main/java/net/md_5/bungee/chat/ComponentSerializer.java @@ -16,7 +16,7 @@ import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TranslatableComponent; import java.lang.reflect.Type; -import java.util.HashSet; +import java.util.Set; public class ComponentSerializer implements JsonDeserializer { @@ -31,7 +31,7 @@ public class ComponentSerializer implements JsonDeserializer registerTypeAdapter( SelectorComponent.class, new SelectorComponentSerializer() ). create(); - public final static ThreadLocal> serializedComponents = new ThreadLocal>(); + public final static ThreadLocal> serializedComponents = new ThreadLocal>(); public static BaseComponent[] parse(String json) { diff --git a/chat/src/main/java/net/md_5/bungee/chat/ScoreComponentSerializer.java b/chat/src/main/java/net/md_5/bungee/chat/ScoreComponentSerializer.java index ecff9beff..f09c8bf90 100644 --- a/chat/src/main/java/net/md_5/bungee/chat/ScoreComponentSerializer.java +++ b/chat/src/main/java/net/md_5/bungee/chat/ScoreComponentSerializer.java @@ -17,17 +17,18 @@ public class ScoreComponentSerializer extends BaseComponentSerializer implements public ScoreComponent deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException { 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" ); } - String name = json.get( "name" ).getAsString(); - String objective = json.get( "objective" ).getAsString(); + String name = score.get( "name" ).getAsString(); + String objective = score.get( "objective" ).getAsString(); 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 ); diff --git a/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java b/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java index bb38f1b1c..8fa10e443 100644 --- a/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java +++ b/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java @@ -75,6 +75,16 @@ public class ComponentsTest 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 public void testBuilderAppend() {