Remove server list icon decoding as ImageIO/BufferedImage do not exist on Android.

This commit is contained in:
Steveice10 2020-06-03 14:58:49 -07:00
parent 4c315aa206
commit 1840d5aa3d
3 changed files with 9 additions and 29 deletions

View file

@ -128,7 +128,7 @@ public class MinecraftProtocolTest {
System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers() + " / " + info.getPlayerInfo().getMaxPlayers()); System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers() + " / " + info.getPlayerInfo().getMaxPlayers());
System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers())); System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers()));
System.out.println("Description: " + info.getDescription().getFullText()); System.out.println("Description: " + info.getDescription().getFullText());
System.out.println("Icon: " + info.getIcon()); System.out.println("Icon: " + info.getIconPng());
} }
}); });

View file

@ -16,5 +16,5 @@ public class ServerStatusInfo {
private @NonNull VersionInfo versionInfo; private @NonNull VersionInfo versionInfo;
private @NonNull PlayerInfo playerInfo; private @NonNull PlayerInfo playerInfo;
private @NonNull Message description; private @NonNull Message description;
private BufferedImage icon; private byte[] iconPng;
} }

View file

@ -20,10 +20,6 @@ import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
import lombok.Setter; import lombok.Setter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -55,7 +51,7 @@ public class StatusResponsePacket implements Packet {
PlayerInfo players = new PlayerInfo(plrs.get("max").getAsInt(), plrs.get("online").getAsInt(), profiles); PlayerInfo players = new PlayerInfo(plrs.get("max").getAsInt(), plrs.get("online").getAsInt(), profiles);
JsonElement desc = obj.get("description"); JsonElement desc = obj.get("description");
Message description = Message.fromJson(desc); Message description = Message.fromJson(desc);
BufferedImage icon = null; byte[] icon = null;
if(obj.has("favicon")) { if(obj.has("favicon")) {
icon = this.stringToIcon(obj.get("favicon").getAsString()); icon = this.stringToIcon(obj.get("favicon").getAsString());
} }
@ -87,8 +83,8 @@ public class StatusResponsePacket implements Packet {
obj.add("version", ver); obj.add("version", ver);
obj.add("players", plrs); obj.add("players", plrs);
obj.add("description", this.info.getDescription().toJson()); obj.add("description", this.info.getDescription().toJson());
if(this.info.getIcon() != null) { if(this.info.getIconPng() != null) {
obj.addProperty("favicon", this.iconToString(this.info.getIcon())); obj.addProperty("favicon", this.iconToString(this.info.getIconPng()));
} }
out.writeString(obj.toString()); out.writeString(obj.toString());
@ -99,31 +95,15 @@ public class StatusResponsePacket implements Packet {
return false; return false;
} }
private BufferedImage stringToIcon(String str) throws IOException { private byte[] stringToIcon(String str) throws IOException {
if(str.startsWith("data:image/png;base64,")) { if(str.startsWith("data:image/png;base64,")) {
str = str.substring("data:image/png;base64,".length()); str = str.substring("data:image/png;base64,".length());
} }
byte[] bytes = Base64.decode(str.getBytes(StandardCharsets.UTF_8)); return Base64.decode(str.getBytes(StandardCharsets.UTF_8));
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
BufferedImage icon = ImageIO.read(in);
in.close();
if(icon != null && (icon.getWidth() != 64 || icon.getHeight() != 64)) {
throw new IOException("Icon must be 64x64.");
}
return icon;
} }
private String iconToString(BufferedImage icon) throws IOException { private String iconToString(byte[] icon) throws IOException {
if(icon.getWidth() != 64 || icon.getHeight() != 64) { return "data:image/png;base64," + new String(Base64.encode(icon), StandardCharsets.UTF_8);
throw new IOException("Icon must be 64x64.");
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(icon, "PNG", out);
out.close();
byte[] encoded = Base64.encode(out.toByteArray());
return "data:image/png;base64," + new String(encoded, StandardCharsets.UTF_8);
} }
} }