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;
|
2012-03-11 13:33:47 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Map;
|
2011-07-22 14:25:12 +02:00
|
|
|
|
|
|
|
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;
|
2012-03-11 13:33:47 +01:00
|
|
|
import com.massivecraft.factions.struct.FFlag;
|
|
|
|
import com.massivecraft.factions.struct.FPerm;
|
2011-10-09 14:53:38 +02:00
|
|
|
import com.massivecraft.factions.struct.Permission;
|
2012-03-11 13:33:47 +01:00
|
|
|
import com.massivecraft.factions.struct.Rel;
|
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");
|
2012-01-19 05:24:12 +01:00
|
|
|
this.errorOnToManyArgs = false;
|
2011-10-09 14:53:38 +02:00
|
|
|
|
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;
|
2011-10-23 17:55:53 +02:00
|
|
|
senderMustBeOfficer = false;
|
|
|
|
senderMustBeLeader = 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())
|
|
|
|
{
|
2011-10-10 13:40:24 +02:00
|
|
|
msg("<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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-13 21:09:47 +02:00
|
|
|
// float
|
|
|
|
else if (target.getType() == float.class)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
float floatVal = Float.parseFloat(value);
|
|
|
|
target.setFloat(null, floatVal);
|
|
|
|
success = "\""+fieldName+"\" option set to "+floatVal+".";
|
|
|
|
}
|
|
|
|
catch(NumberFormatException ex)
|
|
|
|
{
|
|
|
|
sendMessage("Cannot set \""+fieldName+"\": float (numeric) value required.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-22 14:25:12 +02:00
|
|
|
// 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];
|
|
|
|
|
2012-03-11 13:33:47 +01:00
|
|
|
// Set<?>
|
|
|
|
if (targSet.getRawType() == Set.class)
|
2011-10-09 14:53:38 +02:00
|
|
|
{
|
2012-03-11 13:33:47 +01:00
|
|
|
// Set<Material>
|
|
|
|
if (innerType == Material.class)
|
2011-10-09 14:53:38 +02:00
|
|
|
{
|
2012-03-11 13:33:47 +01:00
|
|
|
Material newMat = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
newMat = Material.valueOf(value.toUpperCase());
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
if (newMat == null)
|
|
|
|
{
|
|
|
|
sendMessage("Cannot change \""+fieldName+"\" set: \""+value.toUpperCase()+"\" is not a valid material.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
Set<Material> matSet = (Set<Material>)target.get(null);
|
|
|
|
|
|
|
|
// Material already present, so remove it
|
|
|
|
if (matSet.contains(newMat))
|
|
|
|
{
|
|
|
|
matSet.remove(newMat);
|
|
|
|
target.set(null, matSet);
|
|
|
|
success = "\""+fieldName+"\" set: Material \""+value.toUpperCase()+"\" removed.";
|
|
|
|
}
|
|
|
|
// Material not present yet, add it
|
|
|
|
else
|
|
|
|
{
|
|
|
|
matSet.add(newMat);
|
|
|
|
target.set(null, matSet);
|
|
|
|
success = "\""+fieldName+"\" set: Material \""+value.toUpperCase()+"\" added.";
|
|
|
|
}
|
2011-07-22 14:25:12 +02:00
|
|
|
}
|
2012-03-11 13:33:47 +01:00
|
|
|
|
|
|
|
// Set<String>
|
|
|
|
else if (innerType == String.class)
|
2011-10-09 14:53:38 +02:00
|
|
|
{
|
2012-03-11 13:33:47 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
Set<String> stringSet = (Set<String>)target.get(null);
|
|
|
|
|
|
|
|
// String already present, so remove it
|
|
|
|
if (stringSet.contains(value))
|
|
|
|
{
|
|
|
|
stringSet.remove(value);
|
|
|
|
success = "\""+fieldName+"\" set: \""+value+"\" removed.";
|
|
|
|
}
|
|
|
|
// String not present yet, add it
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stringSet.add(value);
|
|
|
|
success = "\""+fieldName+"\" set: \""+value+"\" added.";
|
|
|
|
}
|
|
|
|
target.set(null, stringSet);
|
2011-07-22 14:25:12 +02:00
|
|
|
}
|
2012-03-11 13:33:47 +01:00
|
|
|
|
|
|
|
// Set of unknown type
|
|
|
|
else
|
2011-10-09 14:53:38 +02:00
|
|
|
{
|
2012-03-11 13:33:47 +01:00
|
|
|
sendMessage("\""+fieldName+"\" is not a data type set which can be modified with this command.");
|
2011-07-22 14:25:12 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-03-11 13:33:47 +01:00
|
|
|
}
|
2011-07-22 14:25:12 +02:00
|
|
|
|
2012-03-11 13:33:47 +01:00
|
|
|
// Map<?, ?>
|
|
|
|
else if (targSet.getRawType() == Map.class)
|
|
|
|
{
|
|
|
|
if (args.size() < 3)
|
2011-10-09 14:53:38 +02:00
|
|
|
{
|
2012-03-11 13:33:47 +01:00
|
|
|
sendMessage("Cannot change \""+fieldName+"\" map: not enough arguments passed.");
|
|
|
|
return;
|
2011-07-22 14:25:12 +02:00
|
|
|
}
|
2012-03-11 13:33:47 +01:00
|
|
|
Type innerType2 = targSet.getActualTypeArguments()[1];
|
|
|
|
String value1 = args.get(1);
|
|
|
|
String value2 = value.substring(value1.length() + 1);
|
|
|
|
|
|
|
|
// Map<FFlag, Boolean>
|
|
|
|
if (innerType == FFlag.class && innerType2 == Boolean.class)
|
2011-10-09 14:53:38 +02:00
|
|
|
{
|
2012-03-11 13:33:47 +01:00
|
|
|
value1 = value1.toUpperCase();
|
|
|
|
FFlag newFlag = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
newFlag = FFlag.valueOf(value1);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex) {}
|
2011-07-22 14:25:12 +02:00
|
|
|
|
2012-03-11 13:33:47 +01:00
|
|
|
if (newFlag == null)
|
|
|
|
{
|
|
|
|
sendMessage("Cannot change \""+fieldName+"\" map: \""+value1+"\" is not a valid FFlag.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
Map<FFlag, Boolean> map = (Map<FFlag, Boolean>)target.get(null);
|
|
|
|
|
|
|
|
Boolean targetValue = this.strAsBool(value2);
|
|
|
|
|
|
|
|
map.put(newFlag, targetValue);
|
|
|
|
target.set(null, map);
|
2011-07-22 14:25:12 +02:00
|
|
|
|
2012-03-11 13:33:47 +01:00
|
|
|
if (targetValue)
|
|
|
|
success = "\""+fieldName+"\" flag \""+value1+"\" set to true (enabled).";
|
|
|
|
else
|
|
|
|
success = "\""+fieldName+"\" flag \""+value1+"\" set to false (disabled).";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map<FPerm, Set<Rel>>
|
|
|
|
else if (innerType == FPerm.class && innerType2 instanceof ParameterizedType)
|
2011-10-09 14:53:38 +02:00
|
|
|
{
|
2012-03-11 13:33:47 +01:00
|
|
|
if (((ParameterizedType)innerType2).getRawType() != Set.class)
|
|
|
|
{
|
|
|
|
sendMessage("\""+fieldName+"\" is not a data type map which can be modified with this command, due to the inner collection type.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
value1 = value1.toUpperCase();
|
|
|
|
value2 = value2.toUpperCase();
|
|
|
|
|
|
|
|
FPerm newPerm = null;
|
|
|
|
Rel newRel = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
newPerm = FPerm.valueOf(value1);
|
|
|
|
newRel = Rel.valueOf(value2);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex) {}
|
|
|
|
|
|
|
|
if (newPerm == null)
|
|
|
|
{
|
|
|
|
sendMessage("Cannot change \""+fieldName+"\" map: \""+value1+"\" is not a valid FPerm.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (newRel == null)
|
|
|
|
{
|
|
|
|
sendMessage("Cannot change \""+fieldName+"\" map: \""+value2+"\" is not a valid Rel.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
Map<FPerm, Set<Rel>> map = (Map<FPerm, Set<Rel>>)target.get(null);
|
|
|
|
|
|
|
|
Set<Rel> relSet = map.get(newPerm);
|
|
|
|
if (relSet == null)
|
|
|
|
relSet = new HashSet<Rel>();
|
|
|
|
|
|
|
|
// Rel already present, so remove it
|
|
|
|
if (relSet.contains(newRel))
|
|
|
|
{
|
|
|
|
relSet.remove(newRel);
|
|
|
|
success = "\""+fieldName+"\" permission \""+value1+"\": relation \""+value2+"\" removed.";
|
|
|
|
}
|
|
|
|
// Rel not present yet, add it
|
|
|
|
else
|
|
|
|
{
|
|
|
|
relSet.add(newRel);
|
|
|
|
success = "\""+fieldName+"\" permission \""+value1+"\": relation \""+value2+"\" added.";
|
|
|
|
}
|
|
|
|
|
|
|
|
map.put(newPerm, relSet);
|
|
|
|
target.set(null, map);
|
2011-07-22 14:25:12 +02:00
|
|
|
}
|
2012-03-11 13:33:47 +01:00
|
|
|
|
|
|
|
// Map of unknown type
|
|
|
|
else
|
2011-10-09 14:53:38 +02:00
|
|
|
{
|
2012-03-11 13:33:47 +01:00
|
|
|
sendMessage("\""+fieldName+"\" is not a data type map which can be modified with this command.");
|
|
|
|
return;
|
2011-07-22 14:25:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-11 13:33:47 +01:00
|
|
|
// not a Set or Map?
|
2011-10-09 14:53:38 +02:00
|
|
|
else
|
|
|
|
{
|
2012-03-11 13:33:47 +01:00
|
|
|
sendMessage("\""+fieldName+"\" is not a data collection type which can be modified with this command.");
|
2011-07-22 14:25:12 +02:00
|
|
|
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
|
2012-05-09 03:24:07 +02:00
|
|
|
SpoutFeatures.updateTitle(null, null);
|
|
|
|
//SpoutFeatures.updateCape(null);
|
2011-07-22 14:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|