добавлен значок сервера
This commit is contained in:
@@ -16,7 +16,8 @@ ext {
|
||||
netty : 'io.netty:netty-all:4.1.22.Final',
|
||||
reactor : 'io.projectreactor:reactor-core:3.4.5',
|
||||
yaml : 'org.yaml:snakeyaml:1.28',
|
||||
json : 'com.eclipsesource.minimal-json:minimal-json:0.9.5'
|
||||
json : 'com.eclipsesource.minimal-json:minimal-json:0.9.5',
|
||||
ioutils : 'commons-io:commons-io:2.6'
|
||||
]
|
||||
|
||||
libs.logger = [
|
||||
|
||||
@@ -48,6 +48,8 @@ import java.util.stream.Collector;
|
||||
* "favicon": "data:image/png;base64,<data>"
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p><code>`$.favicon`</code> должен быть формата PNG и размеры 64x64 px</p>
|
||||
*/
|
||||
@Data
|
||||
public class StatusServerResponse implements ServerSidePacket {
|
||||
@@ -59,11 +61,16 @@ public class StatusServerResponse implements ServerSidePacket {
|
||||
|
||||
@Override
|
||||
public void writeSelf(NetByteBuf netByteBuf) {
|
||||
netByteBuf.writeString(Json.object()
|
||||
JsonObject jsonObject = Json.object()
|
||||
.add("version", createVersionObj())
|
||||
.add("players", createPlayersObj())
|
||||
.add("description", Json.object().add("text", info.description()))
|
||||
.toString());
|
||||
.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() {
|
||||
|
||||
@@ -20,4 +20,5 @@ dependencies {
|
||||
implementation libs.reactor
|
||||
implementation libs.guava
|
||||
implementation libs.yaml
|
||||
implementation libs.ioutils
|
||||
}
|
||||
|
||||
@@ -14,8 +14,13 @@ import mc.server.config.Config;
|
||||
import mc.server.di.ConfigModule;
|
||||
import mc.server.di.DaggerServerComponent;
|
||||
import mc.server.di.ServerComponent;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
|
||||
@Slf4j
|
||||
@@ -58,6 +63,10 @@ public class Main {
|
||||
serverInfo.players().sample(Collections.emptyList());
|
||||
serverInfo.description(config.motd());
|
||||
|
||||
if (config.iconPath() != null) {
|
||||
serverInfo.favicon(faviconToBase64(config.iconPath()));
|
||||
}
|
||||
|
||||
StatusServerResponse response = new StatusServerResponse();
|
||||
response.setInfo(serverInfo);
|
||||
|
||||
@@ -75,4 +84,15 @@ public class Main {
|
||||
|
||||
server.bind(config.server().host(), config.server().port());
|
||||
}
|
||||
|
||||
private static String faviconToBase64(Path iconPath) {
|
||||
try {
|
||||
return "data:image/png;base64," +
|
||||
Base64.getEncoder().encodeToString(
|
||||
IOUtils.toByteArray(Files.newInputStream(iconPath)));
|
||||
} catch (IOException e) {
|
||||
log.error("Can't read icon '{}'", iconPath.toAbsolutePath(), e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,5 +9,5 @@ players:
|
||||
online: 0
|
||||
|
||||
icon:
|
||||
enable: false
|
||||
path: favicon.png
|
||||
enable: true
|
||||
path: src/main/resources/favicon.png
|
||||
BIN
server/src/main/resources/favicon.png
Normal file
BIN
server/src/main/resources/favicon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.6 KiB |
Reference in New Issue
Block a user