mirror of
https://github.com/YouHaveTrouble/CommandWhitelist.git
synced 2026-05-11 22:16:57 +00:00
made perms enum
added some missing stuff
This commit is contained in:
+31
@@ -0,0 +1,31 @@
|
||||
package eu.endermite.commandwhitelist.common;
|
||||
|
||||
public enum CWPermission {
|
||||
|
||||
ADMIN("commandwhitelist.admin"),
|
||||
RELOAD("commandwhitelist.reload"),
|
||||
BYPASS("commandwhitelist.bypass");
|
||||
|
||||
private final String permission;
|
||||
|
||||
CWPermission(String permission) {
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
public String permission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows to check specific group permission
|
||||
* @param configCache
|
||||
* @param groupId
|
||||
* @return
|
||||
*/
|
||||
public static String getGroupPermission(ConfigCache configCache, String groupId) {
|
||||
if (configCache.getGroupList().containsKey(groupId))
|
||||
return configCache.getGroupList().get(groupId).getPermission();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
+18
-13
@@ -15,15 +15,12 @@ public class CommandUtil {
|
||||
* @return Filtered list of suggestions
|
||||
*/
|
||||
public static List<String> filterSuggestions(String buffer, Collection<String> suggestions, Collection<String> blockedSubCommands) {
|
||||
if (buffer.startsWith("/"))
|
||||
buffer = buffer.substring(1);
|
||||
for (String s : blockedSubCommands) {
|
||||
String slast = getLastArgument(s);
|
||||
String scommand = s.replace(slast, "");
|
||||
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)) {
|
||||
String scommand = cutLastArgument(s);
|
||||
if (buffer.startsWith(scommand)) {
|
||||
while (suggestions.contains(slast))
|
||||
suggestions.remove(slast);
|
||||
}
|
||||
@@ -37,12 +34,20 @@ public class CommandUtil {
|
||||
*/
|
||||
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;
|
||||
if (parts.length == 0) return "";
|
||||
return parts[parts.length - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cmd The command
|
||||
* @return Command without the last argument.
|
||||
*/
|
||||
public static String cutLastArgument(String cmd) {
|
||||
String[] cmdSplit = cmd.split(" ");
|
||||
StringBuilder cmdBuilder = new StringBuilder();
|
||||
for (int i = 0; i <= cmdSplit.length - 2; i++)
|
||||
cmdBuilder.append(cmdSplit[i]).append(" ");
|
||||
return cmdBuilder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+57
@@ -8,6 +8,10 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class CWCommand {
|
||||
|
||||
public static boolean addToWhitelist(ConfigCache configCache, String command, String group) {
|
||||
@@ -50,4 +54,57 @@ public class CWCommand {
|
||||
ADD, REMOVE, HELP, RELOAD
|
||||
}
|
||||
|
||||
public static List<String> commandSuggestions(ConfigCache config, Collection<String> serverCommands, String[] args, boolean reloadPerm, boolean adminPerm) {
|
||||
List<String> list = new ArrayList<>();
|
||||
switch (args.length) {
|
||||
case 1:
|
||||
if ("reload".startsWith(args[0]) && reloadPerm)
|
||||
list.add("reload");
|
||||
if ("add".startsWith(args[0]) && adminPerm)
|
||||
list.add("add");
|
||||
if ("remove".startsWith(args[0]) && adminPerm)
|
||||
list.add("remove");
|
||||
return list;
|
||||
case 2:
|
||||
if (args[0].equalsIgnoreCase("add") || args[0].equalsIgnoreCase("remove")) {
|
||||
if (!adminPerm) return list;
|
||||
for (String s : config.getGroupList().keySet()) {
|
||||
if (s.startsWith(args[1]))
|
||||
list.add(s);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
case 3:
|
||||
if (args[0].equalsIgnoreCase("remove")) {
|
||||
if (!adminPerm) return list;
|
||||
try {
|
||||
for (String s : config.getGroupList().get(args[1]).getCommands()) {
|
||||
if (s.startsWith(args[2]))
|
||||
list.add(s);
|
||||
}
|
||||
} catch (NullPointerException ignored) {
|
||||
}
|
||||
return list;
|
||||
}
|
||||
if (args[0].equalsIgnoreCase("add")) {
|
||||
if (!adminPerm) return list;
|
||||
for (String cmd : serverCommands) {
|
||||
if (!cmd.startsWith("/")) continue;
|
||||
if (cmd.contains(":")) {
|
||||
String[] cmdSplit = cmd.split(":");
|
||||
if (cmdSplit.length < 2) continue;
|
||||
cmd = cmd.split(":")[1];
|
||||
}
|
||||
cmd = cmd.replace("/", "");
|
||||
if (config.getGroupList().get(args[1]) == null) continue;
|
||||
if (config.getGroupList().get(args[1]).getCommands().contains(cmd)) continue;
|
||||
if (cmd.startsWith(args[2]))
|
||||
list.add(cmd);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user