2011-10-09 21:57:43 +02:00
|
|
|
package com.massivecraft.factions.cmd;
|
2011-07-22 14:25:12 +02:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
import com.massivecraft.factions.Conf;
|
|
|
|
import com.massivecraft.factions.P;
|
|
|
|
import com.massivecraft.factions.struct.Permission;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2011-07-22 14:25:12 +02:00
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.ParameterizedType;
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.HashMap;
|
2014-04-04 20:55:21 +02:00
|
|
|
import java.util.Set;
|
2011-07-22 14:25:12 +02:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
public class CmdConfig extends FCommand {
|
|
|
|
private static HashMap<String, String> properFieldNames = new HashMap<String, String>();
|
|
|
|
|
|
|
|
public CmdConfig() {
|
2014-07-01 21:52:40 +02:00
|
|
|
super(); this.aliases.add("config");
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 21:52:40 +02:00
|
|
|
this.requiredArgs.add("setting"); this.requiredArgs.add("value"); this.errorOnToManyArgs = false;
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 21:52:40 +02:00
|
|
|
this.permission = Permission.CONFIG.node; this.disableOnLock = true;
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 21:52:40 +02:00
|
|
|
senderMustBePlayer = false; senderMustBeMember = false; senderMustBeModerator = false;
|
2014-04-04 20:55:21 +02:00
|
|
|
senderMustBeAdmin = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void perform() {
|
|
|
|
// store a lookup map of lowercase field names paired with proper capitalization field names
|
|
|
|
// that way, if the person using this command messes up the capitalization, we can fix that
|
|
|
|
if (properFieldNames.isEmpty()) {
|
2014-07-01 21:52:40 +02:00
|
|
|
Field[] fields = Conf.class.getDeclaredFields(); for (int i = 0; i < fields.length; i++) {
|
2014-04-04 20:55:21 +02:00
|
|
|
properFieldNames.put(fields[i].getName().toLowerCase(), fields[i].getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 21:52:40 +02:00
|
|
|
String field = this.argAsString(0).toLowerCase(); if (field.startsWith("\"") && field.endsWith("\"")) {
|
2014-04-04 20:55:21 +02:00
|
|
|
field = field.substring(1, field.length() - 1);
|
2014-07-01 21:52:40 +02:00
|
|
|
} String fieldName = properFieldNames.get(field);
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
if (fieldName == null || fieldName.isEmpty()) {
|
2014-07-01 21:52:40 +02:00
|
|
|
msg("<b>No configuration setting \"<h>%s<b>\" exists.", field); return;
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
2014-04-15 19:42:09 +02:00
|
|
|
String success;
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 21:52:40 +02:00
|
|
|
String value = args.get(1); for (int i = 2; i < args.size(); i++) {
|
2014-04-04 20:55:21 +02:00
|
|
|
value += ' ' + args.get(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Field target = Conf.class.getField(fieldName);
|
|
|
|
|
|
|
|
// boolean
|
|
|
|
if (target.getType() == boolean.class) {
|
2014-07-01 21:52:40 +02:00
|
|
|
boolean targetValue = this.strAsBool(value); target.setBoolean(null, targetValue);
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
if (targetValue) {
|
|
|
|
success = "\"" + fieldName + "\" option set to true (enabled).";
|
2014-07-01 21:52:40 +02:00
|
|
|
} else {
|
2014-04-04 20:55:21 +02:00
|
|
|
success = "\"" + fieldName + "\" option set to false (disabled).";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// int
|
|
|
|
else if (target.getType() == int.class) {
|
|
|
|
try {
|
2014-07-01 21:52:40 +02:00
|
|
|
int intVal = Integer.parseInt(value); target.setInt(null, intVal);
|
2014-04-04 20:55:21 +02:00
|
|
|
success = "\"" + fieldName + "\" option set to " + intVal + ".";
|
|
|
|
} catch (NumberFormatException ex) {
|
2014-07-01 21:52:40 +02:00
|
|
|
sendMessage("Cannot set \"" + fieldName + "\": integer (whole number) value required."); return;
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// long
|
|
|
|
else if (target.getType() == long.class) {
|
|
|
|
try {
|
2014-07-01 21:52:40 +02:00
|
|
|
long longVal = Long.parseLong(value); target.setLong(null, longVal);
|
2014-04-04 20:55:21 +02:00
|
|
|
success = "\"" + fieldName + "\" option set to " + longVal + ".";
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
sendMessage("Cannot set \"" + fieldName + "\": long integer (whole number) value required.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// double
|
|
|
|
else if (target.getType() == double.class) {
|
|
|
|
try {
|
2014-07-01 21:52:40 +02:00
|
|
|
double doubleVal = Double.parseDouble(value); target.setDouble(null, doubleVal);
|
2014-04-04 20:55:21 +02:00
|
|
|
success = "\"" + fieldName + "\" option set to " + doubleVal + ".";
|
|
|
|
} catch (NumberFormatException ex) {
|
2014-07-01 21:52:40 +02:00
|
|
|
sendMessage("Cannot set \"" + fieldName + "\": double (numeric) value required."); return;
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// float
|
|
|
|
else if (target.getType() == float.class) {
|
|
|
|
try {
|
2014-07-01 21:52:40 +02:00
|
|
|
float floatVal = Float.parseFloat(value); target.setFloat(null, floatVal);
|
2014-04-04 20:55:21 +02:00
|
|
|
success = "\"" + fieldName + "\" option set to " + floatVal + ".";
|
|
|
|
} catch (NumberFormatException ex) {
|
2014-07-01 21:52:40 +02:00
|
|
|
sendMessage("Cannot set \"" + fieldName + "\": float (numeric) value required."); return;
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String
|
|
|
|
else if (target.getType() == String.class) {
|
2014-07-01 21:52:40 +02:00
|
|
|
target.set(null, value); success = "\"" + fieldName + "\" option set to \"" + value + "\".";
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChatColor
|
|
|
|
else if (target.getType() == ChatColor.class) {
|
2014-07-01 21:52:40 +02:00
|
|
|
ChatColor newColor = null; try {
|
2014-04-04 20:55:21 +02:00
|
|
|
newColor = ChatColor.valueOf(value.toUpperCase());
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
|
2014-07-01 21:52:40 +02:00
|
|
|
} if (newColor == null) {
|
2014-04-04 20:55:21 +02:00
|
|
|
sendMessage("Cannot set \"" + fieldName + "\": \"" + value.toUpperCase() + "\" is not a valid color.");
|
|
|
|
return;
|
2014-07-01 21:52:40 +02:00
|
|
|
} target.set(null, newColor);
|
2014-04-04 20:55:21 +02:00
|
|
|
success = "\"" + fieldName + "\" color option set to \"" + value.toUpperCase() + "\".";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set<?> or other parameterized collection
|
|
|
|
else if (target.getGenericType() instanceof ParameterizedType) {
|
|
|
|
ParameterizedType targSet = (ParameterizedType) target.getGenericType();
|
|
|
|
Type innerType = targSet.getActualTypeArguments()[0];
|
|
|
|
|
|
|
|
// not a Set, somehow, and that should be the only collection we're using in Conf.java
|
|
|
|
if (targSet.getRawType() != Set.class) {
|
|
|
|
sendMessage("\"" + fieldName + "\" is not a data collection type which can be modified with this command.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set<Material>
|
|
|
|
else if (innerType == Material.class) {
|
2014-07-01 21:52:40 +02:00
|
|
|
Material newMat = null; try {
|
2014-04-04 20:55:21 +02:00
|
|
|
newMat = Material.valueOf(value.toUpperCase());
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
|
2014-07-01 21:52:40 +02:00
|
|
|
} if (newMat == null) {
|
2014-04-04 20:55:21 +02:00
|
|
|
sendMessage("Cannot change \"" + fieldName + "\" set: \"" + value.toUpperCase() + "\" is not a valid material.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-01 21:52:40 +02:00
|
|
|
@SuppressWarnings("unchecked") Set<Material> matSet = (Set<Material>) target.get(null);
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
// Material already present, so remove it
|
|
|
|
if (matSet.contains(newMat)) {
|
2014-07-01 21:52:40 +02:00
|
|
|
matSet.remove(newMat); target.set(null, matSet);
|
2014-04-04 20:55:21 +02:00
|
|
|
success = "\"" + fieldName + "\" set: Material \"" + value.toUpperCase() + "\" removed.";
|
|
|
|
}
|
|
|
|
// Material not present yet, add it
|
|
|
|
else {
|
2014-07-01 21:52:40 +02:00
|
|
|
matSet.add(newMat); target.set(null, matSet);
|
2014-04-04 20:55:21 +02:00
|
|
|
success = "\"" + fieldName + "\" set: Material \"" + value.toUpperCase() + "\" added.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set<String>
|
|
|
|
else if (innerType == String.class) {
|
2014-07-01 21:52:40 +02:00
|
|
|
@SuppressWarnings("unchecked") Set<String> stringSet = (Set<String>) target.get(null);
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
// String already present, so remove it
|
|
|
|
if (stringSet.contains(value)) {
|
2014-07-01 21:52:40 +02:00
|
|
|
stringSet.remove(value); target.set(null, stringSet);
|
2014-04-04 20:55:21 +02:00
|
|
|
success = "\"" + fieldName + "\" set: \"" + value + "\" removed.";
|
|
|
|
}
|
|
|
|
// String not present yet, add it
|
|
|
|
else {
|
2014-07-01 21:52:40 +02:00
|
|
|
stringSet.add(value); target.set(null, stringSet);
|
2014-04-04 20:55:21 +02:00
|
|
|
success = "\"" + fieldName + "\" set: \"" + value + "\" added.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set of unknown type
|
|
|
|
else {
|
|
|
|
sendMessage("\"" + fieldName + "\" is not a data type set which can be modified with this command.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unknown type
|
|
|
|
else {
|
|
|
|
sendMessage("\"" + fieldName + "\" is not a data type which can be modified with this command.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (NoSuchFieldException ex) {
|
|
|
|
sendMessage("Configuration setting \"" + fieldName + "\" couldn't be matched, though it should be... please report this error.");
|
|
|
|
return;
|
|
|
|
} catch (IllegalAccessException ex) {
|
2014-07-01 21:52:40 +02:00
|
|
|
sendMessage("Error setting configuration setting \"" + fieldName + "\" to \"" + value + "\"."); return;
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!success.isEmpty()) {
|
|
|
|
if (sender instanceof Player) {
|
2014-07-01 21:52:40 +02:00
|
|
|
sendMessage(success); P.p.log(success + " Command was run by " + fme.getName() + ".");
|
|
|
|
} else // using P.p.log() instead of sendMessage if run from server console so that "[Factions v#.#.#]" is prepended in server log
|
2014-07-01 21:49:42 +02:00
|
|
|
{ P.p.log(success); }
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
// save change to disk
|
|
|
|
Conf.save();
|
|
|
|
}
|
2011-07-22 14:25:12 +02:00
|
|
|
|
|
|
|
}
|