Probably mostly added support for ButtonChat cmds

It's a whole different story
This commit is contained in:
Norbi Peti 2017-04-23 02:12:56 +02:00
parent 672fab2d1b
commit bcf8cc4e99
6 changed files with 75 additions and 10 deletions

View file

@ -5,11 +5,9 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.PlayerCommandBase;
import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase; 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."); sender.sendMessage("§cYou need to be a mod to use this command.");
return true; 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; final String[] cmdargs = args.length > 0 ? Arrays.copyOfRange(args, args.length - argc, args.length) : args;
try { try {
if (!cmd.OnCommand(sender, alias, cmdargs)) { 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); public abstract boolean OnCommand(Player player, String alias, String[] args);
@Override @Override
public boolean OnCommand(CommandSender sender, String alias, String[] args) { public final boolean OnCommand(CommandSender sender, String alias, String[] args) {
if (sender instanceof Player) if (sender instanceof Player)
return OnCommand((Player) sender, alias, args); return OnCommand((Player) sender, alias, args);
sender.sendMessage("§cYou need to be a player to use this command."); 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()) { for (TBMCCommandBase cmd : TBMCChatAPI.GetCommands().values()) {
if (cmd.GetCommandPath().startsWith(command + " ")) { if (cmd.GetCommandPath().startsWith(command + " ")) {
if (cmd instanceof PlayerCommandBase && !(sender instanceof Player)) if (cmd.isPlayerOnly() && !(sender instanceof Player))
continue; continue;
if (cmd.getClass().getAnnotation(CommandClass.class).modOnly() if (cmd.getClass().getAnnotation(CommandClass.class).modOnly()
&& !MainPlugin.permission.has(sender, "tbmc.admin")) && !MainPlugin.permission.has(sender, "tbmc.admin"))

View file

@ -1,5 +1,7 @@
package buttondevteam.lib.chat; package buttondevteam.lib.chat;
import java.util.function.Function;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -30,8 +32,19 @@ public abstract class TBMCCommandBase {
public final String GetCommandPath() { public final String GetCommandPath() {
if (!getClass().isAnnotationPresent(CommandClass.class)) if (!getClass().isAnnotationPresent(CommandClass.class))
throw new RuntimeException("No @Command annotation on command class " + getClass().getSimpleName() + "!"); throw new RuntimeException("No @Command annotation on command class " + getClass().getSimpleName() + "!");
String path = getClass().getAnnotation(CommandClass.class).path(); Function<Class<?>, String> getFromClass = cl -> getClass().getSimpleName().toLowerCase()
return path.length() == 0 ? getClass().getSimpleName().toLowerCase().replace("command", "") : path; .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 Plugin plugin; // Used By TBMCChatAPI
@ -39,4 +52,12 @@ public abstract class TBMCCommandBase {
public final Plugin getPlugin() { // Used by CommandCaller (ButtonChat) public final Plugin getPlugin() { // Used by CommandCaller (ButtonChat)
return plugin; 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;
}
} }