From a888a9c5b55449f6a6d2dba2a148de6bc36c7ea6 Mon Sep 17 00:00:00 2001 From: Brettflan Date: Sun, 11 Mar 2012 07:33:47 -0500 Subject: [PATCH] Updated /f config to support modifying "factionFlagDefaults" and "factionPermDefaults". Both of these settings require an additional argument more than other settings. Examples: /f config factionFlagDefaults firespread false - set default "FIRESPREAD" faction flag to false /f config factionFlagDefaults monsters true - set default "MONSTERS" faction flag to true /f config factionPermDefaults build ally - for default "BUILD" faction permission, add/remove permission for allies /f config factionPermDefaults withdraw member - for default "WITHDRAW" faction permission, add/remove permission for regular faction members --- .../massivecraft/factions/cmd/CmdConfig.java | 215 ++++++++++++++---- 1 file changed, 165 insertions(+), 50 deletions(-) diff --git a/src/com/massivecraft/factions/cmd/CmdConfig.java b/src/com/massivecraft/factions/cmd/CmdConfig.java index f72366dd..5d180bbf 100644 --- a/src/com/massivecraft/factions/cmd/CmdConfig.java +++ b/src/com/massivecraft/factions/cmd/CmdConfig.java @@ -5,6 +5,8 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.Set; import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -13,7 +15,10 @@ import org.bukkit.entity.Player; import com.massivecraft.factions.Conf; import com.massivecraft.factions.P; import com.massivecraft.factions.integration.SpoutFeatures; +import com.massivecraft.factions.struct.FFlag; +import com.massivecraft.factions.struct.FPerm; import com.massivecraft.factions.struct.Permission; +import com.massivecraft.factions.struct.Rel; public class CmdConfig extends FCommand { @@ -174,76 +179,186 @@ public class CmdConfig extends FCommand 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) + // Set + if (targSet.getRawType() == Set.class) { - sendMessage("\""+fieldName+"\" is not a data collection type which can be modified with this command."); - return; - } + // Set + if (innerType == Material.class) + { + Material newMat = null; + try + { + newMat = Material.valueOf(value.toUpperCase()); + } + catch (IllegalArgumentException ex) + { - // Set - else if (innerType == Material.class) - { - 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; + } + if (newMat == null) + { + sendMessage("Cannot change \""+fieldName+"\" set: \""+value.toUpperCase()+"\" is not a valid material."); + return; + } + + @SuppressWarnings("unchecked") + Set matSet = (Set)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."; + } } - @SuppressWarnings("unchecked") - Set matSet = (Set)target.get(null); - - // Material already present, so remove it - if (matSet.contains(newMat)) + // Set + else if (innerType == String.class) { - matSet.remove(newMat); - target.set(null, matSet); - success = "\""+fieldName+"\" set: Material \""+value.toUpperCase()+"\" removed."; + @SuppressWarnings("unchecked") + Set stringSet = (Set)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); } - // Material not present yet, add it + + // Set of unknown type else { - matSet.add(newMat); - target.set(null, matSet); - success = "\""+fieldName+"\" set: Material \""+value.toUpperCase()+"\" added."; + sendMessage("\""+fieldName+"\" is not a data type set which can be modified with this command."); + return; } } - // Set - else if (innerType == String.class) + // Map + else if (targSet.getRawType() == Map.class) { - @SuppressWarnings("unchecked") - Set stringSet = (Set)target.get(null); - - // String already present, so remove it - if (stringSet.contains(value)) + if (args.size() < 3) { - stringSet.remove(value); - target.set(null, stringSet); - success = "\""+fieldName+"\" set: \""+value+"\" removed."; + sendMessage("Cannot change \""+fieldName+"\" map: not enough arguments passed."); + return; } - // String not present yet, add it - else + Type innerType2 = targSet.getActualTypeArguments()[1]; + String value1 = args.get(1); + String value2 = value.substring(value1.length() + 1); + + // Map + if (innerType == FFlag.class && innerType2 == Boolean.class) { - stringSet.add(value); - target.set(null, stringSet); - success = "\""+fieldName+"\" set: \""+value+"\" added."; + value1 = value1.toUpperCase(); + FFlag newFlag = null; + try + { + newFlag = FFlag.valueOf(value1); + } + catch (IllegalArgumentException ex) {} + + if (newFlag == null) + { + sendMessage("Cannot change \""+fieldName+"\" map: \""+value1+"\" is not a valid FFlag."); + return; + } + + @SuppressWarnings("unchecked") + Map map = (Map)target.get(null); + + Boolean targetValue = this.strAsBool(value2); + + map.put(newFlag, targetValue); + target.set(null, map); + + if (targetValue) + success = "\""+fieldName+"\" flag \""+value1+"\" set to true (enabled)."; + else + success = "\""+fieldName+"\" flag \""+value1+"\" set to false (disabled)."; + } + + // Map> + else if (innerType == FPerm.class && innerType2 instanceof ParameterizedType) + { + 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> map = (Map>)target.get(null); + + Set relSet = map.get(newPerm); + if (relSet == null) + relSet = new HashSet(); + + // 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); + } + + // Map of unknown type + else + { + sendMessage("\""+fieldName+"\" is not a data type map which can be modified with this command."); + return; } } - // Set of unknown type + // not a Set or Map? else { - sendMessage("\""+fieldName+"\" is not a data type set which can be modified with this command."); + sendMessage("\""+fieldName+"\" is not a data collection type which can be modified with this command."); return; } }