= Base commands config class, and command standardization
This commit is contained in:
+43
-44
@@ -1,50 +1,49 @@
|
||||
[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
|
||||
eightball, 8ball=1
|
||||
vote=1
|
||||
results=1
|
||||
wolfram=1
|
||||
map=1
|
||||
info, about=1
|
||||
givebeans=1
|
||||
benchmark, bench=1
|
||||
bet=1
|
||||
bethistory=1
|
||||
blur=1
|
||||
boop=1
|
||||
buildhelp=1
|
||||
buildhelp=1
|
||||
c++, g++, cplus, cpp=1
|
||||
work=1
|
||||
showpoll=1
|
||||
rpend=1
|
||||
rpg=1
|
||||
tweet=1
|
||||
java, jdoodle=1
|
||||
help=1
|
||||
buildHelp=1
|
||||
invite=1
|
||||
shutdown=1
|
||||
addguildrole=1
|
||||
allowedguildrole=1
|
||||
guildroleremove=1
|
||||
catch=1
|
||||
choose=1
|
||||
crouton=1
|
||||
dbflush=1
|
||||
dbstats=1
|
||||
eightball, 8ball=1
|
||||
fetch=1
|
||||
givebeans=1
|
||||
guildroleadd=1
|
||||
guildroleallowed=1
|
||||
fetch=1
|
||||
guildrolenotallowed=1
|
||||
catch=1
|
||||
guildrolelist=1
|
||||
benchmark, bench=1
|
||||
bethistory=1
|
||||
crouton=1
|
||||
dbstats=1
|
||||
dbflush=1
|
||||
guildrolenotallowed=1
|
||||
guildroleremove=1
|
||||
help=1
|
||||
indicator=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;
|
||||
|
||||
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
|
||||
{
|
||||
// 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.util.ArrayList;
|
||||
import dataStructures.TaggedPairStore;
|
||||
import utils.FileUtils;
|
||||
import utils.GlobalLog;
|
||||
import utils.LogFilter;
|
||||
import utils.io.FileMonitor;
|
||||
import utils.io.FileUtils;
|
||||
import utils.io.MonitoredFile;
|
||||
|
||||
// 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
|
||||
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)
|
||||
{
|
||||
this.filename = filename;
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
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.Collections;
|
||||
import java.util.HashMap;
|
||||
|
||||
import utils.FileUtils;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
import dataStructures.Pair;
|
||||
import utils.GlobalLog;
|
||||
import utils.LogFilter;
|
||||
|
||||
@@ -18,11 +16,6 @@ import utils.LogFilter;
|
||||
public class CommandEnabler extends BaseKeyValueFile
|
||||
{
|
||||
// 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 disabled = "0";
|
||||
public static final boolean defaultEnabledState = true;
|
||||
@@ -30,15 +23,16 @@ public class CommandEnabler extends BaseKeyValueFile
|
||||
// Local variables
|
||||
private HashMap<String, Boolean> enabledMap; // Quick lookup
|
||||
private ArrayList<String> keyList; // Tracking ordering for later
|
||||
private final String header;
|
||||
private final static String name = "commands.config";
|
||||
|
||||
public CommandEnabler()
|
||||
{
|
||||
super(name);
|
||||
|
||||
// Create/Init variables
|
||||
GlobalLog.Log(LogFilter.Core, "Initializing " + this.getClass().getSimpleName());
|
||||
enabledMap = new HashMap<>();
|
||||
keyList = new ArrayList<>();
|
||||
header = headerStart + this.getClass().getSimpleName() + headerEnd;
|
||||
|
||||
// Startup
|
||||
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
|
||||
private void ReadIn()
|
||||
{
|
||||
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();
|
||||
String value = pair[1].trim().toLowerCase();
|
||||
Parse((pair) ->{
|
||||
String key = pair.First;
|
||||
String value = pair.Second;
|
||||
|
||||
keyList.add(key);
|
||||
|
||||
@@ -74,8 +53,7 @@ public class CommandEnabler extends BaseKeyValueFile
|
||||
enabledMap.putIfAbsent(key, true);
|
||||
else
|
||||
enabledMap.putIfAbsent(key, false);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 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.
|
||||
private void WriteOut()
|
||||
{
|
||||
try
|
||||
{
|
||||
String outString = "";
|
||||
outString += header + pairSeparator;
|
||||
List<Pair<String, String>> list = new Vector<Pair<String, String>>();
|
||||
|
||||
for(int i = 0; i < keyList.size(); ++i)
|
||||
{
|
||||
String key = keyList.get(i);
|
||||
String value = enabled;
|
||||
String key = keyList.get(i).toLowerCase();
|
||||
String value = enabled.toLowerCase();
|
||||
|
||||
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));
|
||||
writer.write(outString);
|
||||
writer.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
GlobalLog.Error(LogFilter.Core, "Command enabler issue writing file! " + e.getMessage());
|
||||
}
|
||||
Collections.sort(list, (c1, c2) -> { return c1.First.compareTo(c2.First); });
|
||||
|
||||
Write(list);
|
||||
}
|
||||
|
||||
// Looks up a key to see if it's enabled or not
|
||||
public boolean IsEnabled(String key)
|
||||
{
|
||||
if(enabledMap.containsKey(key))
|
||||
return enabledMap.get(key);
|
||||
String toCheck = key.toLowerCase();
|
||||
|
||||
if(enabledMap.containsKey(toCheck))
|
||||
return enabledMap.get(toCheck);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import dataStructures.Pair;
|
||||
import utils.FileUtils;
|
||||
import utils.io.DirectoryMonitor;
|
||||
import utils.io.FileUtils;
|
||||
import utils.io.MonitoredFile;
|
||||
|
||||
// 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.util.Date;
|
||||
|
||||
import utils.io.FileUtils;
|
||||
|
||||
public class GlobalLog
|
||||
{
|
||||
private static final String directory = "logs/";
|
||||
|
||||
@@ -9,8 +9,6 @@ import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import utils.FileUtils;
|
||||
|
||||
// NOTE: Consider shifting internal behavior to https://docs.oracle.com/javase/tutorial/essential/io/notification.html
|
||||
// for external stability and support
|
||||
public class DirectoryMonitor
|
||||
|
||||
@@ -3,8 +3,6 @@ package utils.io;
|
||||
import java.nio.file.Paths;
|
||||
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.
|
||||
public class FileMonitor
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package utils;
|
||||
package utils.io;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -9,6 +9,9 @@ import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import utils.GlobalLog;
|
||||
import utils.LogFilter;
|
||||
|
||||
public class FileUtils
|
||||
{
|
||||
public static void CreateDirectoryIfDoesntExist(String directoryName)
|
||||
Reference in New Issue
Block a user