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 ;
2019-09-14 21:13:01 +02:00
import com.massivecraft.factions.FactionsPlugin ;
2014-04-04 20:55:21 +02:00
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
2019-08-24 19:18:50 +02:00
private static HashMap < String , String > properFieldNames = new HashMap < > ( ) ;
public CmdConfig ( ) {
super ( ) ;
this . aliases . add ( " config " ) ;
this . requiredArgs . add ( " setting " ) ;
this . requiredArgs . add ( " value " ) ;
2019-09-14 21:13:01 +02:00
this . requirements = new CommandRequirements . Builder ( Permission . CONFIG )
. noErrorOnManyArgs ( )
. build ( ) ;
2019-08-24 19:18:50 +02:00
}
@Override
2019-09-14 21:13:01 +02:00
public void perform ( CommandContext context ) {
2019-08-24 19:18:50 +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
if ( properFieldNames . isEmpty ( ) ) {
Field [ ] fields = Conf . class . getDeclaredFields ( ) ;
for ( Field field : fields ) {
properFieldNames . put ( field . getName ( ) . toLowerCase ( ) , field . getName ( ) ) ;
}
}
2019-09-14 21:13:01 +02:00
String field = context . argAsString ( 0 ) . toLowerCase ( ) ;
2019-08-24 19:18:50 +02:00
if ( field . startsWith ( " \" " ) & & field . endsWith ( " \" " ) ) {
field = field . substring ( 1 , field . length ( ) - 1 ) ;
}
String fieldName = properFieldNames . get ( field ) ;
if ( fieldName = = null | | fieldName . isEmpty ( ) ) {
2019-09-14 21:13:01 +02:00
context . msg ( TL . COMMAND_CONFIG_NOEXIST , field ) ;
2019-08-24 19:18:50 +02:00
return ;
}
String success ;
2019-09-14 21:13:01 +02:00
StringBuilder value = new StringBuilder ( context . args . get ( 1 ) ) ;
for ( int i = 2 ; i < context . args . size ( ) ; i + + ) {
value . append ( ' ' ) . append ( context . args . get ( i ) ) ;
2019-08-24 19:18:50 +02:00
}
try {
Field target = Conf . class . getField ( fieldName ) ;
// boolean
if ( target . getType ( ) = = boolean . class ) {
2019-09-14 21:13:01 +02:00
boolean targetValue = context . strAsBool ( value . toString ( ) ) ;
2019-08-24 19:18:50 +02:00
target . setBoolean ( null , targetValue ) ;
if ( targetValue ) {
success = " \" " + fieldName + TL . COMMAND_CONFIG_SET_TRUE . toString ( ) ;
} else {
success = " \" " + fieldName + TL . COMMAND_CONFIG_SET_FALSE . toString ( ) ;
}
}
// int
else if ( target . getType ( ) = = int . class ) {
try {
int intVal = Integer . parseInt ( value . toString ( ) ) ;
target . setInt ( null , intVal ) ;
success = " \" " + fieldName + TL . COMMAND_CONFIG_OPTIONSET . toString ( ) + intVal + " . " ;
} catch ( NumberFormatException ex ) {
2019-09-14 21:13:01 +02:00
context . sendMessage ( TL . COMMAND_CONFIG_INTREQUIRED . format ( fieldName ) ) ;
2019-08-24 19:18:50 +02:00
return ;
}
}
// long
else if ( target . getType ( ) = = long . class ) {
try {
long longVal = Long . parseLong ( value . toString ( ) ) ;
target . setLong ( null , longVal ) ;
success = " \" " + fieldName + TL . COMMAND_CONFIG_OPTIONSET . toString ( ) + longVal + " . " ;
} catch ( NumberFormatException ex ) {
2019-09-14 21:13:01 +02:00
context . sendMessage ( TL . COMMAND_CONFIG_LONGREQUIRED . format ( fieldName ) ) ;
2019-08-24 19:18:50 +02:00
return ;
}
}
// double
else if ( target . getType ( ) = = double . class ) {
try {
double doubleVal = Double . parseDouble ( value . toString ( ) ) ;
target . setDouble ( null , doubleVal ) ;
success = " \" " + fieldName + TL . COMMAND_CONFIG_OPTIONSET . toString ( ) + doubleVal + " . " ;
} catch ( NumberFormatException ex ) {
2019-09-14 21:13:01 +02:00
context . sendMessage ( TL . COMMAND_CONFIG_DOUBLEREQUIRED . format ( fieldName ) ) ;
2019-08-24 19:18:50 +02:00
return ;
}
}
// float
else if ( target . getType ( ) = = float . class ) {
try {
float floatVal = Float . parseFloat ( value . toString ( ) ) ;
target . setFloat ( null , floatVal ) ;
success = " \" " + fieldName + TL . COMMAND_CONFIG_OPTIONSET . toString ( ) + floatVal + " . " ;
} catch ( NumberFormatException ex ) {
2019-09-14 21:13:01 +02:00
context . sendMessage ( TL . COMMAND_CONFIG_FLOATREQUIRED . format ( fieldName ) ) ;
2019-08-24 19:18:50 +02:00
return ;
}
}
// String
else if ( target . getType ( ) = = String . class ) {
target . set ( null , value . toString ( ) ) ;
success = " \" " + fieldName + TL . COMMAND_CONFIG_OPTIONSET . toString ( ) + value + " \" . " ;
}
// ChatColor
else if ( target . getType ( ) = = ChatColor . class ) {
ChatColor newColor = null ;
try {
newColor = ChatColor . valueOf ( value . toString ( ) . toUpperCase ( ) ) ;
} catch ( IllegalArgumentException ex ) {
}
if ( newColor = = null ) {
2019-09-14 21:13:01 +02:00
context . sendMessage ( TL . COMMAND_CONFIG_INVALID_COLOUR . format ( fieldName , value . toString ( ) . toUpperCase ( ) ) ) ;
2019-08-24 19:18:50 +02:00
return ;
}
target . set ( null , newColor ) ;
success = " \" " + fieldName + TL . COMMAND_CONFIG_COLOURSET . toString ( ) + value . toString ( ) . 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 ) {
2019-09-14 21:13:01 +02:00
context . sendMessage ( TL . COMMAND_CONFIG_INVALID_COLLECTION . format ( fieldName ) ) ;
2019-08-24 19:18:50 +02:00
return ;
}
// Set<Material>
else if ( innerType = = Material . class ) {
Material newMat = null ;
try {
newMat = Material . valueOf ( value . toString ( ) . toUpperCase ( ) ) ;
} catch ( IllegalArgumentException ex ) {
}
if ( newMat = = null ) {
2019-09-14 21:13:01 +02:00
context . sendMessage ( TL . COMMAND_CONFIG_INVALID_MATERIAL . format ( fieldName , value . toString ( ) . toUpperCase ( ) ) ) ;
2019-08-24 19:18:50 +02:00
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 = TL . COMMAND_CONFIG_MATERIAL_REMOVED . format ( fieldName , value . toString ( ) . toUpperCase ( ) ) ;
}
// Material not present yet, add it
else {
matSet . add ( newMat ) ;
target . set ( null , matSet ) ;
success = TL . COMMAND_CONFIG_MATERIAL_ADDED . format ( fieldName , value . toString ( ) . toUpperCase ( ) ) ;
}
}
// Set<String>
else if ( innerType = = String . class ) {
@SuppressWarnings ( " unchecked " ) Set < String > stringSet = ( Set < String > ) target . get ( null ) ;
// String already present, so remove it
if ( stringSet . contains ( value . toString ( ) ) ) {
stringSet . remove ( value . toString ( ) ) ;
target . set ( null , stringSet ) ;
success = TL . COMMAND_CONFIG_SET_REMOVED . format ( fieldName , value . toString ( ) ) ;
}
// String not present yet, add it
else {
stringSet . add ( value . toString ( ) ) ;
target . set ( null , stringSet ) ;
success = TL . COMMAND_CONFIG_SET_ADDED . format ( fieldName , value . toString ( ) ) ;
}
}
// Set of unknown type
else {
2019-09-14 21:13:01 +02:00
context . sendMessage ( TL . COMMAND_CONFIG_INVALID_TYPESET . format ( fieldName ) ) ;
2019-08-24 19:18:50 +02:00
return ;
}
}
// unknown type
else {
2019-09-14 21:13:01 +02:00
context . sendMessage ( TL . COMMAND_CONFIG_ERROR_TYPE . format ( fieldName , target . getClass ( ) . getName ( ) ) ) ;
2019-08-24 19:18:50 +02:00
return ;
}
} catch ( NoSuchFieldException ex ) {
2019-09-14 21:13:01 +02:00
context . sendMessage ( TL . COMMAND_CONFIG_ERROR_MATCHING . format ( fieldName ) ) ;
2019-08-24 19:18:50 +02:00
return ;
} catch ( IllegalAccessException ex ) {
2019-09-14 21:13:01 +02:00
context . sendMessage ( TL . COMMAND_CONFIG_ERROR_SETTING . format ( fieldName , value . toString ( ) ) ) ;
2019-08-24 19:18:50 +02:00
return ;
}
if ( ! success . isEmpty ( ) ) {
2019-09-14 21:13:01 +02:00
if ( context . sender instanceof Player ) {
context . sendMessage ( success ) ;
FactionsPlugin . getInstance ( ) . log ( success + TL . COMMAND_CONFIG_LOG . format ( ( Player ) context . sender ) ) ;
} else // using FactionsPlugin.getInstance().log() instead of sendMessage if run from server console so that "[Factions v#.#.#]" is prepended in server log
2019-08-24 19:18:50 +02:00
{
2019-09-14 21:13:01 +02:00
FactionsPlugin . getInstance ( ) . log ( success ) ;
2019-08-24 19:18:50 +02:00
}
}
// save change to disk
Conf . save ( ) ;
}
@Override
public TL getUsageTranslation ( ) {
return TL . COMMAND_CONFIG_DESCRIPTION ;
}
2015-01-22 00:58:33 +01:00
2011-07-22 14:25:12 +02:00
}