Add getPotentialWorlds method to WorldManager.

This commit is contained in:
benwoo1110 2021-03-09 10:40:46 +08:00
parent 24295025ef
commit f5a2d7bc47
3 changed files with 38 additions and 33 deletions

View File

@ -335,4 +335,11 @@ public interface MVWorldManager {
* does not exist. {@code includeLoaded} if the world exists and is loaded. * does not exist. {@code includeLoaded} if the world exists and is loaded.
*/ */
boolean hasUnloadedWorld(String name, boolean includeLoaded); boolean hasUnloadedWorld(String name, boolean includeLoaded);
/**
* Get all the possible worlds that Multiverse has detected to be importable.
*
* @return A collection of world names that are deemed importable.
*/
Collection<String> getPotentialWorlds();
} }

View File

@ -9,7 +9,6 @@ package com.onarandombox.MultiverseCore.commands;
import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseCore.utils.WorldNameChecker; import com.onarandombox.MultiverseCore.utils.WorldNameChecker;
import com.pneumaticraft.commandhandler.CommandHandler; import com.pneumaticraft.commandhandler.CommandHandler;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -19,8 +18,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
import java.io.File; import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -46,33 +43,17 @@ public class ImportCommand extends MultiverseCommand {
this.worldManager = this.plugin.getMVWorldManager(); this.worldManager = this.plugin.getMVWorldManager();
} }
private String getPotentialWorlds() { private String getPotentialWorldStrings() {
File worldFolder = this.plugin.getServer().getWorldContainer(); final Collection<String> potentialWorlds = this.worldManager.getPotentialWorlds();
if (worldFolder == null) { StringBuilder worldList = new StringBuilder();
return "";
}
File[] files = worldFolder.listFiles();
String worldList = "";
Collection<MultiverseWorld> worlds = this.worldManager.getMVWorlds();
List<String> worldStrings = new ArrayList<String>();
for (MultiverseWorld world : worlds) {
worldStrings.add(world.getName());
}
for (String world : this.worldManager.getUnloadedWorlds()) {
worldStrings.add(world);
}
ChatColor currColor = ChatColor.WHITE; ChatColor currColor = ChatColor.WHITE;
for (File file : files) {
if (file.isDirectory() && WorldNameChecker.isValidWorldFolder(file) && !worldStrings.contains(file.getName())) { for (String world : potentialWorlds) {
worldList += currColor + file.getName() + " "; worldList.append(currColor).append(world).append(' ');
if (currColor == ChatColor.WHITE) { currColor = currColor == ChatColor.WHITE ? ChatColor.YELLOW : ChatColor.WHITE;
currColor = ChatColor.YELLOW;
} else {
currColor = ChatColor.WHITE;
} }
}
} return worldList.toString();
return worldList;
} }
private String trimWorldName(String userInput) { private String trimWorldName(String userInput) {
@ -85,7 +66,7 @@ public class ImportCommand extends MultiverseCommand {
String worldName = trimWorldName(args.get(0)); String worldName = trimWorldName(args.get(0));
if (worldName.toLowerCase().equals("--list") || worldName.toLowerCase().equals("-l")) { if (worldName.toLowerCase().equals("--list") || worldName.toLowerCase().equals("-l")) {
String worldList = this.getPotentialWorlds(); String worldList = this.getPotentialWorldStrings();
if (worldList.length() > 2) { if (worldList.length() > 2) {
sender.sendMessage(ChatColor.AQUA + "====[ These look like worlds ]===="); sender.sendMessage(ChatColor.AQUA + "====[ These look like worlds ]====");
sender.sendMessage(worldList); sender.sendMessage(worldList);
@ -128,7 +109,7 @@ public class ImportCommand extends MultiverseCommand {
if (!worldFile.exists()) { if (!worldFile.exists()) {
sender.sendMessage(ChatColor.RED + "FAILED."); sender.sendMessage(ChatColor.RED + "FAILED.");
String worldList = this.getPotentialWorlds(); String worldList = this.getPotentialWorldStrings();
sender.sendMessage("That world folder does not exist. These look like worlds to me:"); sender.sendMessage("That world folder does not exist. These look like worlds to me:");
sender.sendMessage(worldList); sender.sendMessage(worldList);
} else if (!WorldNameChecker.isValidWorldFolder(worldFile)) { } else if (!WorldNameChecker.isValidWorldFolder(worldFile)) {

View File

@ -36,19 +36,19 @@ import java.io.File;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.Stack; import java.util.Stack;
import java.util.Arrays;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
/** /**
* Public facing API to add/remove Multiverse worlds. * Public facing API to add/remove Multiverse worlds.
@ -956,4 +956,21 @@ public class WorldManager implements MVWorldManager {
} }
return false; return false;
} }
/**
* {@inheritDoc}
*/
@Override
public Collection<String> getPotentialWorlds() {
File worldContainer = this.plugin.getServer().getWorldContainer();
if (worldContainer == null) {
return Collections.emptyList();
}
return Arrays.stream(worldContainer.listFiles())
.filter(File::isDirectory)
.filter(folder -> !this.isMVWorld(folder.getName(), false))
.filter(WorldNameChecker::isValidWorldFolder)
.map(File::getName)
.collect(Collectors.toList());
}
} }