Add favicon support

This commit is contained in:
Andrew Steinborn
2018-08-08 04:44:27 -04:00
parent bb601dca4b
commit a261823302
8 changed files with 147 additions and 5 deletions

View File

@@ -3,6 +3,12 @@ package com.velocitypowered.proxy;
import com.velocitypowered.proxy.console.VelocityConsole;
public class Velocity {
static {
// We use BufferedImage for favicons, and on macOS this puts the Java application in the dock. How inconvenient.
// Force AWT to work with its head chopped off.
System.setProperty("java.awt.headless", "true");
}
public static void main(String... args) {
final VelocityServer server = VelocityServer.getServer();
server.start();

View File

@@ -7,6 +7,7 @@ import com.google.gson.GsonBuilder;
import com.velocitypowered.api.command.CommandInvoker;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.server.Favicon;
import com.velocitypowered.natives.util.Natives;
import com.velocitypowered.network.ConnectionManager;
import com.velocitypowered.proxy.command.ServerCommand;
@@ -17,6 +18,7 @@ import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.connection.http.NettyHttpClient;
import com.velocitypowered.api.server.ServerInfo;
import com.velocitypowered.proxy.command.CommandManager;
import com.velocitypowered.proxy.protocol.util.FaviconSerializer;
import com.velocitypowered.proxy.util.AddressUtil;
import com.velocitypowered.proxy.util.EncryptionUtils;
import com.velocitypowered.proxy.util.ServerMap;
@@ -44,6 +46,7 @@ public class VelocityServer implements ProxyServer {
private static final VelocityServer INSTANCE = new VelocityServer();
public static final Gson GSON = new GsonBuilder()
.registerTypeHierarchyAdapter(Component.class, new GsonComponentSerializer())
.registerTypeHierarchyAdapter(Favicon.class, new FaviconSerializer())
.create();
private final ConnectionManager cm = new ConnectionManager();

View File

@@ -2,6 +2,7 @@ package com.velocitypowered.proxy.config;
import com.google.common.collect.ImmutableMap;
import com.moandjiezana.toml.Toml;
import com.velocitypowered.api.server.Favicon;
import com.velocitypowered.proxy.util.AddressUtil;
import com.velocitypowered.api.util.LegacyChatColorUtils;
import net.kyori.text.Component;
@@ -15,6 +16,7 @@ import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -36,6 +38,7 @@ public class VelocityConfiguration {
private final int queryPort;
private Component motdAsComponent;
private Favicon favicon;
private VelocityConfiguration(String bind, String motd, int showMaxPlayers, boolean onlineMode,
IPForwardingMode ipForwardingMode, Map<String, String> servers,
@@ -124,9 +127,22 @@ public class VelocityConfiguration {
logger.warn("ALL packets going through the proxy are going to be compressed. This may hurt performance.");
}
loadFavicon();
return valid;
}
private void loadFavicon() {
Path faviconPath = Paths.get("server-icon.png");
if (Files.exists(faviconPath)) {
try {
this.favicon = Favicon.create(faviconPath);
} catch (Exception e) {
logger.info("Unable to load your server-icon.png, continuing without it.", e);
}
}
}
public InetSocketAddress getBind() {
return AddressUtil.parseAddress(bind);
}
@@ -182,6 +198,14 @@ public class VelocityConfiguration {
return compressionLevel;
}
public Favicon getFavicon() {
return favicon;
}
public static Logger getLogger() {
return logger;
}
@Override
public String toString() {
return "VelocityConfiguration{" +
@@ -197,6 +221,7 @@ public class VelocityConfiguration {
", queryEnabled=" + queryEnabled +
", queryPort=" + queryPort +
", motdAsComponent=" + motdAsComponent +
", favicon=" + favicon +
'}';
}

View File

@@ -41,7 +41,7 @@ public class StatusSessionHandler implements MinecraftSessionHandler {
new ServerPing.Version(shownVersion, "Velocity " + ProtocolConstants.SUPPORTED_GENERIC_VERSION_STRING),
new ServerPing.Players(VelocityServer.getServer().getPlayerCount(), configuration.getShowMaxPlayers()),
configuration.getMotdComponent(),
null
configuration.getFavicon()
);
StatusResponse response = new StatusResponse();
response.setStatus(VelocityServer.GSON.toJson(ping));

View File

@@ -1,14 +1,15 @@
package com.velocitypowered.proxy.data;
import com.velocitypowered.api.server.Favicon;
import net.kyori.text.Component;
public class ServerPing {
private final Version version;
private final Players players;
private final Component description;
private final String favicon;
private final Favicon favicon;
public ServerPing(Version version, Players players, Component description, String favicon) {
public ServerPing(Version version, Players players, Component description, Favicon favicon) {
this.version = version;
this.players = players;
this.description = description;
@@ -27,7 +28,7 @@ public class ServerPing {
return description;
}
public String getFavicon() {
public Favicon getFavicon() {
return favicon;
}

View File

@@ -0,0 +1,18 @@
package com.velocitypowered.proxy.protocol.util;
import com.google.gson.*;
import com.velocitypowered.api.server.Favicon;
import java.lang.reflect.Type;
public class FaviconSerializer implements JsonSerializer<Favicon>, JsonDeserializer<Favicon> {
@Override
public Favicon deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Favicon(json.getAsString());
}
@Override
public JsonElement serialize(Favicon src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getBase64Url());
}
}