Created
April 18, 2020 14:54
-
-
Save HeathLoganCampbell/2e1bffe88874eba92daf57ca94c18e97 to your computer and use it in GitHub Desktop.
A slightly even more useful command CoI DI system. Converting commands to easier to use code and not having to deal with arg lengths and constantly converting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Parameter; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
public class CommandBase | |
{ | |
private CommandFramework parent; | |
private List<RegisteredSubCommand> registeredSubCommands = new ArrayList<>(); | |
public CommandBase(CommandFramework parent) | |
{ | |
this.parent = parent; | |
} | |
public void registerSubCommand(Method execMethod, Object obj, String[] syntaxArgs, Command commandAnno) | |
{ | |
HashMap<String, Class<?>> clazzTypes = new HashMap<>(); | |
Parameter[] parameters = execMethod.getParameters(); | |
for (Parameter parameter : parameters) | |
{ | |
if(parameter.isAnnotationPresent(Param.class)) | |
{ | |
Param param = parameter.getAnnotation(Param.class); | |
clazzTypes.put(param.value(), parameter.getType()); | |
} | |
} | |
RegisteredSubCommand registeredSubCommand = new RegisteredSubCommand(clazzTypes, syntaxArgs, execMethod, parameters, obj); | |
registeredSubCommands.add(registeredSubCommand); | |
} | |
public void execute(String[] args) throws InvocationTargetException, IllegalAccessException { | |
registeredCheck: | |
for (RegisteredSubCommand registeredSubCommand : registeredSubCommands) | |
{ | |
HashMap<Class<?>, Object> passArgs = new HashMap<>(); | |
for (int i = 0; i < registeredSubCommand.getSyntaxArgs().length; i++) | |
{ | |
String syntaxArg = registeredSubCommand.getSyntaxArgs()[i]; | |
String arg = args[i]; | |
if(!syntaxArg.equalsIgnoreCase(arg)) | |
{ | |
if(Patterns.BINDING.matcher(syntaxArg).find()) | |
{ | |
//is place holder | |
String bindName = syntaxArg.substring(1, syntaxArg.length() - 1); | |
Class<?> aClass = registeredSubCommand.getClazzTypes().get(bindName); | |
Object resolve = this.parent.resolve(args, i, aClass); | |
passArgs.put(aClass, resolve); | |
} | |
else | |
{ | |
continue registeredCheck; | |
} | |
} | |
} | |
Object[] injectedObj = new Object[registeredSubCommand.getParameters().length]; | |
int i = 0; | |
for (Parameter parameter : registeredSubCommand.getParameters()) | |
{ | |
Class<?> clazzType = parameter.getType(); | |
Object obj = passArgs.get(clazzType); | |
injectedObj[i] = obj; | |
i++; | |
} | |
registeredSubCommand.getExecutor().invoke(registeredSubCommand.getInstance(), injectedObj); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
public class CommandFramework | |
{ | |
private HashMap<String, CommandBase> commands = new HashMap<>(); | |
public void registerCommands(Object obj) | |
{ | |
for (Method method : obj.getClass().getDeclaredMethods()) | |
{ | |
if(method.isAnnotationPresent(Command.class)) | |
{ | |
Command commandAnno = method.getDeclaredAnnotation(Command.class); | |
String syntax = commandAnno.syntax(); | |
String[] syntaxArgs = Patterns.SPACE.split(syntax); | |
String commandLabel = syntaxArgs[0]; | |
String[] syntaxArgsNoLabel = Arrays.copyOfRange(syntaxArgs, 1, syntaxArgs.length); | |
CommandBase commandBase = commands.get(commandLabel); | |
if(commandBase == null) commandBase = new CommandBase(this); | |
commandBase.registerSubCommand(method, obj, syntaxArgsNoLabel, commandAnno); | |
this.commands.put(commandLabel, commandBase); | |
System.out.println("SwordCF | Register " + commandLabel); | |
} | |
} | |
} | |
public void execute(String cmdLabel, String[] args) | |
{ | |
CommandBase commandBase = this.commands.get(cmdLabel); | |
if(commandBase == null) return; | |
try { | |
commandBase.execute(args); | |
} catch (InvocationTargetException | IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
public Object resolve(String[] args, int targetIndex, Class<?> clazz) | |
{ | |
String str = args[targetIndex]; | |
if(clazz == String.class) | |
{ | |
return str; | |
} | |
if(clazz == User.class) | |
{ | |
return new User(); | |
} | |
if(clazz == Gift.class) | |
{ | |
return Gift.valueOf(str.toUpperCase()); | |
} | |
if(clazz == String[].class) | |
{ | |
String[] result = new String[args.length - targetIndex]; | |
for (int i = targetIndex; i < args.length; i++) | |
result[i - 2] = args[i]; | |
return result; | |
} | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
EXAMPLE | |
**/ | |
import java.util.Arrays; | |
public class Main | |
{ | |
public static void main(String[] args) { | |
CommandFramework commandFramework = new CommandFramework(); | |
commandFramework.registerCommands(new Main()); | |
commandFramework.execute("f", "add Sprock umm me man".split(" ")); | |
} | |
@Command(syntax = "f add <user> <message>") | |
public void onAdd(@Param("user") User user, @Param("message") String[] message) | |
{ | |
System.out.println("I did work... wtffff Nicee " + Arrays.toString(message)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Pattern; | |
public class Patterns | |
{ | |
public static final Pattern PIPE = Pattern.compile("\\|"); | |
public static final Pattern SPACE = Pattern.compile(" "); | |
public static final Pattern BINDING = Pattern.compile("<\\w+>"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.AllArgsConstructor; | |
import lombok.Getter; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Parameter; | |
import java.util.HashMap; | |
@Getter | |
@AllArgsConstructor | |
public class RegisteredSubCommand | |
{ | |
private final HashMap<String, Class<?>> clazzTypes; | |
private final String[] syntaxArgs; | |
private final Method executor; | |
private final Parameter[] parameters; | |
private final Object instance; | |
} |
I believe I was just using a script to upload it
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can revise Gists instead of creating new ones FYI :)