Merge pull request #2590 from Multiverse/small-wm-updates

Small WorldManager updates
This commit is contained in:
Ben Woo 2021-03-10 06:41:18 +08:00 committed by GitHub
commit e843b0711d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 37 deletions

View File

@ -174,6 +174,16 @@ public interface MVWorldManager {
*/ */
MultiverseWorld getMVWorld(String name); MultiverseWorld getMVWorld(String name);
/**
* Returns a {@link MultiverseWorld} if the world with name given exists, and null if it does not.
* This will search optionally for alias names.
*
* @param name The name or optionally the alias of the world to get.
* @param checkAliases Indicates whether to check for world alias name.
* @return A {@link MultiverseWorld} or null.
*/
MultiverseWorld getMVWorld(String name, boolean checkAliases);
/** /**
* Returns a {@link MultiverseWorld} if it exists, and null if it does not. * Returns a {@link MultiverseWorld} if it exists, and null if it does not.
* *
@ -183,13 +193,24 @@ public interface MVWorldManager {
MultiverseWorld getMVWorld(World world); MultiverseWorld getMVWorld(World world);
/** /**
* Checks to see if the given name is a valid {@link MultiverseWorld}. * Checks to see if the given name is a valid {@link MultiverseWorld}
* Searches based on world name AND alias.
* *
* @param name The name or alias of the world to check. * @param name The name or alias of the world to check.
* @return True if the world exists, false if not. * @return True if the world exists, false if not.
*/ */
boolean isMVWorld(String name); boolean isMVWorld(String name);
/**
* Checks to see if the given name is a valid {@link MultiverseWorld}.
* Optionally searches by alias is specified.
*
* @param name The name or alias of the world to check.
* @param checkAliases Indicates whether to check for world alias name.
* @return True if the world exists, false if not.
*/
boolean isMVWorld(String name, boolean checkAliases);
/** /**
* Checks to see if the given world is a valid {@link MultiverseWorld}. * Checks to see if the given world is a valid {@link MultiverseWorld}.
* *
@ -314,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;
return worldList.toString();
} }
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.
@ -617,6 +617,14 @@ public class WorldManager implements MVWorldManager {
*/ */
@Override @Override
public MultiverseWorld getMVWorld(String name) { public MultiverseWorld getMVWorld(String name) {
return this.getMVWorld(name, true);
}
/**
* {@inheritDoc}
*/
@Override
public MultiverseWorld getMVWorld(String name, boolean checkAliases) {
if (name == null) { if (name == null) {
return null; return null;
} }
@ -624,7 +632,7 @@ public class WorldManager implements MVWorldManager {
if (world != null) { if (world != null) {
return world; return world;
} }
return this.getMVWorldByAlias(name); return (checkAliases) ? this.getMVWorldByAlias(name) : null;
} }
/** /**
@ -633,7 +641,7 @@ public class WorldManager implements MVWorldManager {
@Override @Override
public MultiverseWorld getMVWorld(World world) { public MultiverseWorld getMVWorld(World world) {
if (world != null) { if (world != null) {
return this.getMVWorld(world.getName()); return this.getMVWorld(world.getName(), false);
} }
return null; return null;
} }
@ -658,7 +666,15 @@ public class WorldManager implements MVWorldManager {
*/ */
@Override @Override
public boolean isMVWorld(final String name) { public boolean isMVWorld(final String name) {
return (this.worlds.containsKey(name) || isMVWorldAlias(name)); return this.isMVWorld(name, true);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isMVWorld(final String name, boolean checkAliases) {
return this.worlds.containsKey(name) || (checkAliases && this.isMVWorldAlias(name));
} }
/** /**
@ -940,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());
}
} }