2011-10-09 21:57:43 +02:00
|
|
|
package com.massivecraft.factions.cmd;
|
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.Set;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
|
|
|
import com.massivecraft.factions.Conf;
|
2011-10-08 22:03:44 +02:00
|
|
|
import com.massivecraft.factions.P;
|
2011-10-05 12:13:54 +02:00
|
|
|
import com.massivecraft.factions.integration.SpoutFeatures;
|
2011-10-09 14:53:38 +02:00
|
|
|
import com.massivecraft.factions.struct.Permission;
|
2011-07-22 14:25:12 +02:00
|
|
|
|
2011-10-09 20:10:19 +02:00
|
|
|
public class CmdConfig extends FCommand
|
2011-10-09 14:53:38 +02:00
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
private static HashMap<String, String> properFieldNames = new HashMap<String, String>();
|
|
|
|
|
2011-10-09 20:10:19 +02:00
|
|
|
public CmdConfig()
|
2011-10-09 14:53:38 +02:00
|
|
|
{
|
|
|
|
super();
|
|
|
|
this.aliases.add("config");
|
|
|
|
|
|
|
|
this.requiredArgs.add("setting");
|
|
|
|
this.requiredArgs.add("value");
|
|
|
|
this.requiredArgs.add("");
|
|
|
|
//this.optionalArgs.put("", "");
|
|
|
|
|
2011-10-09 21:57:43 +02:00
|
|
|
this.permission = Permission.CONFIG.node;
|
|
|
|
this.disableOnLock = true;
|
2011-10-09 14:53:38 +02:00
|
|
|
|
2011-07-22 14:25:12 +02:00
|
|
|
senderMustBePlayer = false;
|
2011-10-09 14:53:38 +02:00
|
|
|
senderMustBeMember = false;
|
|
|
|
senderMustBeModerator = false;
|
|
|
|
senderMustBeAdmin = false;
|
2011-07-22 14:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-10-09 14:53:38 +02:00
|
|
|
public void perform()
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
// 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
|
2011-10-09 14:53:38 +02:00
|
|
|
if (properFieldNames.isEmpty())
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
Field[] fields = Conf.class.getDeclaredFields();
|
2011-10-09 14:53:38 +02:00
|
|
|
for(int i = 0; i < fields.length; i++)
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
properFieldNames.put(fields[i].getName().toLowerCase(), fields[i].getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-09 14:53:38 +02:00
|
|
|
String field = this.argAsString(0).toLowerCase();
|
|
|
|
if (field.startsWith("\"") && field.endsWith("\""))
|
|
|
|
{
|
2011-08-04 09:27:58 +02:00
|
|
|
field = field.substring(1, field.length() - 1);
|
|
|
|
}
|
|
|
|
String fieldName = properFieldNames.get(field);
|
2011-07-22 14:25:12 +02:00
|
|
|
|
2011-10-09 14:53:38 +02:00
|
|
|
if (fieldName == null || fieldName.isEmpty())
|
|
|
|
{
|
|
|
|
sendMessageParsed("<b>No configuration setting \"<h>%s<b>\" exists.", field);
|
2011-07-22 14:25:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String success = "";
|
|
|
|
|
2011-10-09 14:53:38 +02:00
|
|
|
String value = args.get(1);
|
|
|
|
for(int i = 2; i < args.size(); i++)
|
|
|
|
{
|
|
|
|
value += ' ' + args.get(i);
|
2011-07-22 14:25:12 +02:00
|
|
|
}
|
|
|
|
|
2011-10-09 14:53:38 +02:00
|
|
|
try
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
Field target = Conf.class.getField(fieldName);
|
|
|
|
|
|
|
|
// boolean
|
2011-10-09 14:53:38 +02:00
|
|
|
if (target.getType() == boolean.class)
|
|
|
|
{
|
2011-10-09 20:10:19 +02:00
|
|
|
boolean targetValue = this.strAsBool(value);
|
|
|
|
target.setBoolean(null, targetValue);
|
|
|
|
|
|
|
|
if (targetValue)
|
2011-10-09 14:53:38 +02:00
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
success = "\""+fieldName+"\" option set to true (enabled).";
|
|
|
|
}
|
2011-10-09 14:53:38 +02:00
|
|
|
else
|
|
|
|
{
|
2011-10-09 20:10:19 +02:00
|
|
|
success = "\""+fieldName+"\" option set to false (disabled).";
|
2011-07-22 14:25:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// int
|
2011-10-09 14:53:38 +02:00
|
|
|
else if (target.getType() == int.class)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
int intVal = Integer.parseInt(value);
|
|
|
|
target.setInt(null, intVal);
|
|
|
|
success = "\""+fieldName+"\" option set to "+intVal+".";
|
|
|
|
}
|
2011-10-09 14:53:38 +02:00
|
|
|
catch(NumberFormatException ex)
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
sendMessage("Cannot set \""+fieldName+"\": integer (whole number) value required.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// double
|
2011-10-09 14:53:38 +02:00
|
|
|
else if (target.getType() == double.class)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
double doubleVal = Double.parseDouble(value);
|
|
|
|
target.setDouble(null, doubleVal);
|
|
|
|
success = "\""+fieldName+"\" option set to "+doubleVal+".";
|
|
|
|
}
|
2011-10-09 14:53:38 +02:00
|
|
|
catch(NumberFormatException ex)
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
sendMessage("Cannot set \""+fieldName+"\": double (numeric) value required.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String
|
2011-10-09 14:53:38 +02:00
|
|
|
else if (target.getType() == String.class)
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
target.set(null, value);
|
|
|
|
success = "\""+fieldName+"\" option set to \""+value+"\".";
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChatColor
|
2011-10-09 14:53:38 +02:00
|
|
|
else if (target.getType() == ChatColor.class)
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
ChatColor newColor = null;
|
2011-10-09 14:53:38 +02:00
|
|
|
try
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
newColor = ChatColor.valueOf(value.toUpperCase());
|
|
|
|
}
|
2011-10-09 14:53:38 +02:00
|
|
|
catch (IllegalArgumentException ex)
|
|
|
|
{
|
|
|
|
|
2011-07-22 14:25:12 +02:00
|
|
|
}
|
2011-10-09 14:53:38 +02:00
|
|
|
if (newColor == null)
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
sendMessage("Cannot set \""+fieldName+"\": \""+value.toUpperCase()+"\" is not a valid color.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
target.set(null, newColor);
|
|
|
|
success = "\""+fieldName+"\" color option set to \""+value.toUpperCase()+"\".";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set<?> or other parameterized collection
|
2011-10-09 14:53:38 +02:00
|
|
|
else if (target.getGenericType() instanceof ParameterizedType)
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
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
|
2011-10-09 14:53:38 +02:00
|
|
|
if (targSet.getRawType() != Set.class)
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
sendMessage("\""+fieldName+"\" is not a data collection type which can be modified with this command.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set<Material>
|
2011-10-09 14:53:38 +02:00
|
|
|
else if (innerType == Material.class)
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
Material newMat = null;
|
2011-10-09 14:53:38 +02:00
|
|
|
try
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
newMat = Material.valueOf(value.toUpperCase());
|
|
|
|
}
|
2011-10-09 14:53:38 +02:00
|
|
|
catch (IllegalArgumentException ex)
|
|
|
|
{
|
|
|
|
|
2011-07-22 14:25:12 +02:00
|
|
|
}
|
2011-10-09 14:53:38 +02:00
|
|
|
if (newMat == null)
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
sendMessage("Cannot change \""+fieldName+"\" set: \""+value.toUpperCase()+"\" is not a valid material.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-27 22:56:45 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2011-07-22 14:25:12 +02:00
|
|
|
Set<Material> matSet = (Set<Material>)target.get(null);
|
|
|
|
|
|
|
|
// Material already present, so remove it
|
2011-10-09 14:53:38 +02:00
|
|
|
if (matSet.contains(newMat))
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
matSet.remove(newMat);
|
|
|
|
target.set(null, matSet);
|
|
|
|
success = "\""+fieldName+"\" set: Material \""+value.toUpperCase()+"\" removed.";
|
|
|
|
}
|
|
|
|
// Material not present yet, add it
|
2011-10-09 14:53:38 +02:00
|
|
|
else
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
matSet.add(newMat);
|
|
|
|
target.set(null, matSet);
|
|
|
|
success = "\""+fieldName+"\" set: Material \""+value.toUpperCase()+"\" added.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set<String>
|
2011-10-09 14:53:38 +02:00
|
|
|
else if (innerType == String.class)
|
|
|
|
{
|
2011-07-27 22:56:45 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2011-07-22 14:25:12 +02:00
|
|
|
Set<String> stringSet = (Set<String>)target.get(null);
|
|
|
|
|
|
|
|
// String already present, so remove it
|
2011-10-09 14:53:38 +02:00
|
|
|
if (stringSet.contains(value))
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
stringSet.remove(value);
|
|
|
|
target.set(null, stringSet);
|
|
|
|
success = "\""+fieldName+"\" set: \""+value+"\" removed.";
|
|
|
|
}
|
|
|
|
// String not present yet, add it
|
2011-10-09 14:53:38 +02:00
|
|
|
else
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
stringSet.add(value);
|
|
|
|
target.set(null, stringSet);
|
|
|
|
success = "\""+fieldName+"\" set: \""+value+"\" added.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set of unknown type
|
2011-10-09 14:53:38 +02:00
|
|
|
else
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
sendMessage("\""+fieldName+"\" is not a data type set which can be modified with this command.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unknown type
|
2011-10-09 14:53:38 +02:00
|
|
|
else
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
sendMessage("\""+fieldName+"\" is not a data type which can be modified with this command.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-10-09 14:53:38 +02:00
|
|
|
catch (NoSuchFieldException ex)
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
sendMessage("Configuration setting \""+fieldName+"\" couldn't be matched, though it should be... please report this error.");
|
|
|
|
return;
|
|
|
|
}
|
2011-10-09 14:53:38 +02:00
|
|
|
catch (IllegalAccessException ex)
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
sendMessage("Error setting configuration setting \""+fieldName+"\" to \""+value+"\".");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-09 14:53:38 +02:00
|
|
|
if (!success.isEmpty())
|
|
|
|
{
|
2011-07-22 14:25:12 +02:00
|
|
|
sendMessage(success);
|
2011-10-09 14:53:38 +02:00
|
|
|
if (sender instanceof Player)
|
|
|
|
{
|
|
|
|
P.p.log(success + " Command was run by "+fme.getName()+".");
|
2011-07-22 14:25:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// save change to disk
|
|
|
|
Conf.save();
|
2011-08-20 03:36:23 +02:00
|
|
|
|
|
|
|
// in case some Spout related setting was changed
|
|
|
|
SpoutFeatures.updateAppearances();
|
2011-07-22 14:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|