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;
|
2014-12-08 00:12:52 +01:00
|
|
|
import com.massivecraft.factions.zcore.util.TL;
|
2014-04-04 20:55:21 +02:00
|
|
|
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 {
|
2014-08-05 17:17:27 +02:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
private static HashMap<String, String> properFieldNames = new HashMap<String, String>();
|
|
|
|
|
|
|
|
public CmdConfig() {
|
2014-07-01 22:10:18 +02:00
|
|
|
super();
|
|
|
|
this.aliases.add("config");
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 22:10:18 +02:00
|
|
|
this.requiredArgs.add("setting");
|
|
|
|
this.requiredArgs.add("value");
|
|
|
|
this.errorOnToManyArgs = false;
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 22:10:18 +02:00
|
|
|
this.permission = Permission.CONFIG.node;
|
|
|
|
this.disableOnLock = true;
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 22:10:18 +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 22:10:18 +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 22:10:18 +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 22:10:18 +02:00
|
|
|
}
|
|
|
|
String fieldName = properFieldNames.get(field);
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
if (fieldName == null || fieldName.isEmpty()) {
|
2014-12-08 00:12:52 +01:00
|
|
|
msg(TL.COMMAND_CONFIG_NOEXIST, field);
|
2014-07-01 22:10:18 +02:00
|
|
|
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 22:10:18 +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 22:10:18 +02:00
|
|
|
boolean targetValue = this.strAsBool(value);
|
|
|
|
target.setBoolean(null, targetValue);
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
if (targetValue) {
|
2014-12-08 00:12:52 +01:00
|
|
|
success = "\"" + fieldName + TL.COMMAND_CONFIG_SET_TRUE.toString();
|
2014-07-01 21:52:40 +02:00
|
|
|
} else {
|
2014-12-08 00:12:52 +01:00
|
|
|
success = "\"" + fieldName + TL.COMMAND_CONFIG_SET_FALSE.toString();
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// int
|
|
|
|
else if (target.getType() == int.class) {
|
|
|
|
try {
|
2014-07-01 22:10:18 +02:00
|
|
|
int intVal = Integer.parseInt(value);
|
|
|
|
target.setInt(null, intVal);
|
2014-12-08 00:12:52 +01:00
|
|
|
success = "\"" + fieldName + TL.COMMAND_CONFIG_OPTIONSET.toString() + intVal + ".";
|
2014-04-04 20:55:21 +02:00
|
|
|
} catch (NumberFormatException ex) {
|
2014-12-08 00:12:52 +01:00
|
|
|
sendMessage(TL.COMMAND_CONFIG_INTREQUIRED.format(fieldName));
|
2014-07-01 22:10:18 +02:00
|
|
|
return;
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// long
|
|
|
|
else if (target.getType() == long.class) {
|
|
|
|
try {
|
2014-07-01 22:10:18 +02:00
|
|
|
long longVal = Long.parseLong(value);
|
|
|
|
target.setLong(null, longVal);
|
2014-12-08 00:12:52 +01:00
|
|
|
success = "\"" + fieldName + TL.COMMAND_CONFIG_OPTIONSET.toString() + longVal + ".";
|
2014-04-04 20:55:21 +02:00
|
|
|
} catch (NumberFormatException ex) {
|
2014-12-08 00:12:52 +01:00
|
|
|
sendMessage(TL.COMMAND_CONFIG_LONGREQUIRED.format(fieldName));
|
2014-04-04 20:55:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// double
|
|
|
|
else if (target.getType() == double.class) {
|
|
|
|
try {
|
2014-07-01 22:10:18 +02:00
|
|
|
double doubleVal = Double.parseDouble(value);
|
|
|
|
target.setDouble(null, doubleVal);
|
2014-12-08 00:12:52 +01:00
|
|
|
success = "\"" + fieldName + TL.COMMAND_CONFIG_OPTIONSET.toString() + doubleVal + ".";
|
2014-04-04 20:55:21 +02:00
|
|
|
} catch (NumberFormatException ex) {
|
2014-12-08 00:12:52 +01:00
|
|
|
sendMessage(TL.COMMAND_CONFIG_DOUBLEREQUIRED.format(fieldName));
|
2014-07-01 22:10:18 +02:00
|
|
|
return;
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// float
|
|
|
|
else if (target.getType() == float.class) {
|
|
|
|
try {
|
2014-07-01 22:10:18 +02:00
|
|
|
float floatVal = Float.parseFloat(value);
|
|
|
|
target.setFloat(null, floatVal);
|
2014-12-08 00:12:52 +01:00
|
|
|
success = "\"" + fieldName + TL.COMMAND_CONFIG_OPTIONSET.toString() + floatVal + ".";
|
2014-04-04 20:55:21 +02:00
|
|
|
} catch (NumberFormatException ex) {
|
2014-12-08 00:12:52 +01:00
|
|
|
sendMessage(TL.COMMAND_CONFIG_FLOATREQUIRED.format(fieldName));
|
2014-07-01 22:10:18 +02:00
|
|
|
return;
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String
|
|
|
|
else if (target.getType() == String.class) {
|
2014-07-01 22:10:18 +02:00
|
|
|
target.set(null, value);
|
2014-12-08 00:12:52 +01:00
|
|
|
success = "\"" + fieldName + TL.COMMAND_CONFIG_OPTIONSET.toString() + value + "\".";
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChatColor
|
|
|
|
else if (target.getType() == ChatColor.class) {
|
2014-07-01 22:10:18 +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 22:10:18 +02:00
|
|
|
}
|
|
|
|
if (newColor == null) {
|
2014-12-11 17:05:04 +01:00
|
|
|
sendMessage(TL.COMMAND_CONFIG_INVALID_COLOUR.format(fieldName, value.toUpperCase()));
|
2014-04-04 20:55:21 +02:00
|
|
|
return;
|
2014-07-01 22:10:18 +02:00
|
|
|
}
|
|
|
|
target.set(null, newColor);
|
2014-12-08 00:12:52 +01:00
|
|
|
success = "\"" + fieldName + TL.COMMAND_CONFIG_COLOURSET.toString() + value.toUpperCase() + "\".";
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2014-12-08 00:12:52 +01:00
|
|
|
sendMessage(TL.COMMAND_CONFIG_INVALID_COLLECTION.format(fieldName));
|
2014-04-04 20:55:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set<Material>
|
|
|
|
else if (innerType == Material.class) {
|
2014-07-01 22:10:18 +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 22:10:18 +02:00
|
|
|
}
|
|
|
|
if (newMat == null) {
|
2014-12-11 17:05:04 +01:00
|
|
|
sendMessage(TL.COMMAND_CONFIG_INVALID_MATERIAL.format(fieldName, value.toUpperCase()));
|
2014-04-04 20:55:21 +02:00
|
|
|
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 22:10:18 +02:00
|
|
|
matSet.remove(newMat);
|
|
|
|
target.set(null, matSet);
|
2014-12-11 17:05:04 +01:00
|
|
|
success = TL.COMMAND_CONFIG_MATERIAL_REMOVED.format(fieldName, value.toUpperCase());
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
// Material not present yet, add it
|
|
|
|
else {
|
2014-07-01 22:10:18 +02:00
|
|
|
matSet.add(newMat);
|
|
|
|
target.set(null, matSet);
|
2014-12-11 17:05:04 +01:00
|
|
|
success = TL.COMMAND_CONFIG_MATERIAL_ADDED.format(fieldName, value.toUpperCase());
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 22:10:18 +02:00
|
|
|
stringSet.remove(value);
|
|
|
|
target.set(null, stringSet);
|
2014-12-11 17:05:04 +01:00
|
|
|
success = TL.COMMAND_CONFIG_SET_REMOVED.format(fieldName, value);
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
// String not present yet, add it
|
|
|
|
else {
|
2014-07-01 22:10:18 +02:00
|
|
|
stringSet.add(value);
|
|
|
|
target.set(null, stringSet);
|
2014-12-11 17:05:04 +01:00
|
|
|
success = TL.COMMAND_CONFIG_SET_ADDED.format(fieldName, value);
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set of unknown type
|
|
|
|
else {
|
2014-12-08 00:12:52 +01:00
|
|
|
sendMessage(TL.COMMAND_CONFIG_INVALID_TYPESET.format(fieldName));
|
2014-04-04 20:55:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unknown type
|
|
|
|
else {
|
2014-12-11 17:05:04 +01:00
|
|
|
sendMessage(TL.COMMAND_CONFIG_ERROR_TYPE.format(fieldName, target.getClass().getName()));
|
2014-04-04 20:55:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (NoSuchFieldException ex) {
|
2014-12-08 00:12:52 +01:00
|
|
|
sendMessage(TL.COMMAND_CONFIG_ERROR_MATCHING.format(fieldName));
|
2014-04-04 20:55:21 +02:00
|
|
|
return;
|
|
|
|
} catch (IllegalAccessException ex) {
|
2014-12-11 17:05:04 +01:00
|
|
|
sendMessage(TL.COMMAND_CONFIG_ERROR_SETTING.format(fieldName, value));
|
2014-07-01 22:10:18 +02:00
|
|
|
return;
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!success.isEmpty()) {
|
|
|
|
if (sender instanceof Player) {
|
2014-07-01 22:10:18 +02:00
|
|
|
sendMessage(success);
|
2014-12-11 17:05:04 +01:00
|
|
|
P.p.log(success + TL.COMMAND_CONFIG_LOG.format((Player) sender));
|
2014-07-01 21:52:40 +02:00
|
|
|
} 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 22:10:18 +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
|
|
|
|
|
|
|
}
|