Command annotations added instead of methods, improved channels #31

Merged
NorbiPeti merged 9 commits from dev into master 2017-05-15 00:33:22 +00:00
6 changed files with 75 additions and 10 deletions
Showing only changes of commit bcf8cc4e99 - Show all commits

View file

@ -5,11 +5,9 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.PlayerCommandBase;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase;
@ -76,10 +74,6 @@ public class CommandCaller implements CommandExecutor {
sender.sendMessage("§cYou need to be a mod to use this command.");
return true;
}
if (cmd instanceof PlayerCommandBase && !(sender instanceof Player)) {
sender.sendMessage("§cOnly ingame players can use this command.");
return true;
}
final String[] cmdargs = args.length > 0 ? Arrays.copyOfRange(args, args.length - argc, args.length) : args;
try {
if (!cmd.OnCommand(sender, alias, cmdargs)) {

View file

@ -0,0 +1,30 @@
package buttondevteam.lib.chat;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import buttondevteam.lib.TBMCCoreAPI;
public abstract class OptionallyPlayerCommandBase extends TBMCCommandBase {
public boolean OnCommand(Player player, String alias, String[] args) {
if (getClass().isAnnotationPresent(OptionallyPlayerCommandClass.class)
&& getClass().getAnnotation(OptionallyPlayerCommandClass.class).playerOnly())
TBMCCoreAPI.SendException("Error while executing command " + getClass().getSimpleName() + "!",
new Exception(
"The PlayerCommand annotation is present and set to playerOnly but the Player overload isn't overriden!"));
return true;
}
@Override
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
if (sender instanceof Player)
return OnCommand((Player) sender, alias, args);
if (!getClass().isAnnotationPresent(OptionallyPlayerCommandClass.class)
|| !getClass().getAnnotation(OptionallyPlayerCommandClass.class).playerOnly())
TBMCCoreAPI.SendException("Error while executing command " + getClass().getSimpleName() + "!",
new Exception(
"Command class doesn't override the CommandSender overload and no PlayerCommandClass annotation is present or playerOnly is false!"));
sender.sendMessage("§cYou need to be a player to use this command.");
return true;
}
}

View file

@ -0,0 +1,20 @@
package buttondevteam.lib.chat;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Only needed to use with {@link OptionallyPlayerCommandBase} command classes
*
* @author NorbiPeti
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface OptionallyPlayerCommandClass {
public boolean playerOnly();
}

View file

@ -7,7 +7,7 @@ public abstract class PlayerCommandBase extends TBMCCommandBase {
public abstract boolean OnCommand(Player player, String alias, String[] args);
@Override
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
public final boolean OnCommand(CommandSender sender, String alias, String[] args) {
if (sender instanceof Player)
return OnCommand((Player) sender, alias, args);
sender.sendMessage("§cYou need to be a player to use this command.");

View file

@ -61,7 +61,7 @@ public class TBMCChatAPI {
};
for (TBMCCommandBase cmd : TBMCChatAPI.GetCommands().values()) {
if (cmd.GetCommandPath().startsWith(command + " ")) {
if (cmd instanceof PlayerCommandBase && !(sender instanceof Player))
if (cmd.isPlayerOnly() && !(sender instanceof Player))
continue;
if (cmd.getClass().getAnnotation(CommandClass.class).modOnly()
&& !MainPlugin.permission.has(sender, "tbmc.admin"))

View file

@ -1,5 +1,7 @@
package buttondevteam.lib.chat;
import java.util.function.Function;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
@ -30,8 +32,19 @@ public abstract class TBMCCommandBase {
public final String GetCommandPath() {
if (!getClass().isAnnotationPresent(CommandClass.class))
throw new RuntimeException("No @Command annotation on command class " + getClass().getSimpleName() + "!");
String path = getClass().getAnnotation(CommandClass.class).path();
return path.length() == 0 ? getClass().getSimpleName().toLowerCase().replace("command", "") : path;
Function<Class<?>, String> getFromClass = cl -> getClass().getSimpleName().toLowerCase()
.replace("commandbase", "").replace("command", "");
String path = getClass().getAnnotation(CommandClass.class).path(), prevpath = path; // TODO: Check if annotation exists (No @Inherited?)
for (Class<?> cl = getClass().getSuperclass(); cl != null
&& !cl.getName().equals(TBMCCommandBase.class.getName()); cl = cl.getSuperclass()) {
//com.sun.xml.internal.bind.v2.TODO.prototype();
String newpath = cl.getAnnotation(CommandClass.class).path();
if (newpath.length() == 0)
newpath = getFromClass.apply(cl);
if (!newpath.equals(prevpath))
path = (prevpath = newpath) + " " + path;
}
return path.length() == 0 ? getFromClass.apply(getClass()) : path;
}
Plugin plugin; // Used By TBMCChatAPI
@ -39,4 +52,12 @@ public abstract class TBMCCommandBase {
public final Plugin getPlugin() { // Used by CommandCaller (ButtonChat)
return plugin;
}
public final boolean isPlayerOnly() {
return this instanceof PlayerCommandBase ? true
: this instanceof OptionallyPlayerCommandBase
? getClass().isAnnotationPresent(OptionallyPlayerCommandClass.class)
? getClass().getAnnotation(OptionallyPlayerCommandClass.class).playerOnly() : true
: false;
}
}