Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Server: add check for image size
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFaser committed Dec 21, 2023
1 parent 288beb5 commit ecfd348
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/main/java/net/flectone/chat/manager/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, File> iconsMap = new HashMap<>();
private final HashMap<String, BufferedImage> iconsMap = new HashMap<>();

@Getter
private boolean isLess420;
Expand Down Expand Up @@ -134,21 +137,22 @@ 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() {
List<String> iconNames = config.getStringList("default.server.status.icon.names");
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit ecfd348

Please sign in to comment.