This commit is contained in:
sky 2020-04-26 15:21:45 +08:00
parent e39981d691
commit 058ff6c141
3 changed files with 45 additions and 1 deletions

View File

@ -2,15 +2,17 @@ package io.izzel.taboolib.module.command;
import io.izzel.taboolib.TabooLibAPI; import io.izzel.taboolib.TabooLibAPI;
import io.izzel.taboolib.module.command.base.BaseCommand; import io.izzel.taboolib.module.command.base.BaseCommand;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.module.command.base.BaseMainCommand; import io.izzel.taboolib.module.command.base.BaseMainCommand;
import io.izzel.taboolib.module.inject.TFunction; import io.izzel.taboolib.module.inject.TFunction;
import io.izzel.taboolib.module.lite.SimpleReflection; import io.izzel.taboolib.module.lite.SimpleReflection;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.util.ArrayUtil; import io.izzel.taboolib.util.ArrayUtil;
import io.izzel.taboolib.util.Files; import io.izzel.taboolib.util.Files;
import io.izzel.taboolib.util.Reflection; import io.izzel.taboolib.util.Reflection;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.*; import org.bukkit.command.*;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.SimplePluginManager; import org.bukkit.plugin.SimplePluginManager;
@ -110,6 +112,21 @@ public class TCommandHandler {
*/ */
public static BaseMainCommand registerCommand(BaseCommand tCommand, String command, BaseMainCommand baseMainCommand, Plugin plugin) { public static BaseMainCommand registerCommand(BaseCommand tCommand, String command, BaseMainCommand baseMainCommand, Plugin plugin) {
if (Bukkit.getPluginCommand(command) == null) { if (Bukkit.getPluginCommand(command) == null) {
String permission = tCommand.permission();
if (tCommand.permissionDefault() == PermissionDefault.TRUE || tCommand.permissionDefault() == PermissionDefault.NOT_OP) {
if (permission == null || permission.isEmpty()) {
permission = plugin.getName().toLowerCase() + ".command.use";
}
if (Bukkit.getPluginManager().getPermission(permission) != null) {
try {
Permission p = new Permission(permission, tCommand.permissionDefault());
Bukkit.getPluginManager().addPermission(p);
Bukkit.getPluginManager().recalculatePermissionDefaults(p);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
registerPluginCommand( registerPluginCommand(
plugin, plugin,
command, command,

View File

@ -1,5 +1,7 @@
package io.izzel.taboolib.module.command.base; package io.izzel.taboolib.module.command.base;
import org.bukkit.permissions.PermissionDefault;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
@ -24,4 +26,6 @@ public @interface BaseCommand {
String usage() default ""; String usage() default "";
String[] aliases() default {}; String[] aliases() default {};
PermissionDefault permissionDefault() default PermissionDefault.OP;
} }

View File

@ -5,6 +5,9 @@ import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.module.command.TCommandHandler; import io.izzel.taboolib.module.command.TCommandHandler;
import io.izzel.taboolib.util.ArrayUtil; import io.izzel.taboolib.util.ArrayUtil;
import io.izzel.taboolib.util.Ref; import io.izzel.taboolib.util.Ref;
import org.bukkit.Bukkit;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.util.ArrayList; import java.util.ArrayList;
@ -33,6 +36,7 @@ public class CommandBuilder {
private boolean forceRegister; private boolean forceRegister;
private boolean build; private boolean build;
private boolean simpleMode; private boolean simpleMode;
private PermissionDefault permissionDefault = PermissionDefault.OP;
CommandBuilder(String command, Plugin plugin) { CommandBuilder(String command, Plugin plugin) {
this.command = command; this.command = command;
@ -87,6 +91,11 @@ public class CommandBuilder {
return this; return this;
} }
public CommandBuilder permissionDefault(PermissionDefault permissionDefault) {
this.permissionDefault = permissionDefault;
return this;
}
public CommandBuilder execute(CompleterCommand completerCommand) { public CommandBuilder execute(CompleterCommand completerCommand) {
this.completerCommand = completerCommand; this.completerCommand = completerCommand;
return this; return this;
@ -109,6 +118,20 @@ public class CommandBuilder {
if (forceRegister) { if (forceRegister) {
TCommandHandler.getKnownCommands().remove(command); TCommandHandler.getKnownCommands().remove(command);
} }
if (permissionDefault == PermissionDefault.TRUE || permissionDefault == PermissionDefault.NOT_OP) {
if (permission == null) {
permission = plugin.getName().toLowerCase() + ".command.use";
}
if (Bukkit.getPluginManager().getPermission(permission) != null) {
try {
Permission p = new Permission(permission, permissionDefault);
Bukkit.getPluginManager().addPermission(p);
Bukkit.getPluginManager().recalculatePermissionDefaults(p);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
TCommandHandler.registerPluginCommand( TCommandHandler.registerPluginCommand(
plugin, plugin,
command, command,