Document WorldManager.java

This commit is contained in:
Eric Stokes 2011-09-17 12:55:28 -06:00
parent 1b42a25ccb
commit f9481db5a6
2 changed files with 93 additions and 22 deletions

View File

@ -22,7 +22,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority; import org.bukkit.event.Event.Priority;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.config.Configuration; import org.bukkit.util.config.Configuration;
@ -265,7 +264,7 @@ public class MultiverseCore extends JavaPlugin implements LoggablePlugin {
} }
try { try {
this.worldManager.propigateConfigFile(); this.worldManager.propagateConfigFile();
log(Level.INFO, "- World Config -- Loaded"); log(Level.INFO, "- World Config -- Loaded");
} catch (Exception e) { } catch (Exception e) {
log(Level.INFO, "- Failed to load worlds.yml"); log(Level.INFO, "- Failed to load worlds.yml");

View File

@ -17,6 +17,11 @@ import org.bukkit.util.config.Configuration;
import com.onarandombox.MultiverseCore.MVWorld; import com.onarandombox.MultiverseCore.MVWorld;
import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.MultiverseCore;
/**
* Public facing API to add/remove Multiverse worlds.
*
* @author fernferret, Rigby90
*/
public class WorldManager { public class WorldManager {
private MultiverseCore plugin; private MultiverseCore plugin;
private PurgeWorlds worldPurger; private PurgeWorlds worldPurger;
@ -24,7 +29,7 @@ public class WorldManager {
private Configuration configWorlds = null; private Configuration configWorlds = null;
public WorldManager(MultiverseCore core) { public WorldManager(MultiverseCore core) {
this.plugin = core; this.plugin = core;
this.worlds = new HashMap<String, MVWorld>(); this.worlds = new HashMap<String, MVWorld>();
this.worldPurger = new PurgeWorlds(this.plugin); this.worldPurger = new PurgeWorlds(this.plugin);
@ -34,9 +39,13 @@ public class WorldManager {
* Add a new World to the Multiverse Setup. * Add a new World to the Multiverse Setup.
* <p/> * <p/>
* Isn't there a prettier way to do this??!!?!?! * Isn't there a prettier way to do this??!!?!?!
* *
* @param name World Name * @param name World Name
* @param env Environment Type * @param env Environment Type
* @param seedString The seed in the form of a string. If the seed is a Long, it will be interpreted as such.
* @param generator The Custom generator plugin to use.
*
* @return True if the world is added, false if not.
*/ */
public boolean addWorld(String name, Environment env, String seedString, String generator) { public boolean addWorld(String name, Environment env, String seedString, String generator) {
plugin.log(Level.FINE, "Adding world with: " + name + ", " + env.toString() + ", " + seedString + ", " + generator); plugin.log(Level.FINE, "Adding world with: " + name + ", " + env.toString() + ", " + seedString + ", " + generator);
@ -65,9 +74,7 @@ public class WorldManager {
this.plugin.log(Level.WARNING, "Could not find plugin: " + generatorName); this.plugin.log(Level.WARNING, "Could not find plugin: " + generatorName);
} else { } else {
this.plugin.log(Level.WARNING, "Found plugin: " + generatorName + ", but did not find generatorID: " + generatorID); this.plugin.log(Level.WARNING, "Found plugin: " + generatorName + ", but did not find generatorID: " + generatorID);
} }
return false; return false;
} }
@ -101,11 +108,25 @@ public class WorldManager {
return true; return true;
} }
/**
* Verifies that a given Plugin generator string exists.
*
* @param generator The name of the generator plugin. This should be something like CleanRoomGenerator.
* @return True if the plugin exists and is enabled, false if not.
*/
private boolean pluginExists(String generator) { private boolean pluginExists(String generator) {
Plugin plugin = this.plugin.getServer().getPluginManager().getPlugin(generator); Plugin plugin = this.plugin.getServer().getPluginManager().getPlugin(generator);
return plugin != null; return plugin != null && plugin.isEnabled();
} }
/**
* Test if a given chunk generator is valid.
*
* @param generator The generator name.
* @param generatorID The generator id.
* @param worldName The worldName to use as the default.
* @return A {@link ChunkGenerator} or null
*/
private ChunkGenerator getChunkGenerator(String generator, String generatorID, String worldName) { private ChunkGenerator getChunkGenerator(String generator, String generatorID, String worldName) {
if (generator == null) { if (generator == null) {
return null; return null;
@ -122,7 +143,7 @@ public class WorldManager {
/** /**
* Remove the world from the Multiverse list and from the config * Remove the world from the Multiverse list and from the config
* *
* @param name The name of the world to remove * @param name The name of the world to remove
* @return True if success, false if failure. * @return True if success, false if failure.
*/ */
@ -138,10 +159,10 @@ public class WorldManager {
} }
return false; return false;
} }
/** /**
* Remove the world from the Multiverse list * Remove the world from the Multiverse list
* *
* @param name The name of the world to remove * @param name The name of the world to remove
* @return True if success, false if failure. * @return True if success, false if failure.
*/ */
@ -160,10 +181,10 @@ public class WorldManager {
} }
return false; return false;
} }
/** /**
* Remove the world from the Multiverse list, from the config and deletes the folder * Remove the world from the Multiverse list, from the config and deletes the folder
* *
* @param name The name of the world to remove * @param name The name of the world to remove
* @return True if success, false if failure. * @return True if success, false if failure.
*/ */
@ -217,11 +238,23 @@ public class WorldManager {
} }
} }
/**
* Unload a world from Multiverse
*
* @param name Name of the world to unload
* @param safely Perform this safely. Set to True to save world files before unloading.
* @return True if the world was unloaded, false if not.
*/
private boolean unloadWorld(String name, boolean safely) { private boolean unloadWorld(String name, boolean safely) {
this.removePlayersFromWorld(name); this.removePlayersFromWorld(name);
return this.plugin.getServer().unloadWorld(name, safely); return this.plugin.getServer().unloadWorld(name, safely);
} }
/**
* Removes all players from the specified world.
*
* @param name World to remove players from.
*/
private void removePlayersFromWorld(String name) { private void removePlayersFromWorld(String name) {
World w = this.plugin.getServer().getWorld(name); World w = this.plugin.getServer().getWorld(name);
if (w != null) { if (w != null) {
@ -234,10 +267,21 @@ public class WorldManager {
} }
} }
/**
* Returns a list of all the worlds Multiverse knows about.
*
* @return A list of {@link MVWorld}.
*/
public Collection<MVWorld> getMVWorlds() { public Collection<MVWorld> getMVWorlds() {
return this.worlds.values(); return this.worlds.values();
} }
/**
* Returns a {@link MVWorld} if it exists, and null if it does not. This will search name AND alias.
*
* @param name The name or alias of the world to get.
* @return A {@link MVWorld} or null.
*/
public MVWorld getMVWorld(String name) { public MVWorld getMVWorld(String name) {
if (this.worlds.containsKey(name)) { if (this.worlds.containsKey(name)) {
return this.worlds.get(name); return this.worlds.get(name);
@ -245,6 +289,12 @@ public class WorldManager {
return this.getMVWorldByAlias(name); return this.getMVWorldByAlias(name);
} }
/**
* Returns a {@link MVWorld} if it exists, and null if it does not. This will search ONLY alias.
*
* @param alias The alias of the world to get.
* @return A {@link MVWorld} or null.
*/
private MVWorld getMVWorldByAlias(String alias) { private MVWorld getMVWorldByAlias(String alias) {
for (MVWorld w : this.worlds.values()) { for (MVWorld w : this.worlds.values()) {
if (w.getAlias().equalsIgnoreCase(alias)) { if (w.getAlias().equalsIgnoreCase(alias)) {
@ -254,19 +304,25 @@ public class WorldManager {
return null; return null;
} }
/**
* Checks to see if the given name is a valid {@link MVWorld}
*
* @param name The name or alias of the world to check.
* @return True if the world exists, false if not.
*/
public boolean isMVWorld(String name) { public boolean isMVWorld(String name) {
return (this.worlds.containsKey(name) || isMVWorldAlias(name)); return (this.worlds.containsKey(name) || isMVWorldAlias(name));
} }
/** /**
* This method ONLY checks the alias of each world. * This method ONLY checks the alias of each world.
* *
* @param name * @param alias The alias of the world to check.
* @return * @return True if the world exists, false if not.
*/ */
private boolean isMVWorldAlias(String name) { private boolean isMVWorldAlias(String alias) {
for (MVWorld w : this.worlds.values()) { for (MVWorld w : this.worlds.values()) {
if (w.getAlias().equalsIgnoreCase(name)) { if (w.getAlias().equalsIgnoreCase(alias)) {
return true; return true;
} }
} }
@ -274,6 +330,8 @@ public class WorldManager {
} }
/** /**
* Load the Worlds & Settings from the configuration file. * Load the Worlds & Settings from the configuration file.
*
* @param forceLoad If set to true, this will perform a total reset and not just load new worlds.
*/ */
public void loadWorlds(boolean forceLoad) { public void loadWorlds(boolean forceLoad) {
// Basic Counter to count how many Worlds we are loading. // Basic Counter to count how many Worlds we are loading.
@ -314,9 +372,9 @@ public class WorldManager {
String environment = this.configWorlds.getString("worlds." + worldKey + ".environment", "NORMAL"); // Grab the Environment as a String. String environment = this.configWorlds.getString("worlds." + worldKey + ".environment", "NORMAL"); // Grab the Environment as a String.
String seedString = this.configWorlds.getString("worlds." + worldKey + ".seed", ""); String seedString = this.configWorlds.getString("worlds." + worldKey + ".seed", "");
String generatorstring = this.configWorlds.getString("worlds." + worldKey + ".generator"); String generatorString = this.configWorlds.getString("worlds." + worldKey + ".generator");
addWorld(worldKey, this.plugin.getEnvFromString(environment), seedString, generatorstring); addWorld(worldKey, this.plugin.getEnvFromString(environment), seedString, generatorString);
// Increment the world count // Increment the world count
count++; count++;
@ -327,16 +385,30 @@ public class WorldManager {
this.plugin.log(Level.INFO, count + " - World(s) loaded."); this.plugin.log(Level.INFO, count + " - World(s) loaded.");
} }
/**
* Return the World Purger.
*
* @return A valid {@link PurgeWorlds}.
*/
public PurgeWorlds getWorldPurger() { public PurgeWorlds getWorldPurger() {
return this.worldPurger; return this.worldPurger;
} }
/**
* Load the config from a file.
*
* @param file The file to load.
* @return A loaded configuration.
*/
public Configuration loadWorldConfig(File file) { public Configuration loadWorldConfig(File file) {
this.configWorlds = new Configuration(file); this.configWorlds = new Configuration(file);
return this.configWorlds; return this.configWorlds;
} }
public void propigateConfigFile() { /**
* Reload the Config File
*/
public void propagateConfigFile() {
this.configWorlds.load(); this.configWorlds.load();
} }