Skip to content

Commit 397762d

Browse files
committed
Fix storages command not detecting if a map is loaded or not correctly
1 parent fddf823 commit 397762d

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

common/src/main/java/de/bluecolored/bluemap/common/commands/commands/StoragesCommand.java

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@
3737
import de.bluecolored.bluemap.common.rendermanager.StorageDeleteTask;
3838
import de.bluecolored.bluemap.common.serverinterface.CommandSource;
3939
import de.bluecolored.bluemap.core.map.BmMap;
40-
import de.bluecolored.bluemap.core.storage.MapStorage;
4140
import de.bluecolored.bluemap.core.storage.Storage;
4241
import de.bluecolored.bluemap.core.storage.file.FileStorage;
4342
import lombok.RequiredArgsConstructor;
4443
import net.kyori.adventure.text.Component;
45-
import net.kyori.adventure.text.event.ClickEvent;
46-
import net.kyori.adventure.text.format.TextDecoration;
4744

4845
import java.io.IOException;
4946
import java.util.LinkedList;
@@ -84,14 +81,8 @@ private Component formatStorageEntry(String id, StorageConfig storage, boolean l
8481
public Component storage(CommandSource source, @Argument("storage") @Parser("storage-id") String storageId)
8582
throws ConfigurationException, InterruptedException, IOException {
8683

87-
Map<String, Storage> loadedStorages = plugin.getBlueMap().getLoadedStorages();
88-
Storage storage = loadedStorages.get(storageId);
8984
StorageConfig storageConfig = plugin.getBlueMap().getConfig().getStorageConfigs().get(storageId);
90-
91-
if (storage == null) {
92-
source.sendMessage(format("Initializing storage '%'...", storageId).color(BASE_COLOR));
93-
storage = plugin.getBlueMap().getOrLoadStorage(storageId);
94-
}
85+
Storage storage = getOrLoadStorage(storageId, source);
9586

9687
List<Component> lines = new LinkedList<>();
9788

@@ -124,41 +115,22 @@ public Component storage(CommandSource source, @Argument("storage") @Parser("sto
124115
lines.add(text("Maps:").color(BASE_COLOR));
125116
storage.mapIds()
126117
.limit(20)
127-
.map(this::formatMapEntry)
118+
.map(mapId -> formatMapEntry(mapId, storage))
128119
.forEach(lines::add);
129120

130121
return paragraph("Storage '%s'".formatted(storageId), lines(lines));
131122
}
132123

133-
private Component formatMapEntry(String id) {
134-
BmMap map = plugin.getBlueMap().getMaps().get(id);
135-
136-
Component loadedIcon = map != null ?
137-
text("✔").color(POSITIVE_COLOR):
138-
text("❌").color(BASE_COLOR);
139-
140-
return format("% %",
141-
loadedIcon,
142-
text(id).color(HIGHLIGHT_COLOR)
143-
).color(BASE_COLOR);
144-
}
145-
146124
@Command("storages <storage> delete <map>")
147125
@Permission("bluemap.storages.delete")
148126
public void deleteMap(
149127
CommandSource source,
150128
@Argument("storage") @Parser("storage-id") String storageId,
151129
@Argument("map") String mapId
152130
) throws ConfigurationException, InterruptedException {
153-
Map<String, Storage> loadedStorages = plugin.getBlueMap().getLoadedStorages();
154-
Storage storage = loadedStorages.get(storageId);
155-
156-
if (storage == null) {
157-
source.sendMessage(format("Initializing storage %...", storageId).color(BASE_COLOR));
158-
storage = plugin.getBlueMap().getOrLoadStorage(storageId);
159-
}
131+
Storage storage = getOrLoadStorage(storageId, source);
160132

161-
if (plugin.getBlueMap().getMaps().containsKey(mapId)) {
133+
if (isMapLoaded(mapId, storage)) {
162134
source.sendMessage(text("Can't delete a loaded map!").color(NEGATIVE_COLOR)
163135
.append(format("""
164136
Unload the map by removing it's config-file first,
@@ -183,4 +155,29 @@ public void deleteMap(
183155
));
184156
}
185157

158+
private Storage getOrLoadStorage(String storageId, CommandSource source) throws ConfigurationException, InterruptedException {
159+
Map<String, Storage> loadedStorages = plugin.getBlueMap().getLoadedStorages();
160+
Storage storage = loadedStorages.get(storageId);
161+
if (storage != null) return storage;
162+
163+
source.sendMessage(format("Initializing storage '%'...", storageId).color(BASE_COLOR));
164+
return plugin.getBlueMap().getOrLoadStorage(storageId);
165+
}
166+
167+
private boolean isMapLoaded(String mapId, Storage storage) {
168+
BmMap map = plugin.getBlueMap().getMaps().get(mapId);
169+
return map != null && map.getStorage().equals(storage.map(mapId));
170+
}
171+
172+
private Component formatMapEntry(String id, Storage storage) {
173+
Component loadedIcon = isMapLoaded(id, storage) ?
174+
text("✔").color(POSITIVE_COLOR):
175+
text("❌").color(BASE_COLOR);
176+
177+
return format("% %",
178+
loadedIcon,
179+
text(id).color(HIGHLIGHT_COLOR)
180+
).color(BASE_COLOR);
181+
}
182+
186183
}

core/src/main/java/de/bluecolored/bluemap/core/storage/file/FileStorage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public FileMapStorage map(String mapId) {
5858
@SuppressWarnings("resource")
5959
@Override
6060
public Stream<String> mapIds() throws IOException {
61+
if (!Files.exists(root)) return Stream.empty();
6162
return Files.list(root)
6263
.filter(Files::isDirectory)
6364
.map(Path::getFileName)

0 commit comments

Comments
 (0)