diff --git a/src/main/java/net/craftcitizen/imagemaps/ImageMapCommandHandler.java b/src/main/java/net/craftcitizen/imagemaps/ImageMapCommandHandler.java index bc4fbec..f27d473 100644 --- a/src/main/java/net/craftcitizen/imagemaps/ImageMapCommandHandler.java +++ b/src/main/java/net/craftcitizen/imagemaps/ImageMapCommandHandler.java @@ -6,6 +6,7 @@ public class ImageMapCommandHandler extends CommandHandler { public ImageMapCommandHandler(ImageMaps plugin) { super(plugin); registerSubCommand("download", new ImageMapDownloadCommand(plugin)); + registerSubCommand("delete", new ImageMapDeleteCommand(plugin)); registerSubCommand("place", new ImageMapPlaceCommand(plugin)); registerSubCommand("info", new ImageMapInfoCommand(plugin)); registerSubCommand("list", new ImageMapListCommand(plugin)); diff --git a/src/main/java/net/craftcitizen/imagemaps/ImageMapDeleteCommand.java b/src/main/java/net/craftcitizen/imagemaps/ImageMapDeleteCommand.java new file mode 100644 index 0000000..d119244 --- /dev/null +++ b/src/main/java/net/craftcitizen/imagemaps/ImageMapDeleteCommand.java @@ -0,0 +1,64 @@ +package net.craftcitizen.imagemaps; + +import de.craftlancer.core.Utils; +import de.craftlancer.core.util.MessageLevel; +import de.craftlancer.core.util.MessageUtil; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +import java.io.File; +import java.util.Collections; +import java.util.List; + +public class ImageMapDeleteCommand extends ImageMapSubCommand { + + public ImageMapDeleteCommand(ImageMaps plugin) { + super("imagemaps.delete", plugin, true); + } + + @Override + protected String execute(CommandSender sender, Command cmd, String label, String[] args) { + if (!checkSender(sender)) { + MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "You can't run this command."); + return null; + } + + if (args.length < 2) { + MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "You must specify a file name."); + return null; + } + + String filename = args[1]; + + if (filename.contains("/") || filename.contains("\\") || filename.contains(":")) { + MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "Filename contains illegal character."); + return null; + } + + if (!getPlugin().hasImage(filename)) { + MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "No image with this name exists."); + return null; + } + + if (getPlugin().deleteImage(filename)) { + MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.NORMAL, "File deleted."); + } else { + MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.WARNING, "Failed to delete file."); + } + return null; + } + + @Override + public void help(CommandSender sender) { + MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.NORMAL, "Deletes an image."); + MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.INFO, "Usage: /imagemap delete "); + } + + @Override + protected List onTabComplete(CommandSender sender, String[] args) { + if (args.length == 2) + return Utils.getMatches(args[1], new File(plugin.getDataFolder(), "images").list()); + + return Collections.emptyList(); + } +} diff --git a/src/main/java/net/craftcitizen/imagemaps/ImageMapHelpCommand.java b/src/main/java/net/craftcitizen/imagemaps/ImageMapHelpCommand.java index 579fbe1..31ee186 100644 --- a/src/main/java/net/craftcitizen/imagemaps/ImageMapHelpCommand.java +++ b/src/main/java/net/craftcitizen/imagemaps/ImageMapHelpCommand.java @@ -30,6 +30,7 @@ public class ImageMapHelpCommand extends HelpCommand { MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.NORMAL, buildMessage("/imagemap place [size]", " - starts image placement")); MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.NORMAL, buildMessage("/imagemap download ", " - downloads an image")); + MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.NORMAL, buildMessage("/imagemap delete ", " - deletes an image")); MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.NORMAL, buildMessage("/imagemap info ", " - displays image info")); MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.NORMAL, buildMessage("/imagemap reload ", " - reloads an image from disk")); MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.NORMAL, buildMessage("/imagemap list [page]", " - lists all files in the images folder")); diff --git a/src/main/java/net/craftcitizen/imagemaps/ImageMapInfoCommand.java b/src/main/java/net/craftcitizen/imagemaps/ImageMapInfoCommand.java index e738c81..22d8432 100644 --- a/src/main/java/net/craftcitizen/imagemaps/ImageMapInfoCommand.java +++ b/src/main/java/net/craftcitizen/imagemaps/ImageMapInfoCommand.java @@ -50,11 +50,16 @@ public class ImageMapInfoCommand extends ImageMapSubCommand { BaseComponent placeAction = new TextComponent("[Place]"); placeAction.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("/imagemap place \"%s\"", filename))); placeAction.setColor(ChatColor.GOLD); + BaseComponent deleteAction = new TextComponent("[Delete]"); + deleteAction.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("/imagemap delete \"%s\"", filename))); + deleteAction.setColor(ChatColor.RED); BaseComponent actions = new TextComponent("Action: "); actions.addExtra(reloadAction); actions.addExtra(" "); actions.addExtra(placeAction); + actions.addExtra(" "); + actions.addExtra(deleteAction); MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.INFO, "Image Information: "); MessageUtil.sendMessage(getPlugin(), sender, MessageLevel.NORMAL, String.format("File Name: %s", filename)); diff --git a/src/main/java/net/craftcitizen/imagemaps/ImageMapListCommand.java b/src/main/java/net/craftcitizen/imagemaps/ImageMapListCommand.java index 65661fc..eae95f3 100644 --- a/src/main/java/net/craftcitizen/imagemaps/ImageMapListCommand.java +++ b/src/main/java/net/craftcitizen/imagemaps/ImageMapListCommand.java @@ -44,6 +44,9 @@ public class ImageMapListCommand extends ImageMapSubCommand { BaseComponent placeAction = new TextComponent("[Place]"); placeAction.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("/imagemap place \"%s\"", filename))); placeAction.setColor(ChatColor.GOLD); + BaseComponent deleteAction = new TextComponent("[Delete]"); + deleteAction.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format("/imagemap delete \"%s\"", filename))); + deleteAction.setColor(ChatColor.RED); BaseComponent message = new TextComponent(filename); message.setColor(even ? ChatColor.GRAY : ChatColor.WHITE); @@ -53,6 +56,8 @@ public class ImageMapListCommand extends ImageMapSubCommand { message.addExtra(reloadAction); message.addExtra(" "); message.addExtra(placeAction); + message.addExtra(" "); + message.addExtra(deleteAction); MessageUtil.sendMessage(plugin, sender, MessageLevel.NORMAL, message); even = !even; diff --git a/src/main/java/net/craftcitizen/imagemaps/ImageMaps.java b/src/main/java/net/craftcitizen/imagemaps/ImageMaps.java index f1ec580..9fe7cfd 100644 --- a/src/main/java/net/craftcitizen/imagemaps/ImageMaps.java +++ b/src/main/java/net/craftcitizen/imagemaps/ImageMaps.java @@ -4,6 +4,7 @@ import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.logging.Level; @@ -370,7 +371,28 @@ public class ImageMaps extends JavaPlugin implements Listener { return PlacementResult.SUCCESS; } - + + public boolean deleteImage(String filename) { + File file = new File(getDataFolder(), IMAGES_DIR + File.separatorChar + filename); + boolean fileDeleted = false; + if (file.exists()) fileDeleted = file.delete(); + imageCache.remove(filename.toLowerCase()); + Iterator> it = maps.entrySet().iterator(); + while (it.hasNext()) { + Entry entry = it.next(); + ImageMap imageMap = entry.getKey(); + if (!imageMap.getFilename().equalsIgnoreCase(filename)) continue; + Integer id = entry.getValue(); + @SuppressWarnings("deprecation") + MapView map = Bukkit.getMap(id); + if (map == null) continue; + map.getRenderers().forEach(map::removeRenderer); + it.remove(); + } + saveMaps(); + return fileDeleted; + } + @SuppressWarnings("deprecation") public boolean reloadImage(String filename) { if (!imageCache.containsKey(filename.toLowerCase())) diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2936e80..ded9526 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -10,6 +10,7 @@ commands: usage: | /imagemap place [frameVisible] [frameFixed] [size] - starts image placement /imagemap download - downloads an image + /imagemap delete - deletes an image /imagemap info - displays image info /imagemap reload - reloads an image from disk /imagemap list [page] - lists all files in the images folder @@ -20,6 +21,7 @@ permissions: children: imagemaps.place: true imagemaps.download: true + imagemaps.delete: true imagemaps.info: true imagemaps.list: true imagemaps.reload: true @@ -29,7 +31,9 @@ permissions: imagemaps.place: default: op imagemaps.download: - default: op + default: op + imagemaps.delete: + default: op imagemaps.info: default: op imagemaps.list: