Merge pull request #7 from xCodiq/master
Patched command execution + new command exception handling
This commit is contained in:
commit
7768e05587
9 changed files with 59 additions and 27 deletions
|
@ -2,6 +2,9 @@ package io.github.znetworkw.znpcservers.commands;
|
|||
|
||||
import com.google.common.collect.Iterables;
|
||||
import io.github.znetworkw.znpcservers.cache.CacheRegistry;
|
||||
import io.github.znetworkw.znpcservers.commands.exception.CommandException;
|
||||
import io.github.znetworkw.znpcservers.commands.exception.CommandExecuteException;
|
||||
import io.github.znetworkw.znpcservers.commands.exception.CommandPermissionException;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandMap;
|
||||
|
@ -80,6 +83,8 @@ public class Command extends BukkitCommand {
|
|||
e.printStackTrace();
|
||||
} catch (CommandPermissionException e) {
|
||||
sender.sendMessage(ChatColor.RED + "You do not have permission to execute this command.");
|
||||
} catch (CommandException e) {
|
||||
sender.sendMessage(ChatColor.RED + e.getMessage());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
package io.github.znetworkw.znpcservers.commands;
|
||||
|
||||
public class CommandExecuteException extends Exception {
|
||||
public CommandExecuteException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,8 @@
|
|||
package io.github.znetworkw.znpcservers.commands;
|
||||
|
||||
import io.github.znetworkw.znpcservers.commands.exception.CommandException;
|
||||
import io.github.znetworkw.znpcservers.commands.exception.CommandExecuteException;
|
||||
import io.github.znetworkw.znpcservers.commands.exception.CommandPermissionException;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
@ -15,14 +18,17 @@ public class CommandInvoker {
|
|||
this.permission = permission;
|
||||
}
|
||||
|
||||
public void execute(CommandSender sender, Object command) throws CommandPermissionException, CommandExecuteException {
|
||||
if (!(sender instanceof Player) && this.permission.length() == 0) {
|
||||
throw new CommandPermissionException("Only players may execute this command.");
|
||||
}
|
||||
if (this.permission.length() > 0 && !sender.getCommandSender().hasPermission(this.permission)) {
|
||||
public void execute(CommandSender sender, Object command) throws CommandException {
|
||||
// Check if the sender is not a player
|
||||
if (!(sender instanceof Player player))
|
||||
throw new CommandException("Only players may execute this command.");
|
||||
|
||||
// Check if the permission is not empty and the player does not have the permission
|
||||
if (this.permission.length() > 0 && !player.hasPermission(this.permission))
|
||||
throw new CommandPermissionException("You cannot execute this command.");
|
||||
}
|
||||
|
||||
try {
|
||||
// Command execution
|
||||
this.commandMethod.invoke(this.command, sender, command);
|
||||
} catch (IllegalAccessException | java.lang.reflect.InvocationTargetException e) {
|
||||
throw new CommandExecuteException(e.getMessage(), e.getCause());
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
package io.github.znetworkw.znpcservers.commands;
|
||||
|
||||
public class CommandNotFoundException extends Exception {
|
||||
public CommandNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
package io.github.znetworkw.znpcservers.commands;
|
||||
|
||||
public class CommandPermissionException extends Exception {
|
||||
public CommandPermissionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package io.github.znetworkw.znpcservers.commands.exception;
|
||||
|
||||
/**
|
||||
* @author xCodiq - 20/04/2023
|
||||
*/
|
||||
public class CommandException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CommandException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public CommandException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public CommandException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package io.github.znetworkw.znpcservers.commands.exception;
|
||||
|
||||
public class CommandExecuteException extends CommandException {
|
||||
public CommandExecuteException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package io.github.znetworkw.znpcservers.commands.exception;
|
||||
|
||||
public class CommandNotFoundException extends CommandException {
|
||||
public CommandNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package io.github.znetworkw.znpcservers.commands.exception;
|
||||
|
||||
public class CommandPermissionException extends CommandException {
|
||||
public CommandPermissionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue