Skip to content

Commit

Permalink
✨ Status favicon is now configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
YvanMazy committed Apr 24, 2024
1 parent 0b69e27 commit 12e6b9f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ interface Status {

String getProtocol();

String getFaviconPath();

}

interface Miscellaneous {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ private static class YamlStatus implements ProxyConfiguration.Status {
private final String name;
private final String description;
private final String protocol;
private final String faviconPath;

private YamlStatus() {
this.name = "TransferProxy";
this.description = "<green>A TransferProxy server";
this.protocol = "AUTO";
this.faviconPath = "./favicon.png";
}

@Override
Expand All @@ -139,6 +141,11 @@ public String getProtocol() {
return this.protocol;
}

@Override
public String getFaviconPath() {
return this.faviconPath;
}

}

private static class YamlMiscellaneous implements ProxyConfiguration.Miscellaneous {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,27 @@
import be.darkkraft.transferproxy.api.event.listener.StatusListener;
import be.darkkraft.transferproxy.api.network.connection.PlayerConnection;
import be.darkkraft.transferproxy.api.status.StatusResponse;
import be.darkkraft.transferproxy.api.util.IOUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public final class DefaultStatusListener implements StatusListener {

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultStatusListener.class);

private final String name;
private final Component description;
private final int protocol;
private final boolean autoProtocol;
private final String favicon;

public DefaultStatusListener() {
final ProxyConfiguration.Status config = TransferProxy.getInstance().getConfiguration().getStatus();
Expand All @@ -52,6 +63,7 @@ public DefaultStatusListener() {
this.autoProtocol = false;
this.protocol = parseProtocol(rawProtocol);
}
this.favicon = readFavicon(Path.of(config.getFaviconPath()));
}

@Override
Expand All @@ -60,6 +72,7 @@ public void handle(final @NotNull PlayerConnection connection) {
.name(this.name)
.description(this.description)
.protocol(this.autoProtocol ? connection.getProtocol() : this.protocol)
.favicon(this.favicon)
.build());
}

Expand All @@ -71,4 +84,15 @@ private static int parseProtocol(final @NotNull String rawProtocol) {
}
}

private static @Nullable String readFavicon(final Path path) {
if (Files.isRegularFile(path)) {
try {
return IOUtil.createImage(path);
} catch (final IOException e) {
LOGGER.error("Failed to read favicon", e);
}
}
return null;
}

}
3 changes: 3 additions & 0 deletions api/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ status:
# Sets server protocol version. 'AUTO' automatically matches client's version.
# You can also directly specify the protocol number, such as 766.
protocol: "AUTO"
# The path of the file corresponding to the favicon to display.
# If the file is not found, nothing will be displayed.
favicon-path: "./favicon.png"

miscellaneous:
# Enables kicking players with outdated Minecraft versions.
Expand Down

0 comments on commit 12e6b9f

Please sign in to comment.