config shenanigans

This commit is contained in:
YouHaveTrouble
2021-07-03 22:21:20 +02:00
parent 20eb731b93
commit 88d51b29aa
13 changed files with 190 additions and 149 deletions
+32 -5
View File
@@ -47,15 +47,42 @@
</resources>
</build>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>velocitypowered-repo</id>
<url>https://repo.velocitypowered.com/releases/</url>
</repository>
</repositories>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.28</version>
<groupId>com.github.Thatsmusic99</groupId>
<artifactId>ConfigurationMaster</artifactId>
<version>v2.0.0-ALPHA-2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.velocitypowered</groupId>
<artifactId>velocity-api</artifactId>
<version>1.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.16.5-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.github.waterfallmc</groupId>
<artifactId>waterfall-api</artifactId>
<version>1.16-R0.5-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
@@ -8,18 +8,22 @@ public class CommandUtil {
/**
* Filters blocked command suggestions from provided collection of strings
* @param buffer Command buffer
* @param suggestions Full suggestions list
*
* @param buffer Command buffer
* @param suggestions Full suggestions list
* @param blockedSubCommands Subcommands to filter out
* @return Filtered list of suggestions
*/
public static List<String> filterSuggestions(String buffer, Collection<String> suggestions, Collection<String> blockedSubCommands) {
String cmd = buffer.replace(getLastArgument(buffer), "");
for (String s : blockedSubCommands) {
String slast = getLastArgument(s.toLowerCase());
String slast = getLastArgument(s);
String scommand = s.replace(slast, "");
cmd = cmd.replace(getLastArgument(cmd), "");
if (cmd.substring(1).startsWith("/" + scommand)) {
String[] cmdSplit = buffer.split(" ");
StringBuilder cmdBuilder = new StringBuilder();
for (int i = 0; i <= cmdSplit.length - 1; i++)
cmdBuilder.append(cmdSplit[i]).append(" ");
String cmd = cmdBuilder.toString();
if (cmd.startsWith("/" + scommand)) {
while (suggestions.contains(slast))
suggestions.remove(slast);
}
@@ -33,8 +37,7 @@ public class CommandUtil {
*/
public static String getLastArgument(String cmd) {
String[] parts = cmd.split(" ");
if (parts.length <= 1)
return "";
if (parts.length <= 1) return "";
String last = "";
for (String part : parts) {
last = part;
@@ -1,36 +1,62 @@
package eu.endermite.commandwhitelist.common;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import io.github.thatsmusic99.configurationmaster.api.ConfigFile;
import io.github.thatsmusic99.configurationmaster.api.ConfigSection;
import java.io.*;
import java.util.*;
public class ConfigCache {
private final File configFile;
private ConfigFile config;
private final Object logger;
private final boolean canDoProtocolLib;
private final HashMap<String, CWGroup> groupList = new LinkedHashMap<>();
public String prefix, command_denied, no_permission, no_such_subcommand, config_reloaded, added_to_whitelist,
removed_from_whitelist, group_doesnt_exist, subcommand_denied;
public boolean useProtocolLib = false;
public ConfigCache(File configFile, boolean canDoProtocolLib) {
public ConfigCache(File configFile, boolean canDoProtocolLib, Object logger){
this.configFile = configFile;
this.canDoProtocolLib = canDoProtocolLib;
if (!reloadConfig())
reloadConfig();
this.logger = logger;
reloadConfig();
}
public void saveDefaultConfig(Map<String, Object> data, File configFile, boolean canDoProtocolLib) {
public boolean reloadConfig() {
data.put("messages", processMessages());
boolean firstLoad = createFiles();
config = ConfigFile.loadConfig(configFile);
if (canDoProtocolLib) {
data.put("use_protocollib_to_detect_commands", useProtocolLib);
config.addDefault("messages.prefix", "CommandWhitelist > ");
config.addDefault("messages.command_denied", "No such command.");
config.addDefault("messages.subcommand_denied", "You cannot use this subcommand");
config.addDefault("messages.no_permission", "<red>You don't have permission to do this.");
config.addDefault("messages.no_such_subcommand", "<red>No subcommand by that name.");
config.addDefault("messages.config_reloaded", "<yellow>Configuration reloaded.");
config.addDefault("messages.added_to_whitelist", "<yellow>Whitelisted command <gold>%s <yellow>for permission <gold>%s");
config.addDefault("messages.removed_from_whitelist", "<yellow>Removed command <gold>%s <yellow>from permission <gold>%s");
config.addDefault("messages.group_doesnt_exist", "<red>Group doesn't exist or error occured");
config.addComment("messages", "Messages use MiniMessage formatting (https://docs.adventure.kyori.net/minimessage.html#format)");
if (canDoProtocolLib)
config.addDefault("use_protocollib", false, "Do not enable if you don't have issues with aliased commands.\nThis requires server restart to take effect.");
if (firstLoad) {
List<String> exampleCommands = new ArrayList<>();
exampleCommands.add("example");
List<String> exampleSubCommands = new ArrayList<>();
exampleSubCommands.add("example of");
config.addDefault("groups.example.commands", exampleCommands, "This is the WHITELIST of commands that players will be able to see/use in the group \"example\"");
config.addDefault("groups.example.subcommands", exampleSubCommands, "This is the BLACKLIST of subcommands that players will NOT be able to see/use in the group \"example\"");
config.addComment("groups.example", "All groups except from default require commandwhitelist.group.<group_name> permission\ncommandwhitelist.group.example in this case\n If you wish to leave the list empty, put \"commands: []\" or \"subcommands: []\"");
}
List<String> defaultCommands = new ArrayList<>();
List<String> defaultSubcommands = new ArrayList<>();
defaultCommands.add("help");
defaultCommands.add("spawn");
defaultCommands.add("bal");
@@ -44,73 +70,69 @@ public class ConfigCache {
defaultCommands.add("tpaccept");
defaultCommands.add("tpdeny");
defaultCommands.add("warp");
List<String> defaultSubcommands = new ArrayList<>();
defaultSubcommands.add("help about");
HashMap<String, Object> groups = new LinkedHashMap<>();
config.addDefault("groups.default", new CWGroup("default", defaultCommands, defaultSubcommands).serialize());
for (CWGroup group : groupList.values()) {
groups.put(group.getId(), group.serialize());
}
groups.putIfAbsent("default", new CWGroup("default", defaultCommands, defaultSubcommands).serialize());
//TODO find a way to update groups
data.putIfAbsent("groups", groups);
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setPrettyFlow(true);
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
dumperOptions.setAllowUnicode(true);
Yaml yaml = new Yaml(dumperOptions);
try {
FileWriter writer = new FileWriter(configFile.getPath());
yaml.dump(data, writer);
} catch (IOException e) {
e.printStackTrace();
prefix = config.getString("messages.prefix");
command_denied = config.getString("messages.command_denied");
subcommand_denied = config.getString("messages.subcommand_denied");
no_permission = config.getString("messages.no_permission");
no_such_subcommand = config.getString("messages.no_such_subcommand");
config_reloaded = config.getString("messages.config_reloaded");
added_to_whitelist = config.getString("messages.added_to_whitelist");
removed_from_whitelist = config.getString("messages.removed_from_whitelist");
group_doesnt_exist = config.getString("messages.group_doesnt_exist");
ConfigSection groupSection = config.getConfigSection("groups");
for (String key : groupSection.getKeys(false)) {
groupList.put("key", loadCWGroup(key, groupSection));
}
return saveConfig();
}
public boolean reloadConfig() {
HashMap<String, Object> config = new LinkedHashMap<>();
Yaml yaml = new Yaml();
private boolean saveConfig() {
try {
FileInputStream fileInputStream = new FileInputStream(configFile);
config = yaml.load(fileInputStream);
} catch (FileNotFoundException ignored) {
saveDefaultConfig(config, configFile, canDoProtocolLib);
config.save();
return true;
} catch (IOException e) {
return false;
}
HashMap<String, String> messages = (HashMap<String, String>) config.get("messages");
this.prefix = messages.get("prefix");
this.command_denied = messages.get("command_denied");
this.no_such_subcommand = messages.get("no_such_subcommand");
this.no_permission = messages.get("no_permission");
this.config_reloaded = messages.get("config_reloaded");
this.added_to_whitelist = messages.get("added_to_whitelist");
this.removed_from_whitelist = messages.get("removed_from_whitelist");
this.group_doesnt_exist = messages.get("group_doesnt_exist");
this.subcommand_denied = messages.get("subcommand_denied");
if (canDoProtocolLib)
this.useProtocolLib = (boolean) config.get("use_protocollib_to_detect_commands");
HashMap<String, HashMap<String, Object>> groups = (HashMap<String, HashMap<String, Object>>) config.get("groups");
for (Map.Entry<String, HashMap<String, Object>> entry : groups.entrySet()) {
groupList.put(entry.getKey(), loadCWGroup(entry.getKey(), entry.getValue()));
}
saveDefaultConfig(config, configFile, canDoProtocolLib);
return true;
}
public CWGroup loadCWGroup(String id, HashMap<String, Object> map) {
List<String> subCommands = new ArrayList<>((Collection<? extends String>) map.get("subcommands"));
List<String> commands = new ArrayList<>((Collection<? extends String>) map.get("commands"));
private boolean createFiles() {
boolean creatingFiles = false;
try {
File parent = new File(configFile.getParent());
if (!parent.exists())
parent.mkdir();
if (!configFile.exists()) {
configFile.createNewFile();
creatingFiles = true;
}
return creatingFiles;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public CWGroup loadCWGroup(String id, ConfigSection section) {
HashSet<String> commands = new HashSet<>();
for (String cmd : section.getStringList(id+".commands")) {
if (cmd.contains(" ")) {
String[] cmdSplit = cmd.split(" ");
warn("CommandWhitelist - \""+cmd+"\" is not a command. Loading it as \""+cmdSplit[0]+"\".");
cmd = cmdSplit[0];
}
if (commands.contains(cmd)) continue;
commands.add(cmd);
}
List<String> subCommands = section.getStringList(id+".subcommands");
return new CWGroup(id, commands, subCommands);
}
@@ -118,25 +140,15 @@ public class ConfigCache {
return groupList;
}
public HashMap<String, String> processMessages() {
HashMap<String, String> messages = new LinkedHashMap<>();
messages.put("prefix", stringOrDefault(prefix, "CommandWhitelist > "));
messages.put("command_denied", stringOrDefault(command_denied, "No such command."));
messages.put("subcommand_denied", stringOrDefault(subcommand_denied, "You cannot use this subcommand"));
messages.put("no_permission", stringOrDefault(no_permission, "<red>You don't have permission to do this."));
messages.put("no_such_subcommand", stringOrDefault(no_such_subcommand, "<red>No subcommand by that name."));
messages.put("config_reloaded", stringOrDefault(config_reloaded, "<yellow>Configuration reloaded."));
messages.put("added_to_whitelist", stringOrDefault(added_to_whitelist, "<yellow>Whitelisted command <gold>%s <yellow>for permission <gold>%s"));
messages.put("removed_from_whitelist", stringOrDefault(removed_from_whitelist, "<yellow>Removed command <gold>%s <yellow>from permission <gold>%s"));
messages.put("group_doesnt_exist", stringOrDefault(group_doesnt_exist, "<red>Group doesn't exist or error occured"));
return messages;
}
public String stringOrDefault(String value, String def) {
if (value != null)
return value;
else
return def;
private void warn(String log) {
if (logger instanceof org.slf4j.Logger) {
((org.slf4j.Logger) logger).warn(log);
return;
}
if (logger instanceof java.util.logging.Logger) {
((java.util.logging.Logger) logger).warning(log);
return;
}
}
}