Skip to content

Commit

Permalink
fix some annotation bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Badbird5907 committed Jun 7, 2022
1 parent bbb2ed0 commit fbe9917
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ public class PlayerProvider implements Provider<Player> {

@Override
public Player provide(CommandContext context, CommandInfo commandInfo, ParameterInfo parameterInfo, Deque<String> args) {
if (commandInfo.getCommander().getPlatform().isSenderParameter(parameterInfo)) return (Player) ((BukkitCommandSender) context.getCommandSender()).getSender();
if (args.size() == 0) {
if (parameterInfo.getParameter().isAnnotationPresent(DefaultSelf.class))
return ((BukkitCommandSender) context.getCommandSender()).getPlayer();
return null;
}
return parameterInfo.getParameter().isAnnotationPresent(Sender.class) || parameterInfo.getParameter().getName().equalsIgnoreCase("sender") ? (Player) ((BukkitCommandSender) context.getCommandSender()).getSender() : Bukkit.getPlayer(args.pop());
return Bukkit.getPlayer(args.pop());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.octopvp.commander.annotation.Dependency;
import net.octopvp.commander.command.CommandContext;
import net.octopvp.commander.command.ParameterInfo;
import net.octopvp.commander.exception.CommandException;
import net.octopvp.commander.exception.CommandParseException;
import net.octopvp.commander.exception.InvalidArgsException;
import net.octopvp.commander.provider.Provider;
Expand All @@ -16,75 +17,80 @@

public class ArgumentParser {
public static Object[] parseArguments(CommandContext ctx, CommandArgs cArgs) {
Object[] arguments = new Object[ctx.getCommandInfo().getParameters().length];
for (int i = 0; i < ctx.getCommandInfo().getParameters().length; i++) {
ParameterInfo parameter = ctx.getCommandInfo().getParameters()[i];
if (parameter.isFlag()) {
if (cArgs.getFlags() == null) {
throw new CommandParseException("Flags are null!");
try {
Object[] arguments = new Object[ctx.getCommandInfo().getParameters().length];
for (int i = 0; i < ctx.getCommandInfo().getParameters().length; i++) {
ParameterInfo parameter = ctx.getCommandInfo().getParameters()[i];
if (parameter.isFlag()) {
if (cArgs.getFlags() == null) {
throw new CommandParseException("Flags are null!");
}
String f = cArgs.getFlags().get(parameter.getFlag());
if (f != null) validate(f, parameter, ctx);
arguments[i] = f;
continue;
}
String f = cArgs.getFlags().get(parameter.getFlag());
if (f != null) validate(f, parameter, ctx);
arguments[i] = f;
continue;
}
if (parameter.isSwitch()) {
if (cArgs.getSwitches() == null) {
throw new CommandParseException("Switches are null!");
if (parameter.isSwitch()) {
if (cArgs.getSwitches() == null) {
throw new CommandParseException("Switches are null!");
}
Boolean b = cArgs.getSwitches().get(parameter.getSwitch());
if (b != null) validate(b, parameter, ctx);
arguments[i] = b != null && b;
continue;
}
Boolean b = cArgs.getSwitches().get(parameter.getSwitch());
if (b != null) validate(b, parameter, ctx);
arguments[i] = b != null && b;
continue;
}
if (parameter.getParameter().isAnnotationPresent(Dependency.class)) {
Class<?> classType = parameter.getParameter().getType();
Supplier<?> supplier = cArgs.getCommander().getDependencies().get(classType);
if (supplier == null) {
throw new CommandParseException("Dependency not found for " + classType.getName()); //maybe let the user control this?
if (parameter.getParameter().isAnnotationPresent(Dependency.class)) {
Class<?> classType = parameter.getParameter().getType();
Supplier<?> supplier = cArgs.getCommander().getDependencies().get(classType);
if (supplier == null) {
throw new CommandParseException("Dependency not found for " + classType.getName()); //maybe let the user control this?
}
arguments[i] = supplier.get();
continue;
}
arguments[i] = supplier.get();
continue;
}
Provider<?> provider = parameter.getProvider();
Provider<?> provider = parameter.getProvider();

if (provider == null) {
throw new CommandParseException("No provider found for " + parameter.getParameter().getType().getName());
}
Object obj;
try {
obj = provider.provide(ctx, ctx.getCommandInfo(), parameter, cArgs.getArgs());
} catch (Exception e) {
if (e instanceof InvalidArgsException) {
//cArgs.getCommander().getPlatform().getHelpService().sendHelp(ctx, ctx.getCommandSender());
//return null;
throw e;
}
if (provider.provideUsageOnException()) {
throw new InvalidArgsException(ctx.getCommandInfo());
if (provider == null) {
throw new CommandParseException("No provider found for " + parameter.getParameter().getType().getName());
}
if (provider.failOnExceptionIgnoreOptional()) {
throw new CommandParseException("Failed to parse argument " + parameter.getParameter().getName(), e);
Object obj;
try {
obj = provider.provide(ctx, ctx.getCommandInfo(), parameter, cArgs.getArgs());
} catch (Exception e) {
if (e instanceof InvalidArgsException) {
//cArgs.getCommander().getPlatform().getHelpService().sendHelp(ctx, ctx.getCommandSender());
//return null;
throw e;
}
if (provider.provideUsageOnException()) {
throw new InvalidArgsException(ctx.getCommandInfo());
}
if (provider.failOnExceptionIgnoreOptional()) {
throw new CommandParseException("Failed to parse argument " + parameter.getName(), e);
}
if (parameter.isOptional()) {
obj = null;
} else if (provider.failOnException())
throw new CommandParseException("Failed to parse argument " + parameter.getName(), e);
else {
obj = null;
}
}
if (parameter.isOptional()) {
obj = null;
} else if (provider.failOnException())
throw new CommandParseException("Failed to parse argument " + parameter.getParameter().getName(), e);
else {
obj = null;
if (obj == null) {
obj = provider.provideDefault(ctx, ctx.getCommandInfo(), parameter, cArgs.getArgs());
}
}
if (obj == null) {
obj = provider.provideDefault(ctx, ctx.getCommandInfo(), parameter, cArgs.getArgs());
}

if (obj == null && parameter.isRequired()) {
throw new CommandParseException("Required argument " + parameter.getParameter().getName() + " is null!");
if (obj == null && parameter.isRequired()) {
throw new CommandParseException("Required argument " + parameter.getName() + " is null!");
}
if (obj != null) validate(obj, parameter, ctx);
arguments[i] = obj;
}
if (obj != null) validate(obj, parameter, ctx);
arguments[i] = obj;
return arguments;
} catch (CommandException e) {
ctx.getCommandInfo().getCommander().getPlatform().handleCommandException(ctx, e);
}
return arguments;
return null;
}

private static void validate(Object obj, ParameterInfo parameter, CommandContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class SenderProvider implements Provider<CoreCommandSender> {
@Override
public CoreCommandSender provide(CommandContext context, CommandInfo commandInfo, ParameterInfo parameterInfo, Deque<String> args) {
return parameterInfo.getParameter().isAnnotationPresent(Sender.class) || parameterInfo.getParameter().getName().equalsIgnoreCase("sender") ? context.getCommandSender() : null;
return commandInfo.getCommander().getPlatform().isSenderParameter(parameterInfo) ? context.getCommandSender() : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package net.octopvp.commander.provider.impl;

import net.octopvp.commander.annotation.JoinStrings;
import net.octopvp.commander.annotation.Required;
import net.octopvp.commander.command.CommandContext;
import net.octopvp.commander.command.CommandInfo;
import net.octopvp.commander.command.ParameterInfo;
import net.octopvp.commander.exception.InvalidArgsException;
import net.octopvp.commander.provider.Provider;
import net.octopvp.commander.sender.CoreCommandSender;

Expand All @@ -15,6 +17,9 @@ public class StringProvider implements Provider<String> {
public String provide(CommandContext context, CommandInfo commandInfo, ParameterInfo parameterInfo, Deque<String> args) {
if (parameterInfo.getParameter().isAnnotationPresent(JoinStrings.class)) {
if (args.size() == 0) {
if (parameterInfo.getParameter().isAnnotationPresent(Required.class)) {
throw new InvalidArgsException(commandInfo);
}
return null;
}

Expand All @@ -23,6 +28,15 @@ public String provide(CommandContext context, CommandInfo commandInfo, Parameter
return args.poll();
}

@Override
public String provideDefault(CommandContext context, CommandInfo commandInfo, ParameterInfo parameterInfo, Deque<String> args) {
if (args.size() == 0 && parameterInfo.getParameter().isAnnotationPresent(Required.class)) {
System.out.println("def");
throw new InvalidArgsException(commandInfo);
}
return null;
}

@Override
public List<String> provideSuggestions(String input, String lastArg, CoreCommandSender sender) {
return null;
Expand Down

0 comments on commit fbe9917

Please sign in to comment.