From ecfd348d9c1911a9eeb85f90e378fbdd1177d5a2 Mon Sep 17 00:00:00 2001 From: TheFaser Date: Fri, 22 Dec 2023 03:13:05 +0500 Subject: [PATCH] Server: add check for image size --- .../net/flectone/chat/manager/FileManager.java | 18 +++++++++++------- .../module/server/status/StatusListener.java | 10 +++++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/flectone/chat/manager/FileManager.java b/src/main/java/net/flectone/chat/manager/FileManager.java index 4fd874b..6dc918d 100644 --- a/src/main/java/net/flectone/chat/manager/FileManager.java +++ b/src/main/java/net/flectone/chat/manager/FileManager.java @@ -7,7 +7,10 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; @@ -22,7 +25,7 @@ public class FileManager { private static final String LANGUAGES_FOLDER = SETTINGS_FOLDER + "languages" + File.separator; private static final String ICONS_FOLDER = SETTINGS_FOLDER + "icons" + File.separator; - private final HashMap iconsMap = new HashMap<>(); + private final HashMap iconsMap = new HashMap<>(); @Getter private boolean isLess420; @@ -134,13 +137,14 @@ public static FConfiguration loadFile(String filePath) { return fileConfiguration; } - public File getIcon(String icon) { + public BufferedImage getIcon(String icon) { if (iconsMap.get(icon) != null) return iconsMap.get(icon); - File fileIcon = new File(DATA_FOLDER + ICONS_FOLDER + icon + ".png"); - iconsMap.put(icon, fileIcon); - - return fileIcon; + try { + File fileIcon = new File(DATA_FOLDER + ICONS_FOLDER + icon + ".png"); + iconsMap.put(icon, ImageIO.read(fileIcon)); + } catch (IOException ignored) {} + return null; } private void loadIcons() { @@ -148,7 +152,7 @@ private void loadIcons() { iconNames.add("maintenance"); iconNames.stream() - .filter(icon -> !getIcon(icon).exists() + .filter(icon -> getIcon(icon) == null && FlectoneChat.getPlugin().getResource(ICONS_FOLDER + icon + ".png") != null) .forEach(icon -> FlectoneChat.getPlugin().saveResource(ICONS_FOLDER + icon + ".png", false)); diff --git a/src/main/java/net/flectone/chat/module/server/status/StatusListener.java b/src/main/java/net/flectone/chat/module/server/status/StatusListener.java index cb2fe4f..9b82b8b 100644 --- a/src/main/java/net/flectone/chat/module/server/status/StatusListener.java +++ b/src/main/java/net/flectone/chat/module/server/status/StatusListener.java @@ -13,6 +13,7 @@ import org.bukkit.util.CachedServerIcon; import org.jetbrains.annotations.NotNull; +import java.awt.image.BufferedImage; import java.util.List; public class StatusListener extends FListener { @@ -76,7 +77,14 @@ public void playerLoginEvent(@NotNull PlayerLoginEvent event) { public void setIcon(@NotNull ServerListPingEvent event, @NotNull String iconName) { try { - CachedServerIcon serverIcon = Bukkit.loadServerIcon(FlectoneChat.getPlugin().getFileManager().getIcon(iconName)); + + BufferedImage bufferedImage = FlectoneChat.getPlugin().getFileManager().getIcon(iconName); + if (bufferedImage.getWidth() != 64 || bufferedImage.getHeight() != 64) { + FlectoneChat.warning("Unable to set icon " + iconName + ".png, size should be 64x64"); + return; + } + + CachedServerIcon serverIcon = Bukkit.loadServerIcon(bufferedImage); event.setServerIcon(serverIcon); } catch (Exception exception) { FlectoneChat.warning("Unable to load and install " + iconName + ".png image");