mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-28 13:36:21 +01:00
Improve world name/folder checking for create and import command.
Make use of the new WorldNameChecker class.
This commit is contained in:
parent
a14b822de8
commit
c132d18ae3
@ -9,6 +9,7 @@ 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.utils.WorldNameChecker;
|
||||||
import com.pneumaticraft.commandhandler.CommandHandler;
|
import com.pneumaticraft.commandhandler.CommandHandler;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.World.Environment;
|
import org.bukkit.World.Environment;
|
||||||
@ -17,7 +18,6 @@ import org.bukkit.command.Command;
|
|||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.permissions.PermissionDefault;
|
import org.bukkit.permissions.PermissionDefault;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -47,15 +47,9 @@ public class CreateCommand extends MultiverseCommand {
|
|||||||
this.worldManager = this.plugin.getMVWorldManager();
|
this.worldManager = this.plugin.getMVWorldManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String trimWorldName(String userInput) {
|
|
||||||
// Removes relative paths.
|
|
||||||
return userInput.replaceAll("^[./\\\\]+", "");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void runCommand(CommandSender sender, List<String> args) {
|
public void runCommand(CommandSender sender, List<String> args) {
|
||||||
String worldName = trimWorldName(args.get(0));
|
String worldName = args.get(0);
|
||||||
File worldFile = new File(this.plugin.getServer().getWorldContainer(), worldName);
|
|
||||||
String env = args.get(1);
|
String env = args.get(1);
|
||||||
String seed = CommandHandler.getFlag("-s", args);
|
String seed = CommandHandler.getFlag("-s", args);
|
||||||
String generator = CommandHandler.getFlag("-g", args);
|
String generator = CommandHandler.getFlag("-g", args);
|
||||||
@ -72,9 +66,9 @@ public class CreateCommand extends MultiverseCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure the world name doesn't contain the words 'plugins' and '.dat'
|
if(!this.plugin.getMVConfig().isAllowUnsafeWorldName() && !WorldNameChecker.isValidWorldName(worldName)) {
|
||||||
if(worldName.contains("plugins")||worldName.contains(".dat")){
|
sender.sendMessage(ChatColor.RED + "Multiverse cannot create the world as the world name '"
|
||||||
sender.sendMessage(ChatColor.RED + "Multiverse cannot create a world that contains 'plugins' or '.dat'");
|
+ worldName + "' contains spaces or invalid characters!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +78,7 @@ public class CreateCommand extends MultiverseCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (worldFile.exists()) {
|
if (WorldNameChecker.checkFolder(worldName) != WorldNameChecker.FolderStatus.DOES_NOT_EXIST) {
|
||||||
sender.sendMessage(ChatColor.RED + "A Folder/World already exists with this name!");
|
sender.sendMessage(ChatColor.RED + "A Folder/World already exists with this name!");
|
||||||
sender.sendMessage(ChatColor.RED + "If you are confident it is a world you can import with /mvimport");
|
sender.sendMessage(ChatColor.RED + "If you are confident it is a world you can import with /mvimport");
|
||||||
return;
|
return;
|
||||||
|
@ -17,7 +17,6 @@ import org.bukkit.command.Command;
|
|||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.permissions.PermissionDefault;
|
import org.bukkit.permissions.PermissionDefault;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -56,14 +55,9 @@ public class ImportCommand extends MultiverseCommand {
|
|||||||
return worldList.toString();
|
return worldList.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String trimWorldName(String userInput) {
|
|
||||||
// Removes relative paths.
|
|
||||||
return userInput.replaceAll("^[./\\\\]+", "");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void runCommand(CommandSender sender, List<String> args) {
|
public void runCommand(CommandSender sender, List<String> args) {
|
||||||
String worldName = trimWorldName(args.get(0));
|
String worldName = args.get(0);
|
||||||
|
|
||||||
if (worldName.toLowerCase().equals("--list") || worldName.toLowerCase().equals("-l")) {
|
if (worldName.toLowerCase().equals("--list") || worldName.toLowerCase().equals("-l")) {
|
||||||
String worldList = this.getPotentialWorldStrings();
|
String worldList = this.getPotentialWorldStrings();
|
||||||
@ -82,9 +76,9 @@ public class ImportCommand extends MultiverseCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure the world name doesn't contain the words 'plugins' and '.dat'
|
if(!this.plugin.getMVConfig().isAllowUnsafeWorldName() && !WorldNameChecker.isValidWorldName(worldName)) {
|
||||||
if(worldName.contains("plugins")||worldName.contains(".dat")){
|
sender.sendMessage(ChatColor.RED + "Multiverse cannot import the world as the world name '"
|
||||||
sender.sendMessage(ChatColor.RED + "Multiverse cannot create a world that contains 'plugins' or '.dat'");
|
+ worldName + "' contains spaces or invalid characters!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +89,18 @@ public class ImportCommand extends MultiverseCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
File worldFile = new File(this.plugin.getServer().getWorldContainer(), worldName);
|
switch (WorldNameChecker.checkFolder(worldName)) {
|
||||||
|
case DOES_NOT_EXIST:
|
||||||
|
sender.sendMessage(ChatColor.RED + "FAILED.");
|
||||||
|
sender.sendMessage(ChatColor.RED + "That world folder does not exist. These look like worlds to me:");
|
||||||
|
sender.sendMessage(this.getPotentialWorldStrings());
|
||||||
|
return;
|
||||||
|
case NOT_A_WORLD:
|
||||||
|
sender.sendMessage(ChatColor.RED + "FAILED.");
|
||||||
|
sender.sendMessage(String.format("%s'%s' does not appear to be a world. It is lacking a .dat file.",
|
||||||
|
ChatColor.RED, worldName));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
String generator = CommandHandler.getFlag("-g", args);
|
String generator = CommandHandler.getFlag("-g", args);
|
||||||
boolean useSpawnAdjust = true;
|
boolean useSpawnAdjust = true;
|
||||||
@ -113,24 +118,17 @@ public class ImportCommand extends MultiverseCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!worldFile.exists()) {
|
if (env == null) {
|
||||||
sender.sendMessage(ChatColor.RED + "FAILED.");
|
|
||||||
String worldList = this.getPotentialWorldStrings();
|
|
||||||
sender.sendMessage("That world folder does not exist. These look like worlds to me:");
|
|
||||||
sender.sendMessage(worldList);
|
|
||||||
} else if (!WorldNameChecker.isValidWorldFolder(worldFile)) {
|
|
||||||
sender.sendMessage(ChatColor.RED + "FAILED.");
|
|
||||||
sender.sendMessage(String.format("'%s' does not appear to be a world. It is lacking a .dat file.",
|
|
||||||
worldName));
|
|
||||||
} else if (env == null) {
|
|
||||||
sender.sendMessage(ChatColor.RED + "FAILED.");
|
sender.sendMessage(ChatColor.RED + "FAILED.");
|
||||||
sender.sendMessage("That world environment did not exist.");
|
sender.sendMessage("That world environment did not exist.");
|
||||||
sender.sendMessage("For a list of available world types, type: " + ChatColor.AQUA + "/mvenv");
|
sender.sendMessage("For a list of available world types, type: " + ChatColor.AQUA + "/mvenv");
|
||||||
} else {
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Command.broadcastCommandMessage(sender, String.format("Starting import of world '%s'...", worldName));
|
Command.broadcastCommandMessage(sender, String.format("Starting import of world '%s'...", worldName));
|
||||||
if (this.worldManager.addWorld(worldName, environment, null, null, null, generator, useSpawnAdjust))
|
if (this.worldManager.addWorld(worldName, environment, null, null, null, generator, useSpawnAdjust)) {
|
||||||
Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Complete!");
|
Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Complete!");
|
||||||
else
|
} else {
|
||||||
Command.broadcastCommandMessage(sender, ChatColor.RED + "Failed!");
|
Command.broadcastCommandMessage(sender, ChatColor.RED + "Failed!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,6 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.ArgumentMatchers;
|
import org.mockito.ArgumentMatchers;
|
||||||
import org.mockito.Matchers;
|
|
||||||
import org.mockito.internal.verification.VerificationModeFactory;
|
import org.mockito.internal.verification.VerificationModeFactory;
|
||||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||||
@ -38,8 +37,14 @@ import org.powermock.modules.junit4.PowerMockRunner;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import static junit.framework.Assert.*;
|
import static junit.framework.Assert.assertEquals;
|
||||||
import static org.mockito.Mockito.*;
|
import static junit.framework.Assert.assertNotNull;
|
||||||
|
import static junit.framework.Assert.assertTrue;
|
||||||
|
import static junit.framework.Assert.fail;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@RunWith(PowerMockRunner.class)
|
@RunWith(PowerMockRunner.class)
|
||||||
@PrepareForTest({ PluginManager.class, MultiverseCore.class, Permission.class, Bukkit.class, WorldManager.class,
|
@PrepareForTest({ PluginManager.class, MultiverseCore.class, Permission.class, Bukkit.class, WorldManager.class,
|
||||||
@ -92,7 +97,7 @@ public class TestWorldStuff {
|
|||||||
// Import the first world. The world folder does not exist.
|
// Import the first world. The world folder does not exist.
|
||||||
plugin.onCommand(mockCommandSender, mockCommand, "", normalArgs);
|
plugin.onCommand(mockCommandSender, mockCommand, "", normalArgs);
|
||||||
verify(mockCommandSender).sendMessage(ChatColor.RED + "FAILED.");
|
verify(mockCommandSender).sendMessage(ChatColor.RED + "FAILED.");
|
||||||
verify(mockCommandSender).sendMessage("That world folder does not exist. These look like worlds to me:");
|
verify(mockCommandSender).sendMessage(ChatColor.RED + "That world folder does not exist. These look like worlds to me:");
|
||||||
|
|
||||||
// We should still have no worlds.
|
// We should still have no worlds.
|
||||||
assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size());
|
assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size());
|
||||||
|
Loading…
Reference in New Issue
Block a user