diff --git a/pom.xml b/pom.xml index 21fdf61..64c982d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ cn.lunadeer ColorfulMap - 2.5 + 2.6 jar ColorfulMap diff --git a/src/main/java/cn/lunadeer/colorfulmap/ColorfulMap.java b/src/main/java/cn/lunadeer/colorfulmap/ColorfulMap.java index 29014d0..e292571 100644 --- a/src/main/java/cn/lunadeer/colorfulmap/ColorfulMap.java +++ b/src/main/java/cn/lunadeer/colorfulmap/ColorfulMap.java @@ -1,5 +1,6 @@ package cn.lunadeer.colorfulmap; +import cn.lunadeer.colorfulmap.commands.Purge; import cn.lunadeer.colorfulmap.commands.Reload; import cn.lunadeer.colorfulmap.commands.ToMap; import cn.lunadeer.colorfulmap.utils.*; @@ -22,6 +23,7 @@ public final class ColorfulMap extends JavaPlugin { Objects.requireNonNull(Bukkit.getPluginCommand("tomap")).setExecutor(new ToMap()); Objects.requireNonNull(Bukkit.getPluginCommand("reloadColorfulMap")).setExecutor(new Reload()); + Objects.requireNonNull(Bukkit.getPluginCommand("purgeColorfulMap")).setExecutor(new Purge()); Bukkit.getPluginManager().registerEvents(new Events(), this); diff --git a/src/main/java/cn/lunadeer/colorfulmap/MapManager.java b/src/main/java/cn/lunadeer/colorfulmap/MapManager.java index 564f81f..42aeafb 100644 --- a/src/main/java/cn/lunadeer/colorfulmap/MapManager.java +++ b/src/main/java/cn/lunadeer/colorfulmap/MapManager.java @@ -122,6 +122,27 @@ public class MapManager implements Listener { return savedImages.get(world_name.getName()).get(id); } + public List getMapIds(World world) { + if (!savedImages.containsKey(world.getName())) { + return new ArrayList<>(); + } + return new ArrayList<>(savedImages.get(world.getName()).keySet()); + } + + public void removeMap(World world, Integer id) { + if (!savedImages.containsKey(world.getName())) { + return; + } + FileConfiguration config = dataFile.getConfig(); + String path = config.getString(world.getName() + "." + id); + if (path == null) { + return; + } + new File(path).delete(); + config.set(world.getName() + "." + id, null); + dataFile.saveConfig(); + savedImages.get(world.getName()).remove(id); + } static class MapsFile { diff --git a/src/main/java/cn/lunadeer/colorfulmap/StorageMaps.java b/src/main/java/cn/lunadeer/colorfulmap/StorageMaps.java index 02eb49f..1acd9d9 100644 --- a/src/main/java/cn/lunadeer/colorfulmap/StorageMaps.java +++ b/src/main/java/cn/lunadeer/colorfulmap/StorageMaps.java @@ -110,4 +110,35 @@ public class StorageMaps { return null; } } + + public static void purgeStorageFolder() { + File[] map_folders = new File(data_folder, "maps").listFiles(); + if (map_folders == null) { + return; + } + for (File map_folder : map_folders) { + if (!map_folder.isDirectory()) { + continue; + } + File[] files = map_folder.listFiles(); + if (files == null) { + return; + } + if (files.length == 0) { + map_folder.delete(); + continue; + } + if (files.length == 3) { + for (File file : files) { + if (!file.getName().equals("meta.txt") && !file.getName().equals("raw.png") && !file.getName().equals("thumb.png")) { + return; + } + } + for (File file : files) { + file.delete(); + } + map_folder.delete(); + } + } + } } diff --git a/src/main/java/cn/lunadeer/colorfulmap/commands/Purge.java b/src/main/java/cn/lunadeer/colorfulmap/commands/Purge.java new file mode 100644 index 0000000..0db3f87 --- /dev/null +++ b/src/main/java/cn/lunadeer/colorfulmap/commands/Purge.java @@ -0,0 +1,49 @@ +package cn.lunadeer.colorfulmap.commands; + +import cn.lunadeer.colorfulmap.ColorfulMap; +import cn.lunadeer.colorfulmap.MapManager; +import cn.lunadeer.colorfulmap.StorageMaps; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Item; +import org.bukkit.entity.ItemFrame; +import org.bukkit.inventory.ItemStack; +import org.bukkit.map.MapView; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +public class Purge implements CommandExecutor { + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + ColorfulMap.instance.getServer().getWorlds().forEach(world -> { + List worldMapIds = new ArrayList<>(); + world.getEntities().forEach(entity -> { + if (!(entity instanceof org.bukkit.entity.ItemFrame)) { + return; + } + ItemFrame itemFrame = (ItemFrame) entity; + if (!itemFrame.getItem().getType().equals(org.bukkit.Material.FILLED_MAP)) { + return; + } + ItemStack item = itemFrame.getItem(); + MapView mapView = ((org.bukkit.inventory.meta.MapMeta) item.getItemMeta()).getMapView(); + if (mapView == null) { + return; + } + worldMapIds.add(mapView.getId()); + }); + List currentIds = MapManager.instance.getMapIds(world); + for (Integer id : currentIds) { + if (!worldMapIds.contains(id)) { + MapManager.instance.removeMap(world, id); + } + } + }); + StorageMaps.purgeStorageFolder(); + MapManager.instance.reloadImages(); + return true; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 304b3c7..cb0014f 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -13,6 +13,10 @@ commands: description: 重载ColorfulMap配置 usage: /reloadColorfulMap permission: colorfulmap.reload + purgeColorfulMap: + description: 清除无效的地图画 + usage: /purgeColorfulMap + permission: colorfulmap.purge permissions: colorfulmap.tomap: description: 允许使用/tomap命令 @@ -20,3 +24,6 @@ permissions: colorfulmap.reload: description: 允许使用/reloadColorfulMap命令 default: op + colorfulmap.purge: + description: 允许使用/purgeColorfulMap命令 + default: op