Fix permissions further
This commit is contained in:
@@ -111,12 +111,12 @@ public abstract class Command {
|
|||||||
return Collections.unmodifiableList(contextFilters);
|
return Collections.unmodifiableList(contextFilters);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean removeContextFilter(IContextFilter contextFilter) {
|
public Command removeContextFilter(IContextFilter contextFilter) {
|
||||||
boolean ret = contextFilters.remove(contextFilter);
|
boolean ret = contextFilters.remove(contextFilter);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
computeContextFilterPostParameterIndex();
|
computeContextFilterPostParameterIndex();
|
||||||
}
|
}
|
||||||
return ret;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void computeContextFilterPostParameterIndex() {
|
private void computeContextFilterPostParameterIndex() {
|
||||||
|
|||||||
@@ -2,10 +2,11 @@ package io.dico.dicore.command;
|
|||||||
|
|
||||||
import io.dico.dicore.command.parameter.IArgumentPreProcessor;
|
import io.dico.dicore.command.parameter.IArgumentPreProcessor;
|
||||||
import io.dico.dicore.command.parameter.Parameter;
|
import io.dico.dicore.command.parameter.Parameter;
|
||||||
|
import io.dico.dicore.command.parameter.type.ParameterType;
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public abstract class ExtendedCommand<T extends ExtendedCommand<T>> extends Command {
|
public abstract class ExtendedCommand<T extends ExtendedCommand<T>> extends Command {
|
||||||
protected final boolean modifiable;
|
protected boolean modifiable;
|
||||||
|
|
||||||
public ExtendedCommand() {
|
public ExtendedCommand() {
|
||||||
this(true);
|
this(true);
|
||||||
@@ -24,6 +25,16 @@ public abstract class ExtendedCommand<T extends ExtendedCommand<T>> extends Comm
|
|||||||
return modifiable ? (T) super.addParameter(parameter) : newModifiableInstance().addParameter(parameter);
|
return modifiable ? (T) super.addParameter(parameter) : newModifiableInstance().addParameter(parameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T addContextFilter(IContextFilter contextFilter) {
|
||||||
|
return modifiable ? (T) super.addContextFilter(contextFilter) : newModifiableInstance().addContextFilter(contextFilter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T removeContextFilter(IContextFilter contextFilter) {
|
||||||
|
return modifiable ? (T) super.removeContextFilter(contextFilter) : newModifiableInstance().removeContextFilter(contextFilter);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T requiredParameters(int requiredParameters) {
|
public T requiredParameters(int requiredParameters) {
|
||||||
return modifiable ? (T) super.requiredParameters(requiredParameters) : newModifiableInstance().requiredParameters(requiredParameters);
|
return modifiable ? (T) super.requiredParameters(requiredParameters) : newModifiableInstance().requiredParameters(requiredParameters);
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ public abstract class InheritingContextFilter implements IContextFilter {
|
|||||||
|
|
||||||
private static String[] addParent(String[] path, String parent) {
|
private static String[] addParent(String[] path, String parent) {
|
||||||
String[] out = new String[path.length + 1];
|
String[] out = new String[path.length + 1];
|
||||||
System.arraycopy(path, 0, out, 0, path.length);
|
System.arraycopy(path, 0, out, 1, path.length);
|
||||||
out[0] = parent;
|
out[0] = parent;
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ public class PermissionContextFilter implements IContextFilter {
|
|||||||
doFilter(context, permission);
|
doFilter(context, permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getInheritedPermission(String[] components) {
|
public String getInheritedPermission(String[] components) {
|
||||||
int insertedAmount = components.length;
|
int insertedAmount = components.length;
|
||||||
String[] currentComponents = permissionComponents;
|
String[] currentComponents = permissionComponents;
|
||||||
int currentAmount = currentComponents.length;
|
int currentAmount = currentComponents.length;
|
||||||
@@ -80,7 +80,7 @@ public class PermissionContextFilter implements IContextFilter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void filterSubContext(ExecutionContext subContext, String... path) throws CommandException {
|
public void filterSubContext(ExecutionContext subContext, String... path) throws CommandException {
|
||||||
if (permissionComponents != null) {
|
if (isInheritable()) {
|
||||||
doFilter(subContext, getInheritedPermission(path));
|
doFilter(subContext, getInheritedPermission(path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -90,4 +90,20 @@ public class PermissionContextFilter implements IContextFilter {
|
|||||||
return Priority.PERMISSION;
|
return Priority.PERMISSION;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isInheritable() {
|
||||||
|
return permissionComponents != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPermission() {
|
||||||
|
return permission;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getComponentInsertionIndex() {
|
||||||
|
return componentInsertionIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFailMessage() {
|
||||||
|
return failMessage;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,29 @@
|
|||||||
package io.dico.dicore.command.predef;
|
package io.dico.dicore.command.predef;
|
||||||
|
|
||||||
import io.dico.dicore.command.Command;
|
|
||||||
import io.dico.dicore.command.CommandException;
|
import io.dico.dicore.command.CommandException;
|
||||||
import io.dico.dicore.command.ExecutionContext;
|
import io.dico.dicore.command.ExecutionContext;
|
||||||
|
import io.dico.dicore.command.IContextFilter;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
public class DefaultGroupCommand extends Command {
|
public class DefaultGroupCommand extends PredefinedCommand<DefaultGroupCommand> {
|
||||||
private static final DefaultGroupCommand instance = new DefaultGroupCommand();
|
private static final DefaultGroupCommand instance = new DefaultGroupCommand(false);
|
||||||
|
|
||||||
public static DefaultGroupCommand getInstance() {
|
public static DefaultGroupCommand getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DefaultGroupCommand() {
|
private DefaultGroupCommand(boolean modifiable) {
|
||||||
|
addContextFilter(IContextFilter.INHERIT_PERMISSIONS);
|
||||||
|
this.modifiable = modifiable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public String execute(CommandSender sender, ExecutionContext context) throws CommandException {
|
@Override
|
||||||
|
protected DefaultGroupCommand newModifiableInstance() {
|
||||||
|
return new DefaultGroupCommand(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String execute(CommandSender sender, ExecutionContext context) throws CommandException {
|
||||||
context.getAddress().getChatController().sendHelpMessage(sender, context, context.getAddress(), 1);
|
context.getAddress().getChatController().sendHelpMessage(sender, context, context.getAddress(), 1);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ public abstract class PredefinedCommand<T extends PredefinedCommand<T>> extends
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
registerPredefinedCommandGenerator("help", HelpCommand::registerAsChild);
|
registerPredefinedCommandGenerator("help", HelpCommand::registerAsChild);
|
||||||
|
//noinspection StaticInitializerReferencesSubClass
|
||||||
registerPredefinedCommandGenerator("syntax", SyntaxCommand::registerAsChild);
|
registerPredefinedCommandGenerator("syntax", SyntaxCommand::registerAsChild);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package io.dico.parcels2.command
|
package io.dico.parcels2.command
|
||||||
|
|
||||||
import io.dico.dicore.command.CommandException
|
import io.dico.dicore.command.*
|
||||||
import io.dico.dicore.command.EMessageType
|
import io.dico.dicore.command.IContextFilter.Priority.*
|
||||||
import io.dico.dicore.command.ExecutionContext
|
|
||||||
import io.dico.dicore.command.Validate
|
|
||||||
import io.dico.dicore.command.annotation.Cmd
|
import io.dico.dicore.command.annotation.Cmd
|
||||||
|
import io.dico.dicore.command.annotation.PreprocessArgs
|
||||||
|
import io.dico.dicore.command.parameter.ArgumentBuffer
|
||||||
import io.dico.parcels2.ParcelsPlugin
|
import io.dico.parcels2.ParcelsPlugin
|
||||||
import io.dico.parcels2.Privilege
|
import io.dico.parcels2.Privilege
|
||||||
import io.dico.parcels2.blockvisitor.RegionTraverser
|
import io.dico.parcels2.blockvisitor.RegionTraverser
|
||||||
@@ -95,4 +95,44 @@ class CommandsDebug(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) {
|
|||||||
return target.hasPermission(permission).toString()
|
return target.hasPermission(permission).toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Cmd("message")
|
||||||
|
@PreprocessArgs
|
||||||
|
fun cmdMessage(sender: CommandSender, message: String): Any? {
|
||||||
|
sender.sendMessage(message)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
@Cmd("permissions")
|
||||||
|
fun cmdPermissions(context: ExecutionContext, vararg address: String): Any? {
|
||||||
|
val target = context.address.dispatcherForTree.getDeepChild(ArgumentBuffer(address))
|
||||||
|
Validate.isTrue(target.depth == address.size && target.hasCommand(), "Not found: /${address.joinToString(separator = " ")}")
|
||||||
|
|
||||||
|
val permissions = getPermissionsOf(target)
|
||||||
|
return permissions.joinToString(separator = "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getPermissionsOf(address: ICommandAddress,
|
||||||
|
path: Array<String> = emptyArray(),
|
||||||
|
result: MutableList<String> = mutableListOf()): List<String> {
|
||||||
|
val command = address.command ?: return result
|
||||||
|
|
||||||
|
var inherited = false
|
||||||
|
for (filter in command.contextFilters) {
|
||||||
|
when (filter) {
|
||||||
|
is PermissionContextFilter -> {
|
||||||
|
if (path.isEmpty()) result.add(filter.permission)
|
||||||
|
else if (filter.isInheritable) result.add(filter.getInheritedPermission(path))
|
||||||
|
}
|
||||||
|
is InheritingContextFilter -> {
|
||||||
|
if (filter.priority == PERMISSION && address.hasParent() && !inherited) {
|
||||||
|
inherited = true
|
||||||
|
getPermissionsOf(address.parent, arrayOf(address.mainKey, *path), result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -55,13 +55,13 @@ fun getParcelCommands(plugin: ParcelsPlugin): ICommandDispatcher = CommandBuilde
|
|||||||
generateHelpAndSyntaxCommands(parcelsAddress)
|
generateHelpAndSyntaxCommands(parcelsAddress)
|
||||||
}.getDispatcher()
|
}.getDispatcher()
|
||||||
|
|
||||||
inline fun CommandBuilder.group(name: String, vararg aliases: String, config: CommandBuilder.() -> Unit) {
|
private inline fun CommandBuilder.group(name: String, vararg aliases: String, config: CommandBuilder.() -> Unit) {
|
||||||
group(name, *aliases)
|
group(name, *aliases)
|
||||||
config()
|
config()
|
||||||
parent()
|
parent()
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun CommandBuilder.group(address: ICommandAddress, name: String, vararg aliases: String, config: CommandBuilder.() -> Unit) {
|
private inline fun CommandBuilder.group(address: ICommandAddress, name: String, vararg aliases: String, config: CommandBuilder.() -> Unit) {
|
||||||
group(address, name, *aliases)
|
group(address, name, *aliases)
|
||||||
config()
|
config()
|
||||||
parent()
|
parent()
|
||||||
|
|||||||
Reference in New Issue
Block a user