feat: implemented MotdModule

This commit is contained in:
Andrey Terentev 2024-11-26 21:12:29 +07:00
parent 516129e898
commit 08c8aa43b8
2 changed files with 66 additions and 0 deletions

View File

@ -1,8 +1,10 @@
package ru.dragonestia.msb3.api;
import lombok.extern.log4j.Log4j2;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.GameMode;
import ru.dragonestia.msb3.api.module.FlatWorldModule;
import ru.dragonestia.msb3.api.module.MotdModule;
import ru.dragonestia.msb3.api.module.ResourcePackRepositoryModule;
@Log4j2
@ -11,6 +13,7 @@ public class Bootstrap {
public static void main(String[] args) {
var boot = new ServerBootstrap();
MotdModule.init("logo.png", "<gradient:#ff0059:#e06806><bold>msb3 server</bold></gradient>");
FlatWorldModule.init(GameMode.ADVENTURE);
ResourcePackRepositoryModule.init(boot, "0.0.0.0", 7270);

View File

@ -0,0 +1,63 @@
package ru.dragonestia.msb3.api.module;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.minestom.server.MinecraftServer;
import net.minestom.server.event.server.ServerListPingEvent;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.function.Supplier;
public class MotdModule {
private static boolean used = false;
private MotdModule() {}
public static void init(String internalResourceFile, String minimessage) {
var component = MiniMessage.miniMessage().deserialize(minimessage);
try (var stream = MotdModule.class.getClassLoader().getResourceAsStream(internalResourceFile)) {
assert stream != null;
init(stream.readAllBytes(), () -> component);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public static void init(File icon, String minimessage) {
var component = MiniMessage.miniMessage().deserialize(minimessage);
init(icon, () -> component);
}
public static void init(File icon, Supplier<Component> serverName) {
try {
var image = ImageIO.read(icon);
var stream = new ByteArrayOutputStream();
ImageIO.write(image, "png", stream);
init(stream.toByteArray(), serverName);
stream.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static synchronized void init(byte[] imageBytes, Supplier<Component> serverName) {
if (used) return;
used = true;
MinecraftServer.getGlobalEventHandler().addListener(ServerListPingEvent.class, event -> {
var data = event.getResponseData();
data.setDescription(serverName.get());
data.setFavicon(favicon(imageBytes));
});
}
private static String favicon(byte[] bytes) {
return "data:image/png;base64," + Base64.getEncoder().encodeToString(bytes);
}
}