Saber-Factions/src/main/java/com/massivecraft/factions/cmd/CmdConfig.java

250 lines
9.7 KiB
Java
Raw Normal View History

2011-10-09 21:57:43 +02:00
package com.massivecraft.factions.cmd;
2014-04-04 20:55:21 +02:00
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.SavageFactions;
2014-04-04 20:55:21 +02:00
import com.massivecraft.factions.struct.Permission;
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;
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;
2014-04-04 20:55:21 +02:00
public class CmdConfig extends FCommand {
2017-12-19 11:18:13 +01:00
private static HashMap<String, String> properFieldNames = new HashMap<>();
2014-04-04 20:55:21 +02:00
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;
2018-03-26 23:43:15 +02:00
senderMustBeColeader = 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();
2017-12-19 11:18:13 +01:00
for (Field field : fields) {
properFieldNames.put(field.getName().toLowerCase(), field.getName());
2014-04-04 20:55:21 +02:00
}
}
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()) {
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
2017-12-19 11:18:13 +01:00
StringBuilder value = new StringBuilder(args.get(1));
2014-07-01 22:10:18 +02:00
for (int i = 2; i < args.size(); i++) {
2017-12-19 11:18:13 +01:00
value.append(' ').append(args.get(i));
2014-04-04 20:55:21 +02:00
}
try {
Field target = Conf.class.getField(fieldName);
// boolean
if (target.getType() == boolean.class) {
2017-12-19 11:18:13 +01:00
boolean targetValue = this.strAsBool(value.toString());
2014-07-01 22:10:18 +02:00
target.setBoolean(null, targetValue);
2014-04-04 20:55:21 +02:00
if (targetValue) {
success = "\"" + fieldName + TL.COMMAND_CONFIG_SET_TRUE.toString();
2014-07-01 21:52:40 +02:00
} else {
success = "\"" + fieldName + TL.COMMAND_CONFIG_SET_FALSE.toString();
2014-04-04 20:55:21 +02:00
}
}
// int
else if (target.getType() == int.class) {
try {
2017-12-19 11:18:13 +01:00
int intVal = Integer.parseInt(value.toString());
2014-07-01 22:10:18 +02:00
target.setInt(null, intVal);
success = "\"" + fieldName + TL.COMMAND_CONFIG_OPTIONSET.toString() + intVal + ".";
2014-04-04 20:55:21 +02:00
} catch (NumberFormatException ex) {
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 {
2017-12-19 11:18:13 +01:00
long longVal = Long.parseLong(value.toString());
2014-07-01 22:10:18 +02:00
target.setLong(null, longVal);
success = "\"" + fieldName + TL.COMMAND_CONFIG_OPTIONSET.toString() + longVal + ".";
2014-04-04 20:55:21 +02:00
} catch (NumberFormatException ex) {
sendMessage(TL.COMMAND_CONFIG_LONGREQUIRED.format(fieldName));
2014-04-04 20:55:21 +02:00
return;
}
}
// double
else if (target.getType() == double.class) {
try {
2017-12-19 11:18:13 +01:00
double doubleVal = Double.parseDouble(value.toString());
2014-07-01 22:10:18 +02:00
target.setDouble(null, doubleVal);
success = "\"" + fieldName + TL.COMMAND_CONFIG_OPTIONSET.toString() + doubleVal + ".";
2014-04-04 20:55:21 +02:00
} catch (NumberFormatException ex) {
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 {
2017-12-19 11:18:13 +01:00
float floatVal = Float.parseFloat(value.toString());
2014-07-01 22:10:18 +02:00
target.setFloat(null, floatVal);
success = "\"" + fieldName + TL.COMMAND_CONFIG_OPTIONSET.toString() + floatVal + ".";
2014-04-04 20:55:21 +02:00
} catch (NumberFormatException ex) {
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) {
2017-12-19 11:18:13 +01:00
target.set(null, value.toString());
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 {
2017-12-19 11:18:13 +01:00
newColor = ChatColor.valueOf(value.toString().toUpperCase());
2014-04-04 20:55:21 +02:00
} catch (IllegalArgumentException ex) {
2014-07-01 22:10:18 +02:00
}
if (newColor == null) {
2017-12-19 11:18:13 +01:00
sendMessage(TL.COMMAND_CONFIG_INVALID_COLOUR.format(fieldName, value.toString().toUpperCase()));
2014-04-04 20:55:21 +02:00
return;
2014-07-01 22:10:18 +02:00
}
target.set(null, newColor);
2017-12-19 11:18:13 +01:00
success = "\"" + fieldName + TL.COMMAND_CONFIG_COLOURSET.toString() + value.toString().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) {
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 {
2017-12-19 11:18:13 +01:00
newMat = Material.valueOf(value.toString().toUpperCase());
2014-04-04 20:55:21 +02:00
} catch (IllegalArgumentException ex) {
2014-07-01 22:10:18 +02:00
}
if (newMat == null) {
2017-12-19 11:18:13 +01:00
sendMessage(TL.COMMAND_CONFIG_INVALID_MATERIAL.format(fieldName, value.toString().toUpperCase()));
2014-04-04 20:55:21 +02:00
return;
}
@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);
2017-12-19 11:18:13 +01:00
success = TL.COMMAND_CONFIG_MATERIAL_REMOVED.format(fieldName, value.toString().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);
2017-12-19 11:18:13 +01:00
success = TL.COMMAND_CONFIG_MATERIAL_ADDED.format(fieldName, value.toString().toUpperCase());
2014-04-04 20:55:21 +02:00
}
}
// Set<String>
else if (innerType == String.class) {
@SuppressWarnings("unchecked") Set<String> stringSet = (Set<String>) target.get(null);
2014-04-04 20:55:21 +02:00
// String already present, so remove it
2017-12-19 11:18:13 +01:00
if (stringSet.contains(value.toString())) {
stringSet.remove(value.toString());
2014-07-01 22:10:18 +02:00
target.set(null, stringSet);
2017-12-19 11:18:13 +01:00
success = TL.COMMAND_CONFIG_SET_REMOVED.format(fieldName, value.toString());
2014-04-04 20:55:21 +02:00
}
// String not present yet, add it
else {
2017-12-19 11:18:13 +01:00
stringSet.add(value.toString());
2014-07-01 22:10:18 +02:00
target.set(null, stringSet);
2017-12-19 11:18:13 +01:00
success = TL.COMMAND_CONFIG_SET_ADDED.format(fieldName, value.toString());
2014-04-04 20:55:21 +02:00
}
}
// Set of unknown type
else {
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) {
sendMessage(TL.COMMAND_CONFIG_ERROR_MATCHING.format(fieldName));
2014-04-04 20:55:21 +02:00
return;
} catch (IllegalAccessException ex) {
2017-12-19 11:18:13 +01:00
sendMessage(TL.COMMAND_CONFIG_ERROR_SETTING.format(fieldName, value.toString()));
2014-07-01 22:10:18 +02:00
return;
2014-04-04 20:55:21 +02:00
}
if (!success.isEmpty()) {
2014-04-04 20:55:21 +02:00
if (sender instanceof Player) {
2014-07-01 22:10:18 +02:00
sendMessage(success);
2018-11-07 06:38:43 +01:00
SavageFactions.plugin.log(success + TL.COMMAND_CONFIG_LOG.format((Player) sender));
} else // using SavageFactions.plugin.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
{
2018-11-07 06:38:43 +01:00
SavageFactions.plugin.log(success);
2014-07-01 22:10:18 +02:00
}
2014-04-04 20:55:21 +02:00
}
// save change to disk
Conf.save();
}
@Override
public TL getUsageTranslation() {
return TL.COMMAND_CONFIG_DESCRIPTION;
}
}