Fix permissions further
This commit is contained in:
@@ -111,12 +111,12 @@ public abstract class Command {
|
||||
return Collections.unmodifiableList(contextFilters);
|
||||
}
|
||||
|
||||
public boolean removeContextFilter(IContextFilter contextFilter) {
|
||||
public Command removeContextFilter(IContextFilter contextFilter) {
|
||||
boolean ret = contextFilters.remove(contextFilter);
|
||||
if (ret) {
|
||||
computeContextFilterPostParameterIndex();
|
||||
}
|
||||
return ret;
|
||||
return this;
|
||||
}
|
||||
|
||||
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.Parameter;
|
||||
import io.dico.dicore.command.parameter.type.ParameterType;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public abstract class ExtendedCommand<T extends ExtendedCommand<T>> extends Command {
|
||||
protected final boolean modifiable;
|
||||
protected boolean modifiable;
|
||||
|
||||
public ExtendedCommand() {
|
||||
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);
|
||||
}
|
||||
|
||||
@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
|
||||
public T requiredParameters(int 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) {
|
||||
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;
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ public class PermissionContextFilter implements IContextFilter {
|
||||
doFilter(context, permission);
|
||||
}
|
||||
|
||||
private String getInheritedPermission(String[] components) {
|
||||
public String getInheritedPermission(String[] components) {
|
||||
int insertedAmount = components.length;
|
||||
String[] currentComponents = permissionComponents;
|
||||
int currentAmount = currentComponents.length;
|
||||
@@ -80,7 +80,7 @@ public class PermissionContextFilter implements IContextFilter {
|
||||
|
||||
@Override
|
||||
public void filterSubContext(ExecutionContext subContext, String... path) throws CommandException {
|
||||
if (permissionComponents != null) {
|
||||
if (isInheritable()) {
|
||||
doFilter(subContext, getInheritedPermission(path));
|
||||
}
|
||||
}
|
||||
@@ -90,4 +90,20 @@ public class PermissionContextFilter implements IContextFilter {
|
||||
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;
|
||||
|
||||
import io.dico.dicore.command.Command;
|
||||
import io.dico.dicore.command.CommandException;
|
||||
import io.dico.dicore.command.ExecutionContext;
|
||||
import io.dico.dicore.command.IContextFilter;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public class DefaultGroupCommand extends Command {
|
||||
private static final DefaultGroupCommand instance = new DefaultGroupCommand();
|
||||
public class DefaultGroupCommand extends PredefinedCommand<DefaultGroupCommand> {
|
||||
private static final DefaultGroupCommand instance = new DefaultGroupCommand(false);
|
||||
|
||||
public static DefaultGroupCommand getInstance() {
|
||||
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);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ public abstract class PredefinedCommand<T extends PredefinedCommand<T>> extends
|
||||
|
||||
static {
|
||||
registerPredefinedCommandGenerator("help", HelpCommand::registerAsChild);
|
||||
//noinspection StaticInitializerReferencesSubClass
|
||||
registerPredefinedCommandGenerator("syntax", SyntaxCommand::registerAsChild);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package io.dico.parcels2.command
|
||||
|
||||
import io.dico.dicore.command.CommandException
|
||||
import io.dico.dicore.command.EMessageType
|
||||
import io.dico.dicore.command.ExecutionContext
|
||||
import io.dico.dicore.command.Validate
|
||||
import io.dico.dicore.command.*
|
||||
import io.dico.dicore.command.IContextFilter.Priority.*
|
||||
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.Privilege
|
||||
import io.dico.parcels2.blockvisitor.RegionTraverser
|
||||
@@ -95,4 +95,44 @@ class CommandsDebug(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) {
|
||||
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)
|
||||
}.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)
|
||||
config()
|
||||
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)
|
||||
config()
|
||||
parent()
|
||||
|
||||
Reference in New Issue
Block a user