Reinstated null check and did some cleanup

This commit is contained in:
DerEnderKeks 2021-05-18 14:39:39 +02:00
parent af5804c92b
commit 84a014605a
2 changed files with 23 additions and 6 deletions

View File

@ -80,15 +80,21 @@ public class ImageMapDownloadCommand extends ImageMapSubCommand {
try (InputStream str = connection.getInputStream()) { try (InputStream str = connection.getInputStream()) {
BufferedImage image = ImageIO.read(str); BufferedImage image = ImageIO.read(str);
if (image == null) {
MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "Downloaded file is not an image!");
return;
}
File outFile = new File(plugin.getDataFolder(), "images" + File.separatorChar + filename); File outFile = new File(plugin.getDataFolder(), "images" + File.separatorChar + filename);
boolean fileExisted = outFile.exists(); boolean fileExisted = outFile.exists();
ImageIO.write(image, "PNG", outFile); ImageIO.write(image, "PNG", outFile);
if (fileExisted) { if (fileExisted) {
MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "File already exists, overwriting!"); MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "File already exists, overwriting!");
getPlugin().reloadImage(filename); getPlugin().reloadImage(filename);
} }
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "Downloaded file is not an image!"); MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "Received no data");
return; return;
} }
MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.NORMAL, "Download complete."); MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.NORMAL, "Download complete.");

View File

@ -374,21 +374,32 @@ public class ImageMaps extends JavaPlugin implements Listener {
public boolean deleteImage(String filename) { public boolean deleteImage(String filename) {
File file = new File(getDataFolder(), IMAGES_DIR + File.separatorChar + filename); File file = new File(getDataFolder(), IMAGES_DIR + File.separatorChar + filename);
boolean fileDeleted = false; boolean fileDeleted = false;
if (file.exists()) fileDeleted = file.delete(); if (file.exists()) {
fileDeleted = file.delete();
}
imageCache.remove(filename.toLowerCase()); imageCache.remove(filename.toLowerCase());
Iterator<Entry<ImageMap, Integer>> it = maps.entrySet().iterator(); Iterator<Entry<ImageMap, Integer>> it = maps.entrySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Entry<ImageMap, Integer> entry = it.next(); Entry<ImageMap, Integer> entry = it.next();
ImageMap imageMap = entry.getKey(); ImageMap imageMap = entry.getKey();
if (!imageMap.getFilename().equalsIgnoreCase(filename)) continue; if (!imageMap.getFilename().equalsIgnoreCase(filename)) {
Integer id = entry.getValue(); continue;
}
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
MapView map = Bukkit.getMap(id); MapView map = Bukkit.getMap(entry.getValue());
if (map == null) continue;
if (map == null) {
continue;
}
map.getRenderers().forEach(map::removeRenderer); map.getRenderers().forEach(map::removeRenderer);
it.remove(); it.remove();
} }
saveMaps(); saveMaps();
return fileDeleted; return fileDeleted;
} }