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()) {
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);
boolean fileExisted = outFile.exists();
ImageIO.write(image, "PNG", outFile);
if (fileExisted) {
MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "File already exists, overwriting!");
getPlugin().reloadImage(filename);
}
} 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;
}
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) {
File file = new File(getDataFolder(), IMAGES_DIR + File.separatorChar + filename);
boolean fileDeleted = false;
if (file.exists()) fileDeleted = file.delete();
if (file.exists()) {
fileDeleted = file.delete();
}
imageCache.remove(filename.toLowerCase());
Iterator<Entry<ImageMap, Integer>> it = maps.entrySet().iterator();
while (it.hasNext()) {
Entry<ImageMap, Integer> entry = it.next();
ImageMap imageMap = entry.getKey();
if (!imageMap.getFilename().equalsIgnoreCase(filename)) continue;
Integer id = entry.getValue();
if (!imageMap.getFilename().equalsIgnoreCase(filename)) {
continue;
}
@SuppressWarnings("deprecation")
MapView map = Bukkit.getMap(id);
if (map == null) continue;
MapView map = Bukkit.getMap(entry.getValue());
if (map == null) {
continue;
}
map.getRenderers().forEach(map::removeRenderer);
it.remove();
}
saveMaps();
return fileDeleted;
}