diff --git a/example/com/github/steveice10/mc/protocol/test/MinecraftProtocolTest.java b/example/com/github/steveice10/mc/protocol/test/MinecraftProtocolTest.java index 7bf32867..7d3c4d97 100644 --- a/example/com/github/steveice10/mc/protocol/test/MinecraftProtocolTest.java +++ b/example/com/github/steveice10/mc/protocol/test/MinecraftProtocolTest.java @@ -128,7 +128,7 @@ public class MinecraftProtocolTest { System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers() + " / " + info.getPlayerInfo().getMaxPlayers()); System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers())); System.out.println("Description: " + info.getDescription().getFullText()); - System.out.println("Icon: " + info.getIcon()); + System.out.println("Icon: " + info.getIconPng()); } }); diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/status/ServerStatusInfo.java b/src/main/java/com/github/steveice10/mc/protocol/data/status/ServerStatusInfo.java index 054876ae..2bb81dd7 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/status/ServerStatusInfo.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/status/ServerStatusInfo.java @@ -16,5 +16,5 @@ public class ServerStatusInfo { private @NonNull VersionInfo versionInfo; private @NonNull PlayerInfo playerInfo; private @NonNull Message description; - private BufferedImage icon; + private byte[] iconPng; } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/status/server/StatusResponsePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/status/server/StatusResponsePacket.java index 7687c958..dccd2151 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/status/server/StatusResponsePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/status/server/StatusResponsePacket.java @@ -20,10 +20,6 @@ import lombok.NoArgsConstructor; import lombok.NonNull; 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.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); JsonElement desc = obj.get("description"); Message description = Message.fromJson(desc); - BufferedImage icon = null; + byte[] icon = null; if(obj.has("favicon")) { icon = this.stringToIcon(obj.get("favicon").getAsString()); } @@ -87,8 +83,8 @@ public class StatusResponsePacket implements Packet { obj.add("version", ver); obj.add("players", plrs); obj.add("description", this.info.getDescription().toJson()); - if(this.info.getIcon() != null) { - obj.addProperty("favicon", this.iconToString(this.info.getIcon())); + if(this.info.getIconPng() != null) { + obj.addProperty("favicon", this.iconToString(this.info.getIconPng())); } out.writeString(obj.toString()); @@ -99,31 +95,15 @@ public class StatusResponsePacket implements Packet { return false; } - private BufferedImage stringToIcon(String str) throws IOException { + private byte[] stringToIcon(String str) throws IOException { if(str.startsWith("data:image/png;base64,")) { str = str.substring("data:image/png;base64,".length()); } - byte[] bytes = 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; + return Base64.decode(str.getBytes(StandardCharsets.UTF_8)); } - private String iconToString(BufferedImage icon) throws IOException { - if(icon.getWidth() != 64 || icon.getHeight() != 64) { - 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); + private String iconToString(byte[] icon) throws IOException { + return "data:image/png;base64," + new String(Base64.encode(icon), StandardCharsets.UTF_8); } }