Skip to content

Commit

Permalink
Allow providers to "send" error messages to the player with MessageEx…
Browse files Browse the repository at this point in the history
…ception
  • Loading branch information
Badbird5907 committed Jul 8, 2022
1 parent 219c198 commit 4f89338
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import net.octopvp.commander.sender.CoreCommandSender;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
Expand All @@ -52,6 +51,7 @@

import java.lang.reflect.Field;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -116,11 +116,19 @@ public void runAsync(Runnable runnable) {
@Override
public void registerCommand(CommandInfo command) {
if (commandMap.getCommand(command.getName()) == null) {
Command cmd = new BukkitCommandWrapper(command);
BukkitCommandWrapper cmd = new BukkitCommandWrapper(command);
commandMap.register(plugin.getName(), cmd);
command.setPlatformCommandObject(cmd);
}
}

@Override
public void updateCommandAliases(CommandInfo commandInfo) {
Object obj = commandInfo.getPlatformCommandObject();
BukkitCommandWrapper cmd = (BukkitCommandWrapper) obj;
cmd.setAliases(Arrays.asList(commandInfo.getAliases()));
}

@Override
public boolean isSenderParameter(ParameterInfo parameterInfo) {
return parameterInfo.getParameter().isAnnotationPresent(Sender.class) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ private void registerCmd(Object object) {
for (String alias : parent.aliases()) {
commandMap.put(alias.toLowerCase(), parentInfo);
}
if (!object.getClass().isAnnotationPresent(SecondaryParent.class)) {
if (object.getClass().isAnnotationPresent(SecondaryParent.class)) {
if (parentInfo.isRegisteredWithPlatform()) {
getPlatform().updateCommandAliases(parentInfo);
}
} else {
platform.registerCommand(parentInfo);
parentInfo.setRegisteredWithPlatform(true);
}
Expand Down Expand Up @@ -509,6 +513,11 @@ private void executeInternally(CoreCommandSender sender, String label, String[]
}
} catch (CommandException e) {
LocalizedCommandException.checkResponseHandlerNull(e, getResponseHandler());
if (e instanceof MessageException) {
MessageException me = (MessageException) e;
getPlatform().handleMessage(me.getMessage(), sender);
return;
}
platform.handleCommandException(context, e);
} catch (Exception e) {
System.err.println("An error occurred while executing command \"" + label + "\"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
import net.octopvp.commander.command.CommandInfo;
import net.octopvp.commander.command.CompleterInfo;
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.exception.ProvideDefaultException;
import net.octopvp.commander.exception.*;
import net.octopvp.commander.lang.LocalizedCommandException;
import net.octopvp.commander.provider.Provider;
import net.octopvp.commander.sender.CoreCommandSender;
Expand Down Expand Up @@ -109,6 +106,9 @@ public static Object[] parseArguments(CommandContext ctx, CommandArgs cArgs) {
if (e instanceof InvalidArgsException) {
throw new CommandParseException(e.getLocalizedMessage());
}
if (e instanceof MessageException) {
throw e;
}
if (e instanceof ProvideDefaultException) {
obj = null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class CommandInfo { //This is the object that is stored in the command ma

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

private Object platformCommandObject = null;

public CommandInfo(ParameterInfo[] parameters, String name, String description, String usage, String[] aliases, Method method, Object instance, Map<Class<? extends Annotation>, Annotation> annotations, Commander commander) {
this.parameters = parameters;
this.name = name.toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) Badbird5907 2022.
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package net.octopvp.commander.exception;

public class MessageException extends CommandException {
private final String message;

public MessageException(String message) {
this.message = message;
}

public String getMessage() {
return message;
}

@Override
public String getLocalizedMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ public interface CommanderPlatform {

void handleCommandException(CommandInfo info, CoreCommandSender sender, CommandException e);

default boolean hasPermission(CoreCommandSender sender, String permission){
default boolean hasPermission(CoreCommandSender sender, String permission) {
return sender.hasPermission(permission);
}

void registerCommand(CommandInfo command);

void updateCommandAliases(CommandInfo commandInfo);

default boolean isSenderParameter(ParameterInfo parameterInfo) {
return parameterInfo.getParameter().isAnnotationPresent(Sender.class) ||
parameterInfo.getParameter().getName().equalsIgnoreCase("sender") ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public void registerCommand(CommandInfo command) {
//System.out.println("Registered command: " + command.getName());
}

@Override
public void updateCommandAliases(CommandInfo commandInfo) {

}

@Override
public HelpService getHelpService() {
return new HelpService() {
Expand Down

0 comments on commit 4f89338

Please sign in to comment.