= Base commands config class, and command standardization
This commit is contained in:
+43
-44
@@ -1,50 +1,49 @@
|
|||||||
[CommandEnabler]
|
[CommandEnabler]
|
||||||
indicator=1
|
|
||||||
boop=1
|
|
||||||
tony, stark, dontfeelgood, dontfeelsogood=1
|
|
||||||
yeet=1
|
|
||||||
role=1
|
|
||||||
ping=1
|
|
||||||
rating=1
|
|
||||||
roll=1
|
|
||||||
blur=1
|
|
||||||
choose=1
|
|
||||||
poll=1
|
|
||||||
rpstart=1
|
|
||||||
bet=1
|
|
||||||
perish, thenperish=1
|
|
||||||
teey=1
|
|
||||||
stats=1
|
|
||||||
beans=1
|
beans=1
|
||||||
eightball, 8ball=1
|
benchmark, bench=1
|
||||||
vote=1
|
bet=1
|
||||||
results=1
|
bethistory=1
|
||||||
wolfram=1
|
blur=1
|
||||||
map=1
|
boop=1
|
||||||
info, about=1
|
buildhelp=1
|
||||||
givebeans=1
|
buildhelp=1
|
||||||
c++, g++, cplus, cpp=1
|
c++, g++, cplus, cpp=1
|
||||||
work=1
|
catch=1
|
||||||
showpoll=1
|
choose=1
|
||||||
rpend=1
|
crouton=1
|
||||||
rpg=1
|
dbflush=1
|
||||||
tweet=1
|
dbstats=1
|
||||||
java, jdoodle=1
|
eightball, 8ball=1
|
||||||
help=1
|
fetch=1
|
||||||
buildHelp=1
|
givebeans=1
|
||||||
invite=1
|
|
||||||
shutdown=1
|
|
||||||
addguildrole=1
|
|
||||||
allowedguildrole=1
|
|
||||||
guildroleremove=1
|
|
||||||
guildroleadd=1
|
guildroleadd=1
|
||||||
guildroleallowed=1
|
guildroleallowed=1
|
||||||
fetch=1
|
|
||||||
guildrolenotallowed=1
|
|
||||||
catch=1
|
|
||||||
guildrolelist=1
|
guildrolelist=1
|
||||||
benchmark, bench=1
|
guildrolenotallowed=1
|
||||||
bethistory=1
|
guildroleremove=1
|
||||||
crouton=1
|
help=1
|
||||||
dbstats=1
|
indicator=1
|
||||||
dbflush=1
|
info, about=1
|
||||||
|
invite=1
|
||||||
|
java, jdoodle=1
|
||||||
|
map=1
|
||||||
|
perish, thenperish=1
|
||||||
|
ping=1
|
||||||
|
poll=1
|
||||||
|
rating=1
|
||||||
|
results=1
|
||||||
|
role=1
|
||||||
|
roll=1
|
||||||
|
rpend=1
|
||||||
|
rpg=1
|
||||||
|
rpstart=1
|
||||||
|
showpoll=1
|
||||||
|
shutdown=1
|
||||||
|
stats=1
|
||||||
|
teey=1
|
||||||
|
tony, stark, dontfeelgood, dontfeelsogood=1
|
||||||
|
tweet=1
|
||||||
|
vote=1
|
||||||
|
wolfram=1
|
||||||
|
work=1
|
||||||
|
yeet=1
|
||||||
|
|||||||
@@ -1,6 +1,90 @@
|
|||||||
package core;
|
package core;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ListIterator;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import dataStructures.Pair;
|
||||||
|
import utils.GlobalLog;
|
||||||
|
import utils.LogFilter;
|
||||||
|
import utils.io.FileUtils;
|
||||||
|
|
||||||
public class BaseKeyValueFile
|
public class BaseKeyValueFile
|
||||||
{
|
{
|
||||||
|
// Variables
|
||||||
|
public static final String headerStart = "[";
|
||||||
|
public static final String headerEnd = "]";
|
||||||
|
public static final String pairSplit = "=";
|
||||||
|
public static final char pairSeparator = '\n';
|
||||||
|
|
||||||
|
protected final String filename;
|
||||||
|
protected final String header;
|
||||||
|
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
public BaseKeyValueFile(String filename)
|
||||||
|
{
|
||||||
|
this.filename = filename;
|
||||||
|
this.header = headerStart + this.getClass().getSimpleName() + headerEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reads in and calls the specifid function for each keyvalue pair we find
|
||||||
|
protected void Parse(Consumer<? super Pair<String, String>> keyValueCallback)
|
||||||
|
{
|
||||||
|
File f = new File(filename);
|
||||||
|
if(f.isFile() && f.canRead())
|
||||||
|
{
|
||||||
|
String content = FileUtils.ReadContent(f).trim();
|
||||||
|
String[] lines = content.split("" + pairSeparator);
|
||||||
|
|
||||||
|
for(int i = 0; i < lines.length; ++i)
|
||||||
|
{
|
||||||
|
if(lines[i].contains(header))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
String[] pair = lines[i].split(pairSplit);
|
||||||
|
|
||||||
|
if(pair.length < 2)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
String key = pair[0].trim().toLowerCase();
|
||||||
|
String value = pair[1].trim().toLowerCase();
|
||||||
|
|
||||||
|
keyValueCallback.accept(new Pair<String, String>(key, value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Writes out a set of keyvalue pairs
|
||||||
|
protected void Write(List<Pair<String, String>> toWrite)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ListIterator<Pair<String, String>> iter = toWrite.listIterator();
|
||||||
|
|
||||||
|
String outString = "";
|
||||||
|
outString += header + pairSeparator;
|
||||||
|
|
||||||
|
while(iter.hasNext())
|
||||||
|
{
|
||||||
|
Pair<String, String> pair = iter.next();
|
||||||
|
String key = pair.First.toLowerCase();
|
||||||
|
String value = pair.Second.toLowerCase();
|
||||||
|
|
||||||
|
outString += key + pairSplit + value + pairSeparator;
|
||||||
|
}
|
||||||
|
|
||||||
|
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
|
||||||
|
writer.write(outString);
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
GlobalLog.Error(LogFilter.Core, "Issue writing file " + filename + ": " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ import java.nio.file.Path;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import dataStructures.TaggedPairStore;
|
import dataStructures.TaggedPairStore;
|
||||||
import utils.FileUtils;
|
|
||||||
import utils.GlobalLog;
|
import utils.GlobalLog;
|
||||||
import utils.LogFilter;
|
import utils.LogFilter;
|
||||||
import utils.io.FileMonitor;
|
import utils.io.FileMonitor;
|
||||||
|
import utils.io.FileUtils;
|
||||||
import utils.io.MonitoredFile;
|
import utils.io.MonitoredFile;
|
||||||
|
|
||||||
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
|
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
|
||||||
@@ -37,7 +37,7 @@ public abstract class BaseLocFile
|
|||||||
// File monitoring
|
// File monitoring
|
||||||
protected FileMonitor fileMonitor;
|
protected FileMonitor fileMonitor;
|
||||||
|
|
||||||
// Ok... so this is an array because if it's not an array, the parser will parse the string
|
// Constructor
|
||||||
public BaseLocFile(String filename, String functionName)
|
public BaseLocFile(String filename, String functionName)
|
||||||
{
|
{
|
||||||
this.filename = filename;
|
this.filename = filename;
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
package core;
|
package core;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import utils.FileUtils;
|
import java.util.Vector;
|
||||||
|
import dataStructures.Pair;
|
||||||
import utils.GlobalLog;
|
import utils.GlobalLog;
|
||||||
import utils.LogFilter;
|
import utils.LogFilter;
|
||||||
|
|
||||||
@@ -18,11 +16,6 @@ import utils.LogFilter;
|
|||||||
public class CommandEnabler extends BaseKeyValueFile
|
public class CommandEnabler extends BaseKeyValueFile
|
||||||
{
|
{
|
||||||
// Config/const variables
|
// Config/const variables
|
||||||
public static final String headerStart = "[";
|
|
||||||
public static final String headerEnd = "]";
|
|
||||||
public static final String filename = "commands.config";
|
|
||||||
public static final String pairSplit = "=";
|
|
||||||
public static final char pairSeparator = '\n';
|
|
||||||
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;
|
||||||
@@ -30,15 +23,16 @@ public class CommandEnabler extends BaseKeyValueFile
|
|||||||
// 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 ArrayList<String> keyList; // Tracking ordering for later
|
||||||
private final String header;
|
private final static String name = "commands.config";
|
||||||
|
|
||||||
public CommandEnabler()
|
public CommandEnabler()
|
||||||
{
|
{
|
||||||
|
super(name);
|
||||||
|
|
||||||
// Create/Init variables
|
// Create/Init variables
|
||||||
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<>();
|
||||||
header = headerStart + this.getClass().getSimpleName() + headerEnd;
|
|
||||||
|
|
||||||
// Startup
|
// Startup
|
||||||
ReadIn();
|
ReadIn();
|
||||||
@@ -49,24 +43,9 @@ public class CommandEnabler extends BaseKeyValueFile
|
|||||||
// Reads in the config file and parses it, keeping tabs on the order it read things
|
// Reads in the config file and parses it, keeping tabs on the order it read things
|
||||||
private void ReadIn()
|
private void ReadIn()
|
||||||
{
|
{
|
||||||
File f = new File(filename);
|
Parse((pair) ->{
|
||||||
if(f.isFile() && f.canRead())
|
String key = pair.First;
|
||||||
{
|
String value = pair.Second;
|
||||||
String content = FileUtils.ReadContent(f).trim();
|
|
||||||
String[] lines = content.split("" + pairSeparator);
|
|
||||||
|
|
||||||
for(int i = 0; i < lines.length; ++i)
|
|
||||||
{
|
|
||||||
if(lines[i].contains(header))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
String[] pair = lines[i].split(pairSplit);
|
|
||||||
|
|
||||||
if(pair.length < 2)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
String key = pair[0].trim();
|
|
||||||
String value = pair[1].trim().toLowerCase();
|
|
||||||
|
|
||||||
keyList.add(key);
|
keyList.add(key);
|
||||||
|
|
||||||
@@ -74,8 +53,7 @@ public class CommandEnabler extends BaseKeyValueFile
|
|||||||
enabledMap.putIfAbsent(key, true);
|
enabledMap.putIfAbsent(key, true);
|
||||||
else
|
else
|
||||||
enabledMap.putIfAbsent(key, false);
|
enabledMap.putIfAbsent(key, false);
|
||||||
}
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Look up the already scraped values from the localizer and store them if they
|
// Look up the already scraped values from the localizer and store them if they
|
||||||
@@ -98,36 +76,31 @@ public class CommandEnabler extends BaseKeyValueFile
|
|||||||
// Write out enabled/disabled file info.
|
// Write out enabled/disabled file info.
|
||||||
private void WriteOut()
|
private void WriteOut()
|
||||||
{
|
{
|
||||||
try
|
List<Pair<String, String>> list = new Vector<Pair<String, String>>();
|
||||||
{
|
|
||||||
String outString = "";
|
|
||||||
outString += header + pairSeparator;
|
|
||||||
for(int i = 0; i < keyList.size(); ++i)
|
for(int i = 0; i < keyList.size(); ++i)
|
||||||
{
|
{
|
||||||
String key = keyList.get(i);
|
String key = keyList.get(i).toLowerCase();
|
||||||
String value = enabled;
|
String value = enabled.toLowerCase();
|
||||||
|
|
||||||
if(enabledMap.get(key) == false)
|
if(enabledMap.get(key) == false)
|
||||||
value = disabled;
|
value = disabled.toLowerCase();
|
||||||
|
|
||||||
outString += key + pairSplit + value + pairSeparator;
|
list.add(new Pair<String, String>(key, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
|
Collections.sort(list, (c1, c2) -> { return c1.First.compareTo(c2.First); });
|
||||||
writer.write(outString);
|
|
||||||
writer.close();
|
Write(list);
|
||||||
}
|
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
GlobalLog.Error(LogFilter.Core, "Command enabler issue writing file! " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Looks up a key to see if it's enabled or not
|
// Looks up a key to see if it's enabled or not
|
||||||
public boolean IsEnabled(String key)
|
public boolean IsEnabled(String key)
|
||||||
{
|
{
|
||||||
if(enabledMap.containsKey(key))
|
String toCheck = key.toLowerCase();
|
||||||
return enabledMap.get(key);
|
|
||||||
|
if(enabledMap.containsKey(toCheck))
|
||||||
|
return enabledMap.get(toCheck);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import dataStructures.Pair;
|
import dataStructures.Pair;
|
||||||
import utils.FileUtils;
|
|
||||||
import utils.io.DirectoryMonitor;
|
import utils.io.DirectoryMonitor;
|
||||||
|
import utils.io.FileUtils;
|
||||||
import utils.io.MonitoredFile;
|
import utils.io.MonitoredFile;
|
||||||
|
|
||||||
// All things considered, this doesn't need to be particularly efficient since anything
|
// All things considered, this doesn't need to be particularly efficient since anything
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import java.text.DateFormat;
|
|||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import utils.io.FileUtils;
|
||||||
|
|
||||||
public class GlobalLog
|
public class GlobalLog
|
||||||
{
|
{
|
||||||
private static final String directory = "logs/";
|
private static final String directory = "logs/";
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ import java.util.List;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import utils.FileUtils;
|
|
||||||
|
|
||||||
// NOTE: Consider shifting internal behavior to https://docs.oracle.com/javase/tutorial/essential/io/notification.html
|
// NOTE: Consider shifting internal behavior to https://docs.oracle.com/javase/tutorial/essential/io/notification.html
|
||||||
// for external stability and support
|
// for external stability and support
|
||||||
public class DirectoryMonitor
|
public class DirectoryMonitor
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ package utils.io;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import utils.FileUtils;
|
|
||||||
|
|
||||||
// Monitors a single file for changes - the file must exist and is expected to continue to exist.
|
// Monitors a single file for changes - the file must exist and is expected to continue to exist.
|
||||||
public class FileMonitor
|
public class FileMonitor
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils;
|
package utils.io;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -9,6 +9,9 @@ import java.nio.file.Paths;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import utils.GlobalLog;
|
||||||
|
import utils.LogFilter;
|
||||||
|
|
||||||
public class FileUtils
|
public class FileUtils
|
||||||
{
|
{
|
||||||
public static void CreateDirectoryIfDoesntExist(String directoryName)
|
public static void CreateDirectoryIfDoesntExist(String directoryName)
|
||||||
Reference in New Issue
Block a user