Skip to content

Commit 004f296

Browse files
committed
Add storages command
1 parent 0fce08d commit 004f296

File tree

9 files changed

+287
-65
lines changed

9 files changed

+287
-65
lines changed

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/Commands.java

Lines changed: 126 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,27 @@
3838
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
3939
import com.mojang.brigadier.context.CommandContext;
4040
import com.mojang.brigadier.tree.LiteralCommandNode;
41+
import de.bluecolored.bluemap.common.config.ConfigurationException;
4142
import de.bluecolored.bluemap.common.plugin.Plugin;
4243
import de.bluecolored.bluemap.common.plugin.PluginState;
4344
import de.bluecolored.bluemap.common.plugin.text.Text;
4445
import de.bluecolored.bluemap.common.plugin.text.TextColor;
4546
import de.bluecolored.bluemap.common.plugin.text.TextFormat;
46-
import de.bluecolored.bluemap.common.rendermanager.MapPurgeTask;
47-
import de.bluecolored.bluemap.common.rendermanager.MapUpdateTask;
48-
import de.bluecolored.bluemap.common.rendermanager.RenderTask;
49-
import de.bluecolored.bluemap.common.rendermanager.WorldRegionRenderTask;
47+
import de.bluecolored.bluemap.common.rendermanager.*;
5048
import de.bluecolored.bluemap.common.serverinterface.CommandSource;
5149
import de.bluecolored.bluemap.core.BlueMap;
5250
import de.bluecolored.bluemap.core.MinecraftVersion;
5351
import de.bluecolored.bluemap.core.debug.StateDumper;
5452
import de.bluecolored.bluemap.core.logger.Logger;
5553
import de.bluecolored.bluemap.core.map.BmMap;
5654
import de.bluecolored.bluemap.core.map.MapRenderState;
55+
import de.bluecolored.bluemap.core.storage.Storage;
5756
import de.bluecolored.bluemap.core.world.Block;
5857
import de.bluecolored.bluemap.core.world.World;
5958

6059
import java.io.IOException;
6160
import java.nio.file.Path;
62-
import java.util.ArrayList;
63-
import java.util.Arrays;
64-
import java.util.List;
65-
import java.util.Optional;
61+
import java.util.*;
6662
import java.util.function.Function;
6763
import java.util.function.Predicate;
6864

@@ -88,26 +84,22 @@ public Commands(Plugin plugin, CommandDispatcher<S> dispatcher, Function<S, Comm
8884

8985
public void init() {
9086
// commands
91-
LiteralCommandNode<S> baseCommand =
92-
literal("bluemap")
87+
LiteralCommandNode<S> baseCommand = literal("bluemap")
9388
.requires(requirementsUnloaded("bluemap.status"))
9489
.executes(this::statusCommand)
9590
.build();
9691

97-
LiteralCommandNode<S> versionCommand =
98-
literal("version")
92+
LiteralCommandNode<S> versionCommand = literal("version")
9993
.requires(requirementsUnloaded("bluemap.version"))
10094
.executes(this::versionCommand)
10195
.build();
10296

103-
LiteralCommandNode<S> helpCommand =
104-
literal("help")
97+
LiteralCommandNode<S> helpCommand = literal("help")
10598
.requires(requirementsUnloaded("bluemap.help"))
10699
.executes(this::helpCommand)
107100
.build();
108101

109-
LiteralCommandNode<S> reloadCommand =
110-
literal("reload")
102+
LiteralCommandNode<S> reloadCommand = literal("reload")
111103
.requires(requirementsUnloaded("bluemap.reload"))
112104
.executes(context -> this.reloadCommand(context, false))
113105

@@ -116,8 +108,7 @@ public void init() {
116108

117109
.build();
118110

119-
LiteralCommandNode<S> debugCommand =
120-
literal("debug")
111+
LiteralCommandNode<S> debugCommand = literal("debug")
121112
.requires(requirementsUnloaded("bluemap.debug"))
122113

123114
.then(literal("block")
@@ -147,31 +138,27 @@ public void init() {
147138

148139
.build();
149140

150-
LiteralCommandNode<S> stopCommand =
151-
literal("stop")
141+
LiteralCommandNode<S> stopCommand = literal("stop")
152142
.requires(requirements("bluemap.stop"))
153143
.executes(this::stopCommand)
154144
.build();
155145

156-
LiteralCommandNode<S> startCommand =
157-
literal("start")
146+
LiteralCommandNode<S> startCommand = literal("start")
158147
.requires(requirements("bluemap.start"))
159148
.executes(this::startCommand)
160149
.build();
161150

162-
LiteralCommandNode<S> freezeCommand =
163-
literal("freeze")
151+
LiteralCommandNode<S> freezeCommand = literal("freeze")
164152
.requires(requirements("bluemap.freeze"))
165153
.then(argument("map", StringArgumentType.string()).suggests(new MapSuggestionProvider<>(plugin))
166154
.executes(this::freezeCommand))
167155
.build();
168156

169-
LiteralCommandNode<S> unfreezeCommand =
170-
literal("unfreeze")
171-
.requires(requirements("bluemap.freeze"))
172-
.then(argument("map", StringArgumentType.string()).suggests(new MapSuggestionProvider<>(plugin))
173-
.executes(this::unfreezeCommand))
174-
.build();
157+
LiteralCommandNode<S> unfreezeCommand = literal("unfreeze")
158+
.requires(requirements("bluemap.freeze"))
159+
.then(argument("map", StringArgumentType.string()).suggests(new MapSuggestionProvider<>(plugin))
160+
.executes(this::unfreezeCommand))
161+
.build();
175162

176163
LiteralCommandNode<S> forceUpdateCommand =
177164
addRenderArguments(
@@ -187,33 +174,42 @@ public void init() {
187174
this::updateCommand
188175
).build();
189176

190-
LiteralCommandNode<S> purgeCommand =
191-
literal("purge")
192-
.requires(requirements("bluemap.purge"))
193-
.then(argument("map", StringArgumentType.string()).suggests(new MapSuggestionProvider<>(plugin))
194-
.executes(this::purgeCommand))
195-
.build();
177+
LiteralCommandNode<S> purgeCommand = literal("purge")
178+
.requires(requirements("bluemap.purge"))
179+
.then(argument("map", StringArgumentType.string()).suggests(new MapSuggestionProvider<>(plugin))
180+
.executes(this::purgeCommand))
181+
.build();
196182

197-
LiteralCommandNode<S> cancelCommand =
198-
literal("cancel")
183+
LiteralCommandNode<S> cancelCommand = literal("cancel")
199184
.requires(requirements("bluemap.cancel"))
200185
.executes(this::cancelCommand)
201186
.then(argument("task-ref", StringArgumentType.string()).suggests(new TaskRefSuggestionProvider<>(helper))
202187
.executes(this::cancelCommand))
203188
.build();
204189

205-
LiteralCommandNode<S> worldsCommand =
206-
literal("worlds")
190+
LiteralCommandNode<S> worldsCommand = literal("worlds")
207191
.requires(requirements("bluemap.status"))
208192
.executes(this::worldsCommand)
209193
.build();
210194

211-
LiteralCommandNode<S> mapsCommand =
212-
literal("maps")
195+
LiteralCommandNode<S> mapsCommand = literal("maps")
213196
.requires(requirements("bluemap.status"))
214197
.executes(this::mapsCommand)
215198
.build();
216199

200+
LiteralCommandNode<S> storagesCommand = literal("storages")
201+
.requires(requirements("bluemap.status"))
202+
.executes(this::storagesCommand)
203+
204+
.then(argument("storage", StringArgumentType.string()).suggests(new StorageSuggestionProvider<>(plugin))
205+
.executes(this::storagesInfoCommand)
206+
207+
.then(literal("delete")
208+
.then(argument("map", StringArgumentType.string())
209+
.executes(this::storagesDeleteMapCommand))))
210+
211+
.build();
212+
217213
// command tree
218214
dispatcher.getRoot().addChild(baseCommand);
219215
baseCommand.addChild(versionCommand);
@@ -230,6 +226,7 @@ public void init() {
230226
baseCommand.addChild(purgeCommand);
231227
baseCommand.addChild(worldsCommand);
232228
baseCommand.addChild(mapsCommand);
229+
baseCommand.addChild(storagesCommand);
233230
}
234231

235232
private <B extends ArgumentBuilder<S, B>> B addRenderArguments(B builder, Command<S> command) {
@@ -862,4 +859,91 @@ public int mapsCommand(CommandContext<S> context) {
862859
return 1;
863860
}
864861

862+
public int storagesCommand(CommandContext<S> context) {
863+
CommandSource source = commandSourceInterface.apply(context.getSource());
864+
865+
source.sendMessage(Text.of(TextColor.BLUE, "Storages loaded by BlueMap:"));
866+
for (var entry : plugin.getBlueMap().getConfigs().getStorageConfigs().entrySet()) {
867+
source.sendMessage(Text.of(TextColor.GRAY, " - ", TextColor.WHITE, entry.getKey())
868+
.setHoverText(Text.of(entry.getValue().getStorageType().name()))
869+
.setClickAction(Text.ClickAction.RUN_COMMAND, "/bluemap storages " + entry.getKey())
870+
);
871+
}
872+
873+
return 1;
874+
}
875+
876+
public int storagesInfoCommand(CommandContext<S> context) {
877+
CommandSource source = commandSourceInterface.apply(context.getSource());
878+
String storageId = context.getArgument("storage", String.class);
879+
880+
Storage storage;
881+
try {
882+
storage = plugin.getBlueMap().getStorage(storageId);
883+
} catch (ConfigurationException ex) {
884+
source.sendMessage(Text.of(TextColor.RED, ex.getMessage()));
885+
return 0;
886+
}
887+
888+
Collection<String> mapIds;
889+
try {
890+
mapIds = storage.collectMapIds();
891+
} catch (IOException ex) {
892+
source.sendMessage(Text.of(TextColor.RED, "There was an unexpected exception trying to access this storage. Please check the console for more details..."));
893+
Logger.global.logError("Unexpected exception trying to load mapId's from storage '" + storageId + "'!", ex);
894+
return 0;
895+
}
896+
897+
source.sendMessage(Text.of(TextColor.BLUE, "Storage '", storageId, "':"));
898+
if (mapIds.isEmpty()) {
899+
source.sendMessage(Text.of(TextColor.GRAY, " <empty storage>"));
900+
} else {
901+
for (String mapId : mapIds) {
902+
BmMap map = plugin.getMaps().get(mapId);
903+
boolean isLoaded = map != null && map.getStorage().equals(storage);
904+
905+
if (isLoaded) {
906+
source.sendMessage(Text.of(TextColor.GRAY, " - ", TextColor.WHITE, mapId, TextColor.GREEN, TextFormat.ITALIC, " (loaded)"));
907+
} else {
908+
source.sendMessage(Text.of(TextColor.GRAY, " - ", TextColor.WHITE, mapId, TextColor.DARK_GRAY, TextFormat.ITALIC, " (unloaded/static/remote)"));
909+
}
910+
}
911+
}
912+
913+
return 1;
914+
}
915+
916+
public int storagesDeleteMapCommand(CommandContext<S> context) {
917+
CommandSource source = commandSourceInterface.apply(context.getSource());
918+
String storageId = context.getArgument("storage", String.class);
919+
String mapId = context.getArgument("map", String.class);
920+
921+
Storage storage;
922+
try {
923+
storage = plugin.getBlueMap().getStorage(storageId);
924+
} catch (ConfigurationException ex) {
925+
source.sendMessage(Text.of(TextColor.RED, ex.getMessage()));
926+
return 0;
927+
}
928+
929+
BmMap map = plugin.getMaps().get(mapId);
930+
boolean isLoaded = map != null && map.getStorage().equals(storage);
931+
if (isLoaded) {
932+
Text purgeCommand = Text.of(TextColor.WHITE, "/bluemap purge " + mapId)
933+
.setClickAction(Text.ClickAction.SUGGEST_COMMAND, "/bluemap purge " + mapId);
934+
source.sendMessage(Text.of(TextColor.RED, "Can't delete a loaded map!\n" +
935+
"Unload the map by removing it's config-file first,\n" +
936+
"or use ", purgeCommand, " if you want to purge it."));
937+
return 0;
938+
}
939+
940+
// delete map
941+
StorageDeleteTask deleteTask = new StorageDeleteTask(storage, mapId);
942+
943+
plugin.getRenderManager().scheduleRenderTaskNext(deleteTask);
944+
source.sendMessage(Text.of(TextColor.GREEN, "Created new Task to delete map '" + mapId + "' from storage '" + storageId + "'"));
945+
946+
return 1;
947+
}
948+
865949
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of BlueMap, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package de.bluecolored.bluemap.common.plugin.commands;
26+
27+
import de.bluecolored.bluemap.common.plugin.Plugin;
28+
29+
import java.util.Collection;
30+
31+
public class StorageSuggestionProvider<S> extends AbstractSuggestionProvider<S> {
32+
33+
private final Plugin plugin;
34+
35+
public StorageSuggestionProvider(Plugin plugin) {
36+
this.plugin = plugin;
37+
}
38+
39+
@Override
40+
public Collection<String> getPossibleValues() {
41+
return plugin.getBlueMap().getConfigs().getStorageConfigs().keySet();
42+
}
43+
44+
}

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/TaskRefSuggestionProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
public class TaskRefSuggestionProvider<S> extends AbstractSuggestionProvider<S> {
3030

31-
private CommandHelper helper;
31+
private final CommandHelper helper;
3232

3333
public TaskRefSuggestionProvider(CommandHelper helper) {
3434
this.helper = helper;

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/WorldOrMapSuggestionProvider.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@
2424
*/
2525
package de.bluecolored.bluemap.common.plugin.commands;
2626

27-
import java.util.Collection;
28-
import java.util.HashSet;
29-
30-
import de.bluecolored.bluemap.core.map.BmMap;
3127
import de.bluecolored.bluemap.common.plugin.Plugin;
3228
import de.bluecolored.bluemap.core.world.World;
3329

30+
import java.util.Collection;
31+
import java.util.HashSet;
32+
3433
public class WorldOrMapSuggestionProvider<S> extends AbstractSuggestionProvider<S> {
3534

36-
private Plugin plugin;
35+
private final Plugin plugin;
3736

3837
public WorldOrMapSuggestionProvider(Plugin plugin) {
3938
this.plugin = plugin;

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/rendermanager/MapPurgeTask.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public void doWork() throws Exception {
5050
if (!this.hasMoreWork) return;
5151
this.hasMoreWork = false;
5252
}
53+
if (this.cancelled) return;
5354

5455
// save lowres-tile-manager to clear/flush any buffered data
5556
this.map.getLowresTileManager().save();
@@ -97,7 +98,7 @@ public boolean contains(RenderTask task) {
9798

9899
@Override
99100
public String getDescription() {
100-
return "Purge Map " + map.getId();
101+
return "Purge map " + map.getId();
101102
}
102103

103104
}

0 commit comments

Comments
 (0)