Skip to content

Commit

Permalink
fix some bugs & implemented async
Browse files Browse the repository at this point in the history
  • Loading branch information
Badbird5907 committed Jun 25, 2022
1 parent 1483db4 commit 4182c47
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ public void handleMessage(String message, CoreCommandSender sender) {
@Override
public void handleError(String error, CoreCommandSender sender) {
BukkitCommandSender s = (BukkitCommandSender) sender;
System.out.println("err");
s.sendMessage(ChatColor.RED + error);
}

@Override
public void handleCommandException(CommandContext ctx, CommandException e) {
handleError(e.getMessage(), ctx.getCommandSender());
handleCommandException(ctx.getCommandInfo(), ctx.getCommandSender(), e);
//handleError(e.getMessage(), ctx.getCommandSender());
}

@Override
Expand All @@ -104,11 +104,15 @@ public void handleCommandException(CommandInfo info, CoreCommandSender sender, C
Bukkit.getLogger().severe("Could not find a instance of ResponseHandler to handle command exception: " + e.getClass().getName());
return;
}
System.out.println("LocalizedCommandException");
sender.sendMessage(ChatColor.RED + handler.getMessage(lce, lce.getPlaceholders()));
} else sender.sendMessage(ChatColor.RED + e.getMessage());
}

@Override
public void runAsync(Runnable runnable) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, runnable);
}

@Override
public void registerCommand(CommandInfo command) {
if (commandMap.getCommand(command.getName()) == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,20 @@ public Commander registerCommandPostProcessor(BiConsumer<CommandContext, Object>
@Override
public void executeCommand(CoreCommandSender sender, String label, String[] args) throws CommandParseException {
CommandInfo commandInfo = commandMap.get(label);
try {
if (commandInfo == null) {
throw new CommandNotFoundException("handler.not-found", label.toLowerCase());
}
if (commandInfo.isAsync()) {
getPlatform().runAsync(() -> executeInternally(sender, label, args, commandInfo));
} else executeInternally(sender, label, args, commandInfo);
} catch (CommandException e) {
LocalizedCommandException.checkResponseHandlerNull(e, getResponseHandler());
platform.handleCommandException(commandInfo, sender, e);
}
}

private void executeInternally(CoreCommandSender sender, String label, String[] args, CommandInfo commandInfo) {
if (args == null) {
args = new String[]{};
}
Expand Down Expand Up @@ -486,6 +499,7 @@ public void executeCommand(CoreCommandSender sender, String label, String[] args
LocalizedCommandException.checkResponseHandlerNull(e, getResponseHandler());
platform.handleCommandException(commandInfo, sender, e);
}

}

private Map<String, String> extractFlags(final List<String> args, final ParameterInfo[] params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,21 @@ public static Object[] parseArguments(CommandContext ctx, CommandArgs cArgs) {
obj = provider.provide(ctx, ctx.getCommandInfo(), parameter, cArgs.getArgs());
} catch (Exception e) {
//e.printStackTrace();
LocalizedCommandException.checkResponseHandlerNull(e, ctx.getCommandInfo().getCommander().getResponseHandler());
if (e instanceof InvalidArgsException) {
throw new CommandParseException(e);
throw new CommandParseException(e.getLocalizedMessage());
}
if (provider.provideUsageOnException()) {
throw new InvalidArgsException(ctx.getCommandInfo());
}
if (provider.failOnExceptionIgnoreOptional()) {
throw new CommandParseException(parameter.getName(), e);
throw new CommandParseException("parse.fail", e.getMessage());
}
if (parameter.isOptional()) {
obj = null;
} else if (provider.failOnException())
throw new CommandParseException(parameter.getName(), e);
else {
} else if (provider.failOnException()) {
throw new CommandParseException("parse.fail", e.getMessage());
} else {
obj = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
import lombok.Getter;
import lombok.Setter;
import net.octopvp.commander.Commander;
import net.octopvp.commander.annotation.Command;
import net.octopvp.commander.annotation.Cooldown;
import net.octopvp.commander.annotation.Dependency;
import net.octopvp.commander.annotation.Permission;
import net.octopvp.commander.annotation.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -67,6 +64,8 @@ public class CommandInfo { //This is the object that is stored in the command ma
private boolean hasFlags, foundFlagsAlready;
private boolean hasSwitches, foundSwitchesAlready;

private boolean async;

private Map<Integer, CompleterInfo> completers = new HashMap<>();

public CommandInfo(ParameterInfo[] parameters, String name, String description, String usage, String[] aliases, Method method, Object instance, Map<Class<? extends Annotation>, Annotation> annotations, Commander commander) {
Expand All @@ -88,6 +87,9 @@ public CommandInfo(ParameterInfo[] parameters, String name, String description,
this.cooldownUnit = getAnnotation(Cooldown.class).unit();
this.cooldownMap = new HashMap<>();
}
if (isAnnotationPresent(Async.class)) {
this.async = true;
}
//make sure aliases are lowercase
this.aliases = aliases;
for (int i = 0; i < aliases.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public CommandParseException() {
}

public CommandParseException(Exception cause) {
super("parse.fail", cause);
super("parse.fail", cause.getLocalizedMessage());
}

public CommandParseException(Object... placeholders) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ public LocalizedCommandException(String key, Object... placeholders) {
this.placeholders = placeholders;
}

/*
public LocalizedCommandException(String key, Exception cause, Object... placeholders) {
super(cause);
this.key = key;
this.placeholders = placeholders;
//add cause
}
*/

public static void checkResponseHandlerNull(Exception e, ResponseHandler responseHandler) {
if (e instanceof LocalizedCommandException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import org.reflections.Reflections;

import java.util.Collection;
import java.util.List;
import java.util.UUID;

public interface CommanderPlatform {
void handleMessage(String message, CoreCommandSender sender);
Expand All @@ -58,17 +56,19 @@ default boolean isSenderParameter(ParameterInfo parameterInfo) {
parameterInfo.getParameter().getType().equals(CoreCommandSender.class);
}

default String getPrefix(){
default String getPrefix() {
return "/";
}

HelpService getHelpService();

default Collection<Class<?>> getClassesInPackage(String packageName){
void runAsync(Runnable runnable);

default Collection<Class<?>> getClassesInPackage(String packageName) {
return getReflections(packageName).getSubTypesOf(Object.class);
}

default Reflections getReflections(String packageName){
default Reflections getReflections(String packageName) {
return new Reflections(packageName);
}
}
1 change: 1 addition & 0 deletions Commander-Core/src/main/resources/commander_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ illegal.date=Illegal Date
no-permission=You do not have permission to perform this command.
suggestion.completer-must-return-string=Completer method must return a collection of strings.
suggestion.failed-to-invoke=Failed to invoke completer method.
command.not-found=Command not found!
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ public void sendHelp(CommandInfo info, CoreCommandSender sender) {
}
};
}

@Override
public void runAsync(Runnable runnable) {
new Thread(runnable, "Commander Async").start();
}
}

0 comments on commit 4182c47

Please sign in to comment.