From a5817d0897e0946675cc391fd08244d9df436fa3 Mon Sep 17 00:00:00 2001 From: "Blue (Lukas Rieger)" Date: Sun, 30 Aug 2020 22:25:38 +0200 Subject: [PATCH] Add purge command --- .../common/plugin/commands/Commands.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/Commands.java b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/Commands.java index 65169787..c45817d7 100644 --- a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/Commands.java +++ b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/Commands.java @@ -24,12 +24,15 @@ */ package de.bluecolored.bluemap.common.plugin.commands; +import java.io.File; import java.io.IOException; import java.util.Optional; import java.util.UUID; import java.util.function.Function; import java.util.function.Predicate; +import org.apache.commons.io.FileUtils; + import com.flowpowered.math.vector.Vector2i; import com.flowpowered.math.vector.Vector3d; import com.flowpowered.math.vector.Vector3i; @@ -150,6 +153,13 @@ public void init() { .executes(this::renderCommand))))) // /bluemap render .build(); + LiteralCommandNode purgeCommand = + literal("purge") + .requires(requirements("bluemap.render")) + .then(argument("map", StringArgumentType.string()).suggests(new MapSuggestionProvider<>(plugin)) + .executes(this::purgeCommand)) + .build(); + LiteralCommandNode prioRenderCommand = literal("prioritize") .requires(requirements("bluemap.render")) @@ -211,6 +221,7 @@ public void init() { baseCommand.addChild(pauseCommand); baseCommand.addChild(resumeCommand); baseCommand.addChild(renderCommand); + baseCommand.addChild(purgeCommand); renderCommand.addChild(prioRenderCommand); renderCommand.addChild(cancelRenderCommand); baseCommand.addChild(worldsCommand); @@ -476,6 +487,31 @@ public int renderCommand(CommandContext context) { return 1; } + public int purgeCommand(CommandContext context) { + CommandSource source = commandSourceInterface.apply(context.getSource()); + + // parse map argument + String mapId = context.getArgument("map", String.class); + + new Thread(() -> { + try { + File mapFolder = new File(plugin.getRenderConfig().getWebRoot(), "data" + File.separator + mapId); + if (!mapFolder.exists() || !mapFolder.isDirectory()) { + source.sendMessage(Text.of(TextColor.RED, "There is no map-data to purge for the map-id '" + mapId + "'!")); + return; + } + + FileUtils.deleteDirectory(mapFolder); + source.sendMessage(Text.of(TextColor.GREEN, "Map '" + mapId + "' has been successfully purged!")); + } catch (IOException | IllegalArgumentException e) { + source.sendMessage(Text.of(TextColor.RED, "There was an error trying to purge '" + mapId + "', see console for details.")); + Logger.global.logError("Failed to purge map '" + mapId + "'!", e); + } + }).start(); + + return 1; + } + public int prioritizeRenderTaskCommand(CommandContext context) { CommandSource source = commandSourceInterface.apply(context.getSource());