Added RawJsonMessage constructor taking a json object as a string to reduce boilerplate code

This commit is contained in:
themode 2021-02-25 19:01:29 +01:00
parent e8e8022ec6
commit 882720c822
2 changed files with 6 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package net.minestom.server.chat;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
@ -109,6 +110,10 @@ public abstract class JsonMessage {
this.jsonObject = jsonObject;
}
public RawJsonMessage(@NotNull String jsonObject) {
this.jsonObject = JsonParser.parseString(jsonObject).getAsJsonObject();
}
@NotNull
@Override
public JsonObject getJsonObject() {

View File

@ -1,7 +1,5 @@
package net.minestom.server.utils.binary;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minestom.server.chat.JsonMessage;
@ -152,8 +150,7 @@ public class BinaryReader extends InputStream {
}
public JsonMessage readJsonMessage(int maxLength) {
final String string = readSizedString(maxLength);
final JsonObject jsonObject = JsonParser.parseString(string).getAsJsonObject();
final String jsonObject = readSizedString(maxLength);
return new JsonMessage.RawJsonMessage(jsonObject);
}