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

@@ -0,0 +1,88 @@
package com.velocitypowered.api.server;
import com.google.common.base.Preconditions;
import javax.annotation.Nonnull;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.Objects;
/**
* Represents a Minecraft server favicon. A Minecraft server favicon is a 64x64 image that can be displayed to a remote
* client that sends a Server List Ping packet, and is automatically displayed in the Minecraft client.
*/
public final class Favicon {
private final String base64Url;
/**
* Directly create a favicon using its Base64 URL directly. You are generally better served by the create() series
* of functions.
* @param base64Url the url for use with this favicon
*/
public Favicon(@Nonnull String base64Url) {
this.base64Url = Preconditions.checkNotNull(base64Url, "base64Url");
}
/**
* Returns the Base64-encoded URI for this image.
* @return a URL representing this favicon
*/
public String getBase64Url() {
return base64Url;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Favicon favicon = (Favicon) o;
return Objects.equals(base64Url, favicon.base64Url);
}
@Override
public int hashCode() {
return Objects.hash(base64Url);
}
@Override
public String toString() {
return "Favicon{" +
"base64Url='" + base64Url + '\'' +
'}';
}
/**
* Creates a new {@code Favicon} from the specified {@code image}.
* @param image the image to use for the favicon
* @return the created {@link Favicon} instance
*/
public static Favicon create(@Nonnull BufferedImage image) {
Preconditions.checkNotNull(image, "image");
Preconditions.checkArgument(image.getWidth() == 64 && image.getHeight() == 64, "Image does not have" +
" 64x64 dimensions (found %sx%s)", image.getWidth(), image.getHeight());
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(image, "PNG", os);
} catch (IOException e) {
throw new AssertionError(e);
}
return new Favicon("data:image/png;base64," + Base64.getEncoder().encodeToString(os.toByteArray()));
}
/**
* Creates a new {@code Favicon} by reading the image from the specified {@code path}.
* @param path the path to the image to create a favicon for
* @return the created {@link Favicon} instance
*/
public static Favicon create(@Nonnull Path path) throws IOException {
try (InputStream stream = Files.newInputStream(path)) {
return create(ImageIO.read(stream));
}
}
}