использование Text
This commit is contained in:
@@ -6,6 +6,7 @@ import lombok.ToString;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.experimental.Accessors;
|
import lombok.experimental.Accessors;
|
||||||
|
import mc.protocol.model.text.Text;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ public class ServerInfo {
|
|||||||
private final Version version = new Version();
|
private final Version version = new Version();
|
||||||
private final Players players = new Players();
|
private final Players players = new Players();
|
||||||
|
|
||||||
private String description;
|
private Text description;
|
||||||
private String favicon;
|
private String favicon;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
package mc.protocol.model.text;
|
package mc.protocol.model.text;
|
||||||
|
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@Accessors(fluent = true)
|
||||||
|
@Getter
|
||||||
@EqualsAndHashCode
|
@EqualsAndHashCode
|
||||||
@ToString
|
@ToString
|
||||||
public class Text {
|
public class Text {
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package mc.protocol.packets.server;
|
package mc.protocol.packets.server;
|
||||||
|
|
||||||
import com.eclipsesource.json.Json;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import mc.protocol.State;
|
import mc.protocol.State;
|
||||||
import mc.protocol.io.NetByteBuf;
|
import mc.protocol.io.NetByteBuf;
|
||||||
|
import mc.protocol.model.text.Text;
|
||||||
import mc.protocol.packets.ServerSidePacket;
|
import mc.protocol.packets.ServerSidePacket;
|
||||||
|
import mc.protocol.serializer.TextSerializer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Diconnect packet.
|
* Diconnect packet.
|
||||||
@@ -34,10 +35,10 @@ public class DisconnectPacket implements ServerSidePacket {
|
|||||||
/**
|
/**
|
||||||
* Причина отключения.
|
* Причина отключения.
|
||||||
*/
|
*/
|
||||||
private String reason;
|
private Text reason;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeSelf(NetByteBuf netByteBuf) {
|
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;
|
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 lombok.Data;
|
||||||
import mc.protocol.io.NetByteBuf;
|
import mc.protocol.io.NetByteBuf;
|
||||||
import mc.protocol.model.ServerInfo;
|
import mc.protocol.model.ServerInfo;
|
||||||
import mc.protocol.packets.ServerSidePacket;
|
import mc.protocol.packets.ServerSidePacket;
|
||||||
|
import mc.protocol.serializer.ServerInfoSerializer;
|
||||||
import java.util.stream.Collector;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Status server packet, response.
|
* Status server packet, response.
|
||||||
@@ -61,39 +56,6 @@ public class StatusServerResponse implements ServerSidePacket {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeSelf(NetByteBuf netByteBuf) {
|
public void writeSelf(NetByteBuf netByteBuf) {
|
||||||
JsonObject jsonObject = Json.object()
|
netByteBuf.writeString(ServerInfoSerializer.toJsonObject(info).toString());
|
||||||
.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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import mc.protocol.NettyServer;
|
import mc.protocol.NettyServer;
|
||||||
import mc.protocol.ProtocolConstant;
|
import mc.protocol.ProtocolConstant;
|
||||||
import mc.protocol.model.ServerInfo;
|
import mc.protocol.model.ServerInfo;
|
||||||
|
import mc.protocol.model.text.Text;
|
||||||
import mc.protocol.packets.PingPacket;
|
import mc.protocol.packets.PingPacket;
|
||||||
import mc.protocol.packets.client.HandshakePacket;
|
import mc.protocol.packets.client.HandshakePacket;
|
||||||
import mc.protocol.packets.client.LoginStartPacket;
|
import mc.protocol.packets.client.LoginStartPacket;
|
||||||
@@ -61,7 +62,7 @@ public class Main {
|
|||||||
serverInfo.players().max(config.players().maxOnlile());
|
serverInfo.players().max(config.players().maxOnlile());
|
||||||
serverInfo.players().online(config.players().onlile());
|
serverInfo.players().online(config.players().onlile());
|
||||||
serverInfo.players().sample(Collections.emptyList());
|
serverInfo.players().sample(Collections.emptyList());
|
||||||
serverInfo.description(config.motd());
|
serverInfo.description(Text.of(config.motd()));
|
||||||
|
|
||||||
if (config.iconPath() != null) {
|
if (config.iconPath() != null) {
|
||||||
serverInfo.favicon(faviconToBase64(config.iconPath()));
|
serverInfo.favicon(faviconToBase64(config.iconPath()));
|
||||||
@@ -77,7 +78,7 @@ public class Main {
|
|||||||
.doOnNext(channel -> log.info("{}", channel.getPacket()))
|
.doOnNext(channel -> log.info("{}", channel.getPacket()))
|
||||||
.subscribe(channel -> {
|
.subscribe(channel -> {
|
||||||
DisconnectPacket disconnectPacket = new DisconnectPacket();
|
DisconnectPacket disconnectPacket = new DisconnectPacket();
|
||||||
disconnectPacket.setReason("Server is not available.");
|
disconnectPacket.setReason(Text.of("Server is not available."));
|
||||||
|
|
||||||
channel.getCtx().writeAndFlush(disconnectPacket).channel().disconnect();
|
channel.getCtx().writeAndFlush(disconnectPacket).channel().disconnect();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user