reorganize stuff

This commit is contained in:
YouHaveTrouble
2021-04-25 19:01:27 +02:00
parent 8a16ee39ef
commit d4396fdba2
42 changed files with 1125 additions and 949 deletions
+60
View File
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>CommandWhitelist</artifactId>
<groupId>eu.endermite</groupId>
<version>2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Common</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.28</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,58 @@
package eu.endermite.commandwhitelist.common;
import java.util.*;
public class CWGroup {
private final String id, permission;
private final HashSet<String> commands = new HashSet<>();
private final HashSet<String> subCommands = new HashSet<>();
public CWGroup(String id, Collection<String> commands, Collection<String> subCommands) {
this.id = id;
this.permission = "commandwhitelist.group."+id;
this.commands.addAll(commands);
this.subCommands.addAll(subCommands);
}
public String getId() {
return id;
}
public String getPermission() {
return permission;
}
public HashSet<String> getCommands() {
return commands;
}
public void addCommand(String command) {
commands.add(command);
}
public void removeCommand(String command) {
commands.remove(command);
}
public HashSet<String> getSubCommands() {
return subCommands;
}
public void addSubCommand(String subCommand) {
subCommands.add(subCommand);
}
public void removeSubCommand(String subCommand) {
subCommands.remove(subCommand);
}
public HashMap<String, Object> serialize() {
HashMap<String, Object> serializedGroup = new LinkedHashMap<>();
List<String> commands = new ArrayList<>(this.commands);
List<String> subCommands = new ArrayList<>(this.subCommands);
serializedGroup.put("commands", commands);
serializedGroup.put("subcommands", subCommands);
return serializedGroup;
}
}
@@ -0,0 +1,56 @@
package eu.endermite.commandwhitelist.common;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class CommandUtil {
/**
* Filters blocked command suggestions from provided collection of strings
* @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 scommand = s.replace(slast, "");
cmd = cmd.replace(getLastArgument(cmd), "");
if (cmd.substring(1).startsWith("/" + scommand)) {
while (suggestions.contains(slast))
suggestions.remove(slast);
}
}
return new ArrayList<>(suggestions);
}
/**
* @param cmd The command
* @return Last argument of the command
*/
public static String getLastArgument(String cmd) {
String[] parts = cmd.split(" ");
if (parts.length <= 1)
return "";
String last = "";
for (String part : parts) {
last = part;
}
return last;
}
/**
* @param cmd The command
* @return Command label
*/
public static String getCommandLabel(String cmd) {
String[] parts = cmd.split(" ");
if (parts[0].startsWith("/"))
parts[0] = parts[0].substring(1);
return parts[0];
}
}
@@ -0,0 +1,143 @@
package eu.endermite.commandwhitelist.common;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import java.io.*;
import java.util.*;
public class ConfigCache {
private final File configFile;
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) {
this.configFile = configFile;
this.canDoProtocolLib = canDoProtocolLib;
if (!reloadConfig())
reloadConfig();
}
public void saveDefaultConfig(Map<String, Object> data, File configFile, boolean canDoProtocolLib) {
data.put("messages", processMessages());
if (canDoProtocolLib) {
data.put("use_protocollib_to_detect_commands", useProtocolLib);
}
List<String> defaultCommands = new ArrayList<>();
List<String> defaultSubcommands = new ArrayList<>();
defaultCommands.add("help");
defaultCommands.add("spawn");
defaultCommands.add("bal");
defaultCommands.add("balance");
defaultCommands.add("baltop");
defaultCommands.add("pay");
defaultCommands.add("r");
defaultCommands.add("msg");
defaultCommands.add("tpa");
defaultCommands.add("tpahere");
defaultCommands.add("tpaccept");
defaultCommands.add("tpdeny");
defaultCommands.add("warp");
defaultSubcommands.add("help about");
HashMap<String, Object> groups = new LinkedHashMap<>();
for (CWGroup group : groupList.values()) {
groups.put(group.getId(), group.serialize());
}
groups.putIfAbsent("default", new CWGroup("default", defaultCommands, defaultSubcommands).serialize());
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();
}
}
public boolean reloadConfig() {
HashMap<String, Object> config = new LinkedHashMap<>();
Yaml yaml = new Yaml();
try {
FileInputStream fileInputStream = new FileInputStream(configFile);
config = yaml.load(fileInputStream);
} catch (FileNotFoundException ignored) {
saveDefaultConfig(config, configFile, canDoProtocolLib);
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"));
return new CWGroup(id, commands, subCommands);
}
public HashMap<String, CWGroup> getGroupList() {
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 <orange>%s <yellow>for permission <orange>%s"));
messages.put("removed_from_whitelist", stringOrDefault(removed_from_whitelist, "<yellow>Removed command <orange>%s <yellow>from permission <orange>%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;
}
}
@@ -0,0 +1,53 @@
package eu.endermite.commandwhitelist.common.commands;
import eu.endermite.commandwhitelist.common.CWGroup;
import eu.endermite.commandwhitelist.common.ConfigCache;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
public class CWCommand {
public static boolean addToWhitelist(ConfigCache configCache, String command, String group) {
CWGroup cwGroup = configCache.getGroupList().get(group);
if (cwGroup == null)
return false;
cwGroup.addCommand(command);
configCache.reloadConfig();
return true;
}
public static boolean removeFromWhitelist(ConfigCache configCache, String command, String group) {
CWGroup cwGroup = configCache.getGroupList().get(group);
if (cwGroup == null)
return false;
cwGroup.removeCommand(command);
configCache.reloadConfig();
return true;
}
public Component helpComponent(String baseCommand, boolean showReloadCommand, boolean showAdminCommands) {
Component component = Component.text("CommandWhitelist by YouHaveTrouble").decorate(TextDecoration.BOLD).color(NamedTextColor.BLUE)
.append(Component.newline());
component = component.append(Component.text("Hover over the command to see what it does!").color(NamedTextColor.AQUA)).decoration(TextDecoration.BOLD, false).append(Component.newline());
component = component.append(Component.text("/"+baseCommand+" help").color(NamedTextColor.AQUA).hoverEvent(HoverEvent.showText(Component.text("Displays this message"))));
if (showReloadCommand) {
component = component.append(Component.newline());
component = component.append(Component.text("/"+baseCommand+" reload").color(NamedTextColor.AQUA).hoverEvent(HoverEvent.showText(Component.text("Reloads plugin configuration"))));
}
if (showAdminCommands) {
component = component.append(Component.newline());
component = component.append(Component.text("/"+baseCommand+" add <group> <command>").color(NamedTextColor.AQUA).hoverEvent(HoverEvent.showText(Component.text("Add a command to selected permission group"))));
component = component.append(Component.newline());
component = component.append(Component.text("/"+baseCommand+" remove <group> <command>").color(NamedTextColor.AQUA).hoverEvent(HoverEvent.showText(Component.text("Removes a command from selected permission group"))));
}
return component;
}
public enum CommandType {
ADD, REMOVE, HELP, RELOAD
}
}