Move to ActiveConfig Properties.

This still needs some cleanup, but works well.

I'd like some users to test (and comment here) on the following
commands:
mvm set pvp true/false (can't test this by myself)

Everything else looks good. All tests pass. Think this one (due to the
active properties) fixes #322
This commit is contained in:
Eric Stokes 2012-01-04 21:46:19 -07:00
parent bb0b26e03c
commit 53c801bb81
9 changed files with 343 additions and 64 deletions

View File

@ -9,8 +9,8 @@ package com.onarandombox.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseCore.configuration.ConfigPropertyFactory;
import com.onarandombox.MultiverseCore.configuration.MVActiveConfigProperty;
import com.onarandombox.MultiverseCore.configuration.MVConfigProperty;
import com.onarandombox.MultiverseCore.configuration.TempStringConfigProperty;
import com.onarandombox.MultiverseCore.enums.EnglishChatColor;
import com.onarandombox.MultiverseCore.event.MVWorldPropertyChangeEvent;
import com.onarandombox.MultiverseCore.exceptions.PropertyDoesNotExistException;
@ -123,20 +123,20 @@ public class MVWorld implements MultiverseWorld {
this.propertyList.put("color", fac.getNewProperty("color", EnglishChatColor.WHITE, "alias.color",
"Sorry, 'color' must either one of: " + EnglishChatColor.getAllColors()));
this.propertyList.put("pvp", fac.getNewProperty("pvp", true,
"Sorry, 'pvp' must either be:" + ChatColor.GREEN + " true " + ChatColor.WHITE + "or" + ChatColor.RED + " false" + ChatColor.WHITE + "."));
this.propertyList.put("scale", fac.getNewProperty("scale", this.getDefaultScale(this.environment),
"Scale must be a positive double value. ex: " + ChatColor.GOLD + "2.3"));
"Sorry, 'pvp' must either be:" + ChatColor.GREEN + " true " + ChatColor.WHITE + "or" + ChatColor.RED + " false" + ChatColor.WHITE + ".", "setActualPVP"));
this.propertyList.put("scale", fac.getNewProperty("scale", this.getDefaultScale(this.environment), "scale",
"Scale must be a positive double value. ex: " + ChatColor.GOLD + "2.3", "verifyScaleSetProperly"));
this.propertyList.put("respawn", fac.getNewProperty("respawn", "", "respawnworld",
"You must set this to the " + ChatColor.GOLD + " NAME" + ChatColor.RED + " not alias of a world."));
this.propertyList.put("weather", fac.getNewProperty("weather", true, "allowweather",
"Sorry, 'weather' must either be:" + ChatColor.GREEN + " true " + ChatColor.WHITE + "or" + ChatColor.RED + " false" + ChatColor.WHITE + "."));
"Sorry, 'weather' must either be:" + ChatColor.GREEN + " true " + ChatColor.WHITE + "or" + ChatColor.RED + " false" + ChatColor.WHITE + ".", "setActualWeather"));
this.propertyList.put("difficulty", fac.getNewProperty("difficulty", Difficulty.EASY,
"Difficulty must be set as one of the following: " + ChatColor.GREEN + "peaceful "
+ ChatColor.AQUA + "easy " + ChatColor.GOLD + "normal " + ChatColor.RED + "hard"));
this.propertyList.put("animals", fac.getNewProperty("animals", true, "animals.spawn",
"Sorry, 'animals' must either be:" + ChatColor.GREEN + " true " + ChatColor.WHITE + "or" + ChatColor.RED + " false" + ChatColor.WHITE + "."));
"Sorry, 'animals' must either be:" + ChatColor.GREEN + " true " + ChatColor.WHITE + "or" + ChatColor.RED + " false" + ChatColor.WHITE + ".", "syncMobs"));
this.propertyList.put("monsters", fac.getNewProperty("monsters", true, "monsters.spawn",
"Sorry, 'monsters' must either be:" + ChatColor.GREEN + " true " + ChatColor.WHITE + "or" + ChatColor.RED + " false" + ChatColor.WHITE + "."));
"Sorry, 'monsters' must either be:" + ChatColor.GREEN + " true " + ChatColor.WHITE + "or" + ChatColor.RED + " false" + ChatColor.WHITE + ".", "syncMobs"));
this.propertyList.put("currency", fac.getNewProperty("currency", -1, "entryfee.currency",
"Currency must be an integer between -1 and the highest Minecraft item ID."));
this.propertyList.put("price", fac.getNewProperty("price", 0.0, "entryfee.price",
@ -155,13 +155,13 @@ public class MVWorld implements MultiverseWorld {
this.propertyList.put("gamemode", fac.getNewProperty("gamemode", GameMode.SURVIVAL,
"GameMode must be set as one of the following: " + ChatColor.RED + "survival " + ChatColor.GREEN + "creative "));
this.propertyList.put("memory", fac.getNewProperty("keepspawninmemory", true, "keepspawninmemory",
"Sorry, 'memory' must either be:" + ChatColor.GREEN + " true " + ChatColor.WHITE + "or" + ChatColor.RED + " false" + ChatColor.WHITE + "."));
this.propertyList.put("spawn", fac.getNewProperty("spawn", this.world.getSpawnLocation(),
"There is no help available for this variable. Go bug Rigby90 about it."));
"Sorry, 'memory' must either be:" + ChatColor.GREEN + " true " + ChatColor.WHITE + "or" + ChatColor.RED + " false" + ChatColor.WHITE + ".", "setActualKeepSpawnInMemory"));
this.propertyList.put("spawn", fac.getNewProperty("spawn", this.world.getSpawnLocation(), "spawn",
"There is no help available for this variable. Go bug Rigby90 about it.", "setActualKeepSpawnInMemory"));
this.propertyList.put("autoload", fac.getNewProperty("autoload", true,
"Set this to false ONLY if you don't want this world to load itself on server restart."));
this.propertyList.put("bedrespawn", fac.getNewProperty("bedrespawn", true, "If a player dies in this world, shoudld they go to their bed?"));
this.propertyList.put("time", fac.getNewProperty("time", "", "Set the time to whatever you want! (Will NOT freeze time)", "setTime", true));
this.propertyList.put("time", fac.getNewProperty("time", "", "Set the time to whatever you want! (Will NOT freeze time)", "setActualTime", true));
this.getKnownProperty("spawn", Location.class).setValue(this.readSpawnFromConfig(this.getCBWorld()));
@ -201,45 +201,33 @@ public class MVWorld implements MultiverseWorld {
}
}
/**
* Applies all settings to the Bukkit-{@link World}.
*/
public void changeActiveEffects() {
// Disable any current weather
if (!this.getKnownProperty("weather", Boolean.class).getValue()) {
this.getCBWorld().setStorm(false);
this.getCBWorld().setThundering(false);
}
// Set the spawn location
Location spawnLocation = this.getKnownProperty("spawn", Location.class).getValue();
this.getCBWorld().setSpawnLocation(spawnLocation.getBlockX(), spawnLocation.getBlockY(), spawnLocation.getBlockZ());
// Synchronize all Mob settings
this.syncMobs();
// Ensure the memory setting is correct
this.world.setKeepSpawnInMemory(this.getKnownProperty("memory", Boolean.class).getValue());
private boolean setActualPVP() {
// Set the PVP mode
this.world.setPVP(this.getKnownProperty("pvp", Boolean.class).getValue());
return true;
}
private boolean verifyScaleSetProperly() {
// Ensure the scale is above 0
if (this.getKnownProperty("scale", Double.class).getValue() <= 0) {
// Disallow negative or 0 scalings.
this.getKnownProperty("scale", Double.class).setValue(1.0);
this.plugin.log(Level.WARNING, "Someone tried to set a scale <= 0, defaulting to 1.");
}
return true;
}
for (Player p : this.plugin.getServer().getWorld(this.getName()).getPlayers()) {
this.plugin.log(Level.FINER, "Setting " + p.getName() + "'s GameMode to "
+ this.getKnownProperty("mode", GameMode.class).getValue().toString());
this.plugin.getPlayerListener().handleGameMode(p, this);
}
private boolean setActualKeepSpawnInMemory() {
// Ensure the memory setting is correct
this.world.setKeepSpawnInMemory(this.getKnownProperty("memory", Boolean.class).getValue());
return true;
}
// Set the difficulty
this.getCBWorld().setDifficulty(this.getKnownProperty("diff", Difficulty.class).getValue());
private boolean setActualSpawn() {
// Set the spawn location
Location spawnLocation = this.getKnownProperty("spawn", Location.class).getValue();
this.getCBWorld().setSpawnLocation(spawnLocation.getBlockX(), spawnLocation.getBlockY(), spawnLocation.getBlockZ());
return true;
}
private double getDefaultScale(Environment environment) {
@ -508,8 +496,8 @@ public class MVWorld implements MultiverseWorld {
value = propertyChangeEvent.getNewValue();
}
if (property.parseValue(value)) {
if (property instanceof TempStringConfigProperty) {
return this.setActiveProperty((TempStringConfigProperty) property);
if (property instanceof MVActiveConfigProperty) {
return this.setActiveProperty((MVActiveConfigProperty) property);
}
this.saveConfig();
return true;
@ -517,10 +505,14 @@ public class MVWorld implements MultiverseWorld {
return false;
}
private boolean setActiveProperty(TempStringConfigProperty property) {
private boolean setActiveProperty(MVActiveConfigProperty property) {
try {
Method method = this.getClass().getMethod(property.getMethod(), String.class);
Object returnVal = method.invoke(this, (String) property.getValue());
if(property.getMethod() == null) {
// This property did not have a method.
return true;
}
Method method = this.getClass().getMethod(property.getMethod());
Object returnVal = method.invoke(this);
if (returnVal instanceof Boolean) {
return (Boolean) returnVal;
} else {
@ -823,7 +815,6 @@ public class MVWorld implements MultiverseWorld {
private void saveConfig() {
if (this.canSave) {
try {
this.changeActiveEffects();
this.config.save(new File(this.plugin.getDataFolder(), "worlds.yml"));
} catch (IOException e) {
this.plugin.log(Level.SEVERE, "Could not save worlds.yml. Please check your filesystem permissions.");
@ -836,7 +827,25 @@ public class MVWorld implements MultiverseWorld {
*/
@Override
public boolean setGameMode(String gameMode) {
return this.setKnownProperty("mode", gameMode + "", null);
return this.setKnownProperty("mode", gameMode, null);
}
/**
* Sets the actual gamemode by iterating through players.
*
* gameMode is not used, but it's in the reflection template.
*
* Needs a bit o' refactoring.
*
* @return True if the gamemodes of players were set successfully. (always)
*/
public boolean setActualGameMode() {
for (Player p : this.plugin.getServer().getWorld(this.getName()).getPlayers()) {
this.plugin.log(Level.FINER, String.format("Setting %s's GameMode to %s",
p.getName(), this.getKnownProperty("mode", GameMode.class).getValue().toString()));
this.plugin.getPlayerListener().handleGameMode(p, this);
}
return true;
}
/**
@ -855,6 +864,15 @@ public class MVWorld implements MultiverseWorld {
this.setKnownProperty("weather", weather + "", null);
}
public boolean setActualWeather() {
// Disable any current weather
if (!this.getKnownProperty("weather", Boolean.class).getValue()) {
this.getCBWorld().setStorm(false);
this.getCBWorld().setThundering(false);
}
return true;
}
/**
* {@inheritDoc}
*/
@ -969,7 +987,12 @@ public class MVWorld implements MultiverseWorld {
*/
@Override
public boolean setDifficulty(String difficulty) {
return this.setKnownProperty("diff", difficulty, null);
if (this.setKnownProperty("diff", difficulty, null)) {
// Set the difficulty
this.getCBWorld().setDifficulty(this.getKnownProperty("diff", Difficulty.class).getValue());
return true;
}
return false;
}
/**
@ -1067,6 +1090,10 @@ public class MVWorld implements MultiverseWorld {
return String.format("%d:%02d", hours, minutes);
}
public boolean setActualTime() {
return this.setTime(this.getKnownProperty("time", String.class).toString());
}
/**
* {@inheritDoc}
*/

View File

@ -10,20 +10,20 @@ package com.onarandombox.MultiverseCore.configuration;
/**
* A {@link String} config-property that will NOT be saved to the config.
*/
public class TempStringConfigProperty implements MVConfigProperty<String> {
public class ActiveStringConfigProperty implements MVActiveConfigProperty<String> {
private String name;
private String value;
private String method;
private String help;
public TempStringConfigProperty(String name, String defaultValue, String help) {
public ActiveStringConfigProperty(String name, String defaultValue, String help) {
this.name = name;
this.help = help;
this.value = defaultValue;
this.parseValue(defaultValue);
}
public TempStringConfigProperty(String name, String defaultValue, String help, String method) {
public ActiveStringConfigProperty(String name, String defaultValue, String help, String method) {
this(name, defaultValue, help);
this.method = method;
}
@ -45,13 +45,28 @@ public class TempStringConfigProperty implements MVConfigProperty<String> {
}
/**
* Gets the method used to set this {@link TempStringConfigProperty}.
* @return The name of that method.
* {@inheritDoc}
*/
public String getMethod() {
return this.method;
}
/**
* {@inheritDoc}
*/
@Override
public void setMethod(String methodName) {
this.method = methodName;
}
/**
* {@inheritDoc}
*/
@Override
public Class<?> getPropertyClass() {
return String.class;
}
/**
* {@inheritDoc}
*/

View File

@ -12,12 +12,13 @@ import org.bukkit.configuration.ConfigurationSection;
/**
* A {@link Boolean} config-property.
*/
public class BooleanConfigProperty implements MVConfigProperty<Boolean> {
public class BooleanConfigProperty implements MVActiveConfigProperty<Boolean> {
private String name;
private Boolean value;
private String configNode;
private ConfigurationSection section;
private String help;
private String method;
public BooleanConfigProperty(ConfigurationSection section, String name, Boolean defaultValue, String help) {
this(section, name, defaultValue, name, help);
@ -32,6 +33,11 @@ public class BooleanConfigProperty implements MVConfigProperty<Boolean> {
this.setValue(this.section.getBoolean(this.configNode, defaultValue));
}
public BooleanConfigProperty(ConfigurationSection section, String name, Boolean defaultValue, String configNode, String help, String method) {
this(section, name, defaultValue, configNode, help);
this.method = method;
}
/**
* {@inheritDoc}
*/
@ -96,4 +102,34 @@ public class BooleanConfigProperty implements MVConfigProperty<Boolean> {
public String toString() {
return value.toString();
}
/**
* Gets the method that will be executed.
*
* @return The name of the method in MVWorld to be called.
*/
@Override
public String getMethod() {
return this.method;
}
/**
* Sets the method that will be executed.
*
* @param methodName The name of the method in MVWorld to be called.
*/
@Override
public void setMethod(String methodName) {
this.method = methodName;
}
/**
* Returns the class of the object we're looking at.
*
* @return the class of the object we're looking at.
*/
@Override
public Class<?> getPropertyClass() {
return Boolean.class;
}
}

View File

@ -1,5 +1,5 @@
/******************************************************************************
* Multiverse 2 Copyright (c) the Multiverse Team 2011. *
* Multiverse 2 Copyright (c) the Multiverse Team 2012. *
* Multiverse 2 is licensed under the BSD License. *
* For more information please check the README.md file included *
* with this project. *
@ -47,6 +47,22 @@ public class ConfigPropertyFactory {
return new BooleanConfigProperty(this.section, name, defaultValue, node, help);
}
/**
* Constructs a new ActiveBooleanConfigProperty
*
* This property will execute 'method' after it has been successfully set.
*
* @param name The name of this ConifgProperty
* @param defaultValue The default value.
* @param help What string is shown for help.
* @param node
* @param method The method that should be executed.
* @return The ActiveStringConfigProperty
*/
public BooleanConfigProperty getNewProperty(String name, boolean defaultValue, String help, String node, String method) {
return new BooleanConfigProperty(this.section, name, defaultValue, help, node, method);
}
// Integers
/**
* Constructs a new ConfigProperty.
@ -99,6 +115,19 @@ public class ConfigPropertyFactory {
return new DoubleConfigProperty(this.section, name, defaultValue, node, help);
}
/**
* Constructs a new ConfigProperty.
*
* @param name The name of this ConfigProperty.
* @param defaultValue The default-value.
* @param node The name of the configuration-node this ConfigProperty will be stored as.
* @param help The text that's displayed when a user failed to set the property.
* @return The ConfigProperty.
*/
public DoubleConfigProperty getNewProperty(String name, double defaultValue, String node, String help, String method) {
return new DoubleConfigProperty(this.section, name, defaultValue, node, help, method);
}
// Strings
/**
* Constructs a new ConfigProperty.
@ -230,18 +259,32 @@ public class ConfigPropertyFactory {
}
/**
* Constructs a new TempStringConfigProperty
* Constructs a new ConfigProperty.
*
* The boolean is a dummy. This is so I can differentiate from the non-temp one.
* @param name The name of this ConfigProperty.
* @param defaultValue The default-value.
* @param node The name of the configuration-node this ConfigProperty will be stored as.
* @param help The text that's displayed when a user failed to set the property.
* @return The ConfigProperty.
*/
public LocationConfigProperty getNewProperty(String name, Location defaultValue, String node, String help, String method) {
return new LocationConfigProperty(this.section, name, defaultValue, node, help, method);
}
/**
* Constructs a new ActiveStringConfigProperty
*
* This property will execute 'method' after it has been successfully set.
* This string will NOT be saved to the config file.
*
* @param name The name of this ConifgProperty
* @param defaultValue The default value.
* @param help What string is shown for help.
* @param method The method that should be executed.
* @param b Dummy.
* @return The TempStringConfigProperty
* @param saveToConfig Should the variable save to the config?
* @return The ActiveStringConfigProperty
*/
public TempStringConfigProperty getNewProperty(String name, String defaultValue, String help, String method, boolean b) {
return new TempStringConfigProperty(name, defaultValue, help, method);
public ActiveStringConfigProperty getNewProperty(String name, String defaultValue, String help, String method, boolean saveToConfig) {
return new ActiveStringConfigProperty(name, defaultValue, help, method);
}
}

View File

@ -13,7 +13,7 @@ import org.bukkit.configuration.ConfigurationSection;
/**
* A {@link Difficulty} config-property.
*/
public class DifficultyConfigProperty implements MVConfigProperty<Difficulty> {
public class DifficultyConfigProperty implements MVActiveConfigProperty<Difficulty> {
private String name;
private Difficulty value;
private String configNode;
@ -98,4 +98,32 @@ public class DifficultyConfigProperty implements MVConfigProperty<Difficulty> {
public String toString() {
return value.toString();
}
/**
* Gets the method that will be executed.
*
* @return The name of the method in MVWorld to be called.
*/
@Override
public String getMethod() {
return "setDifficulty";
}
/**
* Sets the method that will be executed.
*
* @param methodName The name of the method in MVWorld to be called.
*/
@Override
public void setMethod(String methodName) {
// Unused here. This will only ever be setDifficulty.
}
/**
* {@inheritDoc}
*/
@Override
public Class<?> getPropertyClass() {
return Difficulty.class;
}
}

View File

@ -12,12 +12,13 @@ import org.bukkit.configuration.ConfigurationSection;
/**
* A {@link Double} config-property.
*/
public class DoubleConfigProperty implements MVConfigProperty<Double> {
public class DoubleConfigProperty implements MVActiveConfigProperty<Double> {
private String name;
private Double value;
private String configNode;
private ConfigurationSection section;
private String help;
private String method;
public DoubleConfigProperty(ConfigurationSection section, String name, Double defaultValue, String help) {
this(section, name, defaultValue, name, help);
@ -32,6 +33,11 @@ public class DoubleConfigProperty implements MVConfigProperty<Double> {
this.setValue(this.section.getDouble(this.configNode, defaultValue));
}
public DoubleConfigProperty(ConfigurationSection section, String name, Double defaultValue, String configNode, String help, String method) {
this(section, name, defaultValue, configNode, help);
this.method = method;
}
/**
* {@inheritDoc}
*/
@ -94,4 +100,34 @@ public class DoubleConfigProperty implements MVConfigProperty<Double> {
public String toString() {
return value.toString();
}
/**
* Gets the method that will be executed.
*
* @return The name of the method in MVWorld to be called.
*/
@Override
public String getMethod() {
return this.method;
}
/**
* Sets the method that will be executed.
*
* @param methodName The name of the method in MVWorld to be called.
*/
@Override
public void setMethod(String methodName) {
this.method = methodName;
}
/**
* Returns the class of the object we're looking at.
*
* @return the class of the object we're looking at.
*/
@Override
public Class<?> getPropertyClass() {
return Double.class;
}
}

View File

@ -13,7 +13,7 @@ import org.bukkit.configuration.ConfigurationSection;
/**
* A {@link GameMode} config-property.
*/
public class GameModeConfigProperty implements MVConfigProperty<GameMode> {
public class GameModeConfigProperty implements MVActiveConfigProperty<GameMode> {
private String name;
private GameMode value;
private String configNode;
@ -98,4 +98,32 @@ public class GameModeConfigProperty implements MVConfigProperty<GameMode> {
public String toString() {
return value.toString();
}
/**
* Gets the method that will be executed.
*
* @return The name of the method in MVWorld to be called.
*/
@Override
public String getMethod() {
return "setActualGameMode";
}
/**
* Sets the method that will be executed.
*
* @param methodName The name of the method in MVWorld to be called.
*/
@Override
public void setMethod(String methodName) {
// Not required. Gamemode will only ever be one.
}
/**
* {@inheritDoc}
*/
@Override
public Class<?> getPropertyClass() {
return GameMode.class;
}
}

View File

@ -14,12 +14,13 @@ import org.bukkit.configuration.ConfigurationSection;
/**
* A {@link Location} config-property.
*/
public class LocationConfigProperty implements MVConfigProperty<Location> {
public class LocationConfigProperty implements MVActiveConfigProperty<Location> {
private String name;
private Location value;
private String configNode;
private ConfigurationSection section;
private String help;
private String method;
public LocationConfigProperty(ConfigurationSection section, String name, Location defaultValue, String help) {
this(section, name, defaultValue, name, help);
@ -34,6 +35,11 @@ public class LocationConfigProperty implements MVConfigProperty<Location> {
this.setValue(this.getLocationFromConfig(defaultValue));
}
public LocationConfigProperty(ConfigurationSection section, String name, Location defaultValue, String configNode, String help, String method) {
this(section, name, defaultValue, configNode, help);
this.method = method;
}
/**
* {@inheritDoc}
*/
@ -112,4 +118,34 @@ public class LocationConfigProperty implements MVConfigProperty<Location> {
public String toString() {
return LocationManipulation.strCoordsRaw(this.value);
}
/**
* Gets the method that will be executed.
*
* @return The name of the method in MVWorld to be called.
*/
@Override
public String getMethod() {
return this.method;
}
/**
* Sets the method that will be executed.
*
* @param methodName The name of the method in MVWorld to be called.
*/
@Override
public void setMethod(String methodName) {
this.method = methodName;
}
/**
* Returns the class of the object we're looking at.
*
* @return the class of the object we're looking at.
*/
@Override
public Class<?> getPropertyClass() {
return Location.class;
}
}

View File

@ -0,0 +1,30 @@
/******************************************************************************
* Multiverse 2 Copyright (c) the Multiverse Team 2012. *
* Multiverse 2 is licensed under the BSD License. *
* For more information please check the README.md file included *
* with this project. *
******************************************************************************/
package com.onarandombox.MultiverseCore.configuration;
public interface MVActiveConfigProperty<T> extends MVConfigProperty<T> {
/**
* Gets the method that will be executed.
*
* @return The name of the method in MVWorld to be called.
*/
String getMethod();
/**
* Sets the method that will be executed.
*
* @param methodName The name of the method in MVWorld to be called.
*/
void setMethod(String methodName);
/**
* Returns the class of the object we're looking at.
* @return the class of the object we're looking at.
*/
Class<?> getPropertyClass();
}