Skip to content

Commit

Permalink
Add Router | Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
RoiEXLab committed Apr 28, 2017
1 parent b1171e6 commit d883135
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/roiex/plugins/cmdhelper/CMDHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
if (command.getPermission() != null && !sender.hasPermission(command.getPermission())) {
break tryLabel;
}
Arrays.stream(permissionMasks).filter(m -> StructureParser.matches(m, args, sender));
Arrays.stream(permissionMasks).filter(m -> StructureParser.matches(m.getMask(), args, sender));
List<CommandArgument> cmdArgs = StructureParser.getCommandArguments(pattern, args, sender);
for (PermissionMask mask : permissionMasks) {
if (!sender.hasPermission(mask.getPermission())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.roiex.plugins.cmdhelper;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

@FunctionalInterface
public interface CommandConsumer {
boolean runAction(CommandSender sender, Command command, String label, String[] args);
}
31 changes: 31 additions & 0 deletions src/main/java/com/roiex/plugins/cmdhelper/CommandRouter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.roiex.plugins.cmdhelper;

import java.util.HashMap;
import java.util.Map;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class CommandRouter implements CommandExecutor {

private Map<String, CommandConsumer> routes = new HashMap<>();

public void addRoute(String pattern, CommandConsumer consumer){
routes.put(pattern, consumer);
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
boolean executed = false;
boolean success = true;
for(Map.Entry<String, CommandConsumer> entry : routes.entrySet()){
if(StructureParser.matches(entry.getKey(), args, sender)){
executed = true;
success &= entry.getValue().runAction(sender, command, label, args);
}
}
return success && executed;
}

}
12 changes: 6 additions & 6 deletions src/main/java/com/roiex/plugins/cmdhelper/StructureParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ private static int checkRecursively(char[] chars, int startIndex) {
return chars.length - 1;
}

public static boolean matches(PermissionMask mask, String[] args, CommandSender sender) {
public static boolean matches(String mask, String[] args, CommandSender sender) {
return matches(0, mask, args, sender);
}

private static boolean matches(int index, PermissionMask mask, String[] args, CommandSender sender) {
String nextArg = findNextArg(mask.getMask());
private static boolean matches(int index, String pattern, String[] args, CommandSender sender) {
String nextArg = findNextArg(pattern);
String[] subArgs = nextArg.split("\\|");
for (String subArg : subArgs) {
if (subArg.startsWith("<") && subArg.endsWith(">")) {
String identifier = subArg.substring(1, subArg.length() - 1);
CommandArgument current = CommandArgument.args.get(identifier);
if (current != null) {
return current.matches(args[index], sender) && (index + 1 >= args.length || matches(index + 1, new PermissionMask(mask.getMask().substring(nextArg.length()).trim(), mask.getPermission()), args, sender));
return current.matches(args[index], sender) && (index + 1 >= args.length || matches(index + 1, pattern.substring(nextArg.length()).trim(), args, sender));
} else {
throw new IllegalArgumentException("Indentifier '" + identifier + "' doesn't exist!");
}
} else if (subArg.startsWith("[") && subArg.endsWith("]")) {
return matches(index, new PermissionMask(subArg.substring(1, subArg.length() - 1) + mask.getMask().substring(nextArg.length()), mask.getPermission()), args, sender);
return matches(index, subArg.substring(1, subArg.length() - 1) + pattern.substring(nextArg.length()), args, sender);
} else {
return subArg.equalsIgnoreCase(args[index]) && (index + 1 >= args.length || matches(index + 1, new PermissionMask(mask.getMask().substring(nextArg.length()).trim(), mask.getPermission()), args, sender));
return subArg.equalsIgnoreCase(args[index]) && (index + 1 >= args.length || matches(index + 1, pattern.substring(nextArg.length()).trim(), args, sender));
}
}
return false;
Expand Down

0 comments on commit d883135

Please sign in to comment.