использование Text
This commit is contained in:
@@ -6,6 +6,7 @@ import lombok.ToString;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
import mc.protocol.model.text.Text;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -18,7 +19,7 @@ public class ServerInfo {
|
||||
private final Version version = new Version();
|
||||
private final Players players = new Players();
|
||||
|
||||
private String description;
|
||||
private Text description;
|
||||
private String favicon;
|
||||
|
||||
@Getter
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
package mc.protocol.model.text;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@Accessors(fluent = true)
|
||||
@Getter
|
||||
@EqualsAndHashCode
|
||||
@ToString
|
||||
public class Text {
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package mc.protocol.packets.server;
|
||||
|
||||
import com.eclipsesource.json.Json;
|
||||
import lombok.Data;
|
||||
import mc.protocol.State;
|
||||
import mc.protocol.io.NetByteBuf;
|
||||
import mc.protocol.model.text.Text;
|
||||
import mc.protocol.packets.ServerSidePacket;
|
||||
import mc.protocol.serializer.TextSerializer;
|
||||
|
||||
/**
|
||||
* Diconnect packet.
|
||||
@@ -34,10 +35,10 @@ public class DisconnectPacket implements ServerSidePacket {
|
||||
/**
|
||||
* Причина отключения.
|
||||
*/
|
||||
private String reason;
|
||||
private Text reason;
|
||||
|
||||
@Override
|
||||
public void writeSelf(NetByteBuf netByteBuf) {
|
||||
netByteBuf.writeString(Json.object().add("text", reason).toString());
|
||||
netByteBuf.writeString(TextSerializer.toJsonObject(reason).toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
package mc.protocol.packets.server;
|
||||
|
||||
import com.eclipsesource.json.Json;
|
||||
import com.eclipsesource.json.JsonArray;
|
||||
import com.eclipsesource.json.JsonObject;
|
||||
import com.google.common.collect.Streams;
|
||||
import lombok.Data;
|
||||
import mc.protocol.io.NetByteBuf;
|
||||
import mc.protocol.model.ServerInfo;
|
||||
import mc.protocol.packets.ServerSidePacket;
|
||||
|
||||
import java.util.stream.Collector;
|
||||
import mc.protocol.serializer.ServerInfoSerializer;
|
||||
|
||||
/**
|
||||
* Status server packet, response.
|
||||
@@ -61,39 +56,6 @@ public class StatusServerResponse implements ServerSidePacket {
|
||||
|
||||
@Override
|
||||
public void writeSelf(NetByteBuf netByteBuf) {
|
||||
JsonObject jsonObject = Json.object()
|
||||
.add("version", createVersionObj())
|
||||
.add("players", createPlayersObj())
|
||||
.add("description", Json.object().add("text", info.description()));
|
||||
|
||||
if (info.favicon() != null && !info.favicon().isEmpty()) {
|
||||
jsonObject.add("favicon", info.favicon());
|
||||
}
|
||||
|
||||
netByteBuf.writeString(jsonObject.toString());
|
||||
}
|
||||
|
||||
private JsonObject createVersionObj() {
|
||||
return Json.object()
|
||||
.add("name", info.version().name())
|
||||
.add("protocol", info.version().protocol());
|
||||
}
|
||||
|
||||
private JsonObject createPlayersObj() {
|
||||
JsonArray sampleArr = info.players().sample().stream()
|
||||
.map(samplePlayer -> Json.object()
|
||||
.add("name", samplePlayer.name())
|
||||
.add("id", samplePlayer.id()))
|
||||
.collect(Collector.of(Json::array, JsonArray::add, StatusServerResponse::jsonArrayAddAll));
|
||||
|
||||
return Json.object()
|
||||
.add("max", info.players().max())
|
||||
.add("online", info.players().online())
|
||||
.add("sample", sampleArr);
|
||||
}
|
||||
|
||||
private static JsonArray jsonArrayAddAll(JsonArray jsonArrayTo, JsonArray jsonArrayFrom) {
|
||||
Streams.stream(jsonArrayFrom).forEach(jsonArrayTo::add);
|
||||
return jsonArrayTo;
|
||||
netByteBuf.writeString(ServerInfoSerializer.toJsonObject(info).toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package mc.protocol.serializer;
|
||||
|
||||
import com.eclipsesource.json.Json;
|
||||
import com.eclipsesource.json.JsonArray;
|
||||
import com.eclipsesource.json.JsonObject;
|
||||
import com.google.common.collect.Streams;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import mc.protocol.model.ServerInfo;
|
||||
|
||||
import java.util.stream.Collector;
|
||||
|
||||
@UtilityClass
|
||||
public class ServerInfoSerializer {
|
||||
|
||||
public JsonObject toJsonObject(ServerInfo info) {
|
||||
JsonObject jsonObject = Json.object()
|
||||
.add("version", createVersionObj(info))
|
||||
.add("players", createPlayersObj(info))
|
||||
.add("description", TextSerializer.toJsonObject(info.description()));
|
||||
|
||||
if (info.favicon() != null && !info.favicon().isEmpty()) {
|
||||
jsonObject.add("favicon", info.favicon());
|
||||
}
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
private JsonObject createVersionObj(ServerInfo info) {
|
||||
return Json.object()
|
||||
.add("name", info.version().name())
|
||||
.add("protocol", info.version().protocol());
|
||||
}
|
||||
|
||||
private JsonObject createPlayersObj(ServerInfo info) {
|
||||
JsonArray sampleArr = info.players().sample().stream()
|
||||
.map(samplePlayer -> Json.object()
|
||||
.add("name", samplePlayer.name())
|
||||
.add("id", samplePlayer.id()))
|
||||
.collect(Collector.of(Json::array, JsonArray::add, ServerInfoSerializer::jsonArrayAddAll));
|
||||
|
||||
return Json.object()
|
||||
.add("max", info.players().max())
|
||||
.add("online", info.players().online())
|
||||
.add("sample", sampleArr);
|
||||
}
|
||||
|
||||
private static JsonArray jsonArrayAddAll(JsonArray jsonArrayTo, JsonArray jsonArrayFrom) {
|
||||
Streams.stream(jsonArrayFrom).forEach(jsonArrayTo::add);
|
||||
return jsonArrayTo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package mc.protocol.serializer;
|
||||
|
||||
import com.eclipsesource.json.Json;
|
||||
import com.eclipsesource.json.JsonArray;
|
||||
import com.eclipsesource.json.JsonObject;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import mc.protocol.model.text.Text;
|
||||
|
||||
@UtilityClass
|
||||
public class TextSerializer {
|
||||
|
||||
public JsonObject toJsonObject(Text text) {
|
||||
JsonObject jsonObject = Json.object();
|
||||
|
||||
if (text.content() != null) {
|
||||
jsonObject.add("text", text.content());
|
||||
}
|
||||
|
||||
if (text.color() != null) {
|
||||
jsonObject.add("color", text.color().getName());
|
||||
}
|
||||
|
||||
if (text.style() != null) {
|
||||
//@formatter:off
|
||||
if (text.style().bold() != null) jsonObject.add("bold", text.style().bold());
|
||||
if (text.style().italic() != null) jsonObject.add("italic", text.style().italic());
|
||||
if (text.style().underline() != null) jsonObject.add("underline", text.style().underline());
|
||||
if (text.style().strikethrough() != null) jsonObject.add("strikethrough", text.style().strikethrough());
|
||||
if (text.style().obfuscated() != null) jsonObject.add("obfuscated", text.style().obfuscated());
|
||||
//@formatter:on
|
||||
}
|
||||
|
||||
if (text.children() != null && !text.children().isEmpty()) {
|
||||
JsonArray extra = Json.array();
|
||||
text.children().forEach(child -> extra.add(toJsonObject(child)));
|
||||
jsonObject.add("extra", extra);
|
||||
}
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user