+ Added disabling and re-enabling commands live

This commit is contained in:
Matthew Cech
2019-06-29 23:11:34 -07:00
parent cfa8d7acee
commit 07613b3144
2 changed files with 29 additions and 13 deletions
+23 -7
View File
@@ -18,12 +18,15 @@ public class CommandEnabler implements IConfigSection
public static final String enabled = "1"; public static final String enabled = "1";
public static final String disabled = "0"; public static final String disabled = "0";
public static final boolean defaultEnabledState = true; public static final boolean defaultEnabledState = true;
public static final String commandSplit = ",";
public static final String HeaderName = "Command Enabler"; public static final String HeaderName = "Command Enabler";
// Local variables // Local variables
private HashMap<String, Boolean> enabledMap; // Quick lookup private HashMap<String, Boolean> enabledMap; // Quick lookup
private ArrayList<String> keyList; // Tracking ordering for later private HashMap<String, String> likeNames; // Single names mapped to multiple names for use in enabledMap.
private ArrayList<String> keyList; // Tracking ordering for later
// Instance
public static CommandEnabler instance; public static CommandEnabler instance;
// Constructor // Constructor
@@ -37,6 +40,7 @@ public class CommandEnabler implements IConfigSection
GlobalLog.log(LogFilter.Core, "Initializing " + this.getClass().getSimpleName()); GlobalLog.log(LogFilter.Core, "Initializing " + this.getClass().getSimpleName());
enabledMap = new HashMap<>(); enabledMap = new HashMap<>();
keyList = new ArrayList<>(); keyList = new ArrayList<>();
likeNames = new HashMap<String, String>();
instance = this; instance = this;
} }
@@ -57,8 +61,10 @@ public class CommandEnabler implements IConfigSection
String key = item.key; String key = item.key;
String value = item.value; String value = item.value;
// Keep track of the key itself
keyList.add(key); keyList.add(key);
// Write if it's enabled or disabled.
if(value.equalsIgnoreCase(enabled)) if(value.equalsIgnoreCase(enabled))
{ {
enabledMap.putIfAbsent(key, true); enabledMap.putIfAbsent(key, true);
@@ -67,6 +73,13 @@ public class CommandEnabler implements IConfigSection
{ {
enabledMap.putIfAbsent(key, false); enabledMap.putIfAbsent(key, false);
} }
// Keep track of alternate names
String[] keys = key.split(commandSplit);
for(int i = 0; i < keys.length; ++i)
{
likeNames.putIfAbsent(keys[i], key);
}
} }
} }
@@ -117,12 +130,15 @@ public class CommandEnabler implements IConfigSection
{ {
String toCheck = key.toLowerCase(); String toCheck = key.toLowerCase();
if(enabledMap.containsKey(toCheck)) // Look up to see if we have the key anywhere. If not, default to enabled.
{ // It should be noted that this is just checking for disabled commands, we don't
return enabledMap.get(toCheck); // determine if a key is actually a real command in here or not.
} String fullKey = likeNames.getOrDefault(toCheck, null);
if(fullKey == null)
return true;
return true; // If we have it, look it up. If we can't find it, default to true.
return enabledMap.getOrDefault(fullKey, true);
} }
@Override @Override
+6 -6
View File
@@ -36,12 +36,6 @@ public class CommandManager
if(pair == null || pair.Second == null) if(pair == null || pair.Second == null)
return; return;
// If we haven't already split a multisplit command (or even assessed that),
// then verify if we even need to register the commands at all. If it's
// not enabled, don't register it.
if(pair.First != null && !commandEnabler.isEnabled(pair.First))
return;
String key = pair.Second; String key = pair.Second;
if(key.contains(",")) if(key.contains(","))
@@ -80,6 +74,12 @@ public class CommandManager
if(input == null || !input.isValid()) if(input == null || !input.isValid())
return false; return false;
// If we haven't already split a multisplit command (or even assessed that),
// then verify if we even need to register the commands at all. If it's
// not enabled, don't register it.
if(input.key != null && !commandEnabler.isEnabled(input.key))
return false;
Command command = commands.get(input.key); Command command = commands.get(input.key);
if(command != null) if(command != null)
{ {