+ Added config system
This commit is contained in:
+16
-4
@@ -1,6 +1,7 @@
|
||||
package core;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
import utils.io.FileMonitor;
|
||||
|
||||
@@ -25,9 +26,9 @@ public class Config
|
||||
sections.add(new LocStrings());
|
||||
sections.add(new CommandEnabler());
|
||||
|
||||
// Being monitoring configs and mark this as the instance now that it's made
|
||||
// Build the config file, then start monitoring it.
|
||||
buildConfigFile(filepath);
|
||||
monitoredConfigFile = new FileMonitor(filepath);
|
||||
build(filepath);
|
||||
|
||||
instance = this;
|
||||
}
|
||||
@@ -39,16 +40,27 @@ public class Config
|
||||
}
|
||||
}
|
||||
|
||||
public void build(String path)
|
||||
public void buildConfigFile(String path)
|
||||
{
|
||||
configCSV = new ConfigCSV(sections, path);
|
||||
Map<String, List<ConfigItem>> groupedSections = ConfigCSV.groupByColumn(configCSV.getItems(), 0);
|
||||
|
||||
if(groupedSections != null)
|
||||
{
|
||||
for(IConfigSection section : sections)
|
||||
{
|
||||
List<ConfigItem> items = groupedSections.getOrDefault(section.getSectionTitle(), new Vector<ConfigItem>());
|
||||
section.consume(items);
|
||||
}
|
||||
}
|
||||
|
||||
configCSV.writeFile();
|
||||
}
|
||||
|
||||
public void upkeep()
|
||||
{
|
||||
monitoredConfigFile.update((monitoredFile) -> {
|
||||
build(monitoredFile.path.toString());
|
||||
buildConfigFile(monitoredFile.path.toString());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -91,7 +90,14 @@ public class ConfigCSV
|
||||
// Get a copy of the internal items
|
||||
public List<ConfigItem> getItems()
|
||||
{
|
||||
return Arrays.asList((ConfigItem[])fileContents.toArray());
|
||||
List<ConfigItem> out = new Vector<ConfigItem>();
|
||||
|
||||
for(ConfigItem item : fileContents)
|
||||
{
|
||||
out.add(new ConfigItem(item.type, item.key, item.value));
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
// Set the internal items
|
||||
|
||||
Reference in New Issue
Block a user