Condense some command packages

This commit is contained in:
Kenzie Togami
2019-04-30 15:03:18 -07:00
parent 743d7f08ab
commit fb4fb980e0
15 changed files with 20 additions and 23 deletions

View File

@ -1,104 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command;
import java.util.stream.Stream;
public class CommandArgParser {
private enum State {
NORMAL,
QUOTE
}
private final Stream.Builder<String> args = Stream.builder();
private final StringBuilder currentArg = new StringBuilder();
private final String input;
private int index = 0;
private State state = State.NORMAL;
public CommandArgParser(String input) {
this.input = input;
}
public Stream<String> parseArgs() {
for (; index < input.length(); index++) {
char c = input.charAt(index);
switch (state) {
case NORMAL:
handleNormal(c);
break;
case QUOTE:
handleQuote(c);
}
}
finishArg(true);
return args.build();
}
private void handleNormal(char c) {
switch (c) {
case '"':
state = State.QUOTE;
break;
case ' ':
finishArg(true);
break;
case '\\':
if (index + 1 < input.length()) {
index++;
}
appendChar(input.charAt(index));
break;
default:
appendChar(c);
}
}
private void handleQuote(char c) {
switch (c) {
case '"':
state = State.NORMAL;
finishArg(false);
break;
case '\\':
if (index + 1 < input.length()) {
index++;
}
appendChar(input.charAt(index));
break;
default:
appendChar(c);
}
}
private void finishArg(boolean requireText) {
if (currentArg.length() == 0 && requireText) {
return;
}
args.add(currentArg.toString());
currentArg.setLength(0);
}
private void appendChar(char c) {
currentArg.append(c);
}
}

View File

@ -1,54 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command;
import com.google.common.collect.ImmutableList;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
import com.sk89q.worldedit.internal.command.CommandLoggingHandler;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.gen.CommandCallListener;
import org.enginehub.piston.gen.CommandRegistration;
import java.util.List;
import java.util.logging.Logger;
public class CommandRegistrationHandler {
private static final CommandPermissionsConditionGenerator PERM_GEN = new CommandPermissionsConditionGenerator();
private final List<CommandCallListener> callListeners;
public CommandRegistrationHandler(List<CommandCallListener> callListeners) {
this.callListeners = ImmutableList.copyOf(callListeners);
}
public <CI> void register(CommandManager manager, CommandRegistration<CI> registration, CI instance) {
registration.containerInstance(instance)
.commandManager(manager)
.listeners(callListeners);
if (registration instanceof CommandPermissionsConditionGenerator.Registration) {
((CommandPermissionsConditionGenerator.Registration) registration).commandPermissionsConditionGenerator(
PERM_GEN
);
}
registration.build();
}
}

View File

@ -1,53 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command;
import com.sk89q.worldedit.extension.platform.PlatformCommandManager;
import org.enginehub.piston.Command;
import org.enginehub.piston.part.SubCommandPart;
import java.util.Comparator;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class CommandUtil {
public static Map<String, Command> getSubCommands(Command currentCommand) {
return currentCommand.getParts().stream()
.filter(p -> p instanceof SubCommandPart)
.flatMap(p -> ((SubCommandPart) p).getCommands().stream())
.collect(Collectors.toMap(Command::getName, Function.identity()));
}
private static String clean(String input) {
return PlatformCommandManager.COMMAND_CLEAN_PATTERN.matcher(input).replaceAll("");
}
private static final Comparator<Command> BY_CLEAN_NAME =
Comparator.comparing(c -> clean(c.getName()));
public static Comparator<Command> byCleanName() {
return BY_CLEAN_NAME;
}
private CommandUtil() {
}
}

View File

@ -1,52 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command.parametric;
import org.enginehub.piston.exception.CommandException;
import org.enginehub.piston.exception.CommandExecutionException;
/**
* Used to convert a recognized {@link Throwable} into an appropriate
* {@link CommandException}.
*
* <p>Methods may throw relevant exceptions that are not caught by the command manager,
* but translate into reasonable exceptions for an application. However, unknown exceptions are
* normally simply wrapped in a {@link CommandExecutionException} and bubbled up. Only
* normal {@link CommandException}s will be printed correctly, so a converter translates
* one of these unknown exceptions into an appropriate {@link CommandException}.</p>
*
* <p>This also allows the code calling the command to not need be aware of these
* application-specific exceptions, as they will all be converted to
* {@link CommandException}s that are handled normally.</p>
*/
public interface ExceptionConverter {
/**
* Attempt to convert the given throwable into a {@link CommandException}.
*
* <p>If the exception is not recognized, then nothing should be thrown.</p>
*
* @param t the throwable
* @throws CommandException a command exception
*/
void convert(Throwable t) throws CommandException;
}

View File

@ -1,111 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command.parametric;
import com.google.common.collect.ImmutableList;
import com.sk89q.minecraft.util.commands.WrappedCommandException;
import org.enginehub.piston.exception.CommandException;
import org.enginehub.piston.exception.CommandExecutionException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* An implementation of an {@link ExceptionConverter} that automatically calls
* the correct method defined on this object.
*
* <p>Only public methods will be used. Methods will be called in order of decreasing
* levels of inheritance (between classes where one inherits the other). For two
* different inheritance branches, the order between them is undefined.</p>
*/
public abstract class ExceptionConverterHelper implements ExceptionConverter {
private final List<ExceptionHandler> handlers;
@SuppressWarnings("unchecked")
public ExceptionConverterHelper() {
List<ExceptionHandler> handlers = new ArrayList<>();
for (Method method : this.getClass().getMethods()) {
if (method.getAnnotation(ExceptionMatch.class) == null) {
continue;
}
Class<?>[] parameters = method.getParameterTypes();
if (parameters.length == 1) {
Class<?> cls = parameters[0];
if (Throwable.class.isAssignableFrom(cls)) {
handlers.add(new ExceptionHandler(
(Class<? extends Throwable>) cls, method));
}
}
}
Collections.sort(handlers);
this.handlers = handlers;
}
@Override
public void convert(Throwable t) throws CommandException {
Class<?> throwableClass = t.getClass();
for (ExceptionHandler handler : handlers) {
if (handler.cls.isAssignableFrom(throwableClass)) {
try {
handler.method.invoke(this, t);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof CommandException) {
throw (CommandException) e.getCause();
}
throw new CommandExecutionException(e, ImmutableList.of());
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new CommandExecutionException(e, ImmutableList.of());
}
}
}
}
private static class ExceptionHandler implements Comparable<ExceptionHandler> {
final Class<? extends Throwable> cls;
final Method method;
private ExceptionHandler(Class<? extends Throwable> cls, Method method) {
this.cls = cls;
this.method = method;
}
@Override
public int compareTo(ExceptionHandler o) {
if (cls.equals(o.cls)) {
return 0;
} else if (cls.isAssignableFrom(o.cls)) {
return 1;
} else if (o.cls.isAssignableFrom(cls)) {
return -1;
} else {
return cls.getCanonicalName().compareTo(o.cls.getCanonicalName());
}
}
}
}

View File

@ -1,34 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.command.parametric;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Denotes a match of an exception.
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExceptionMatch {
}

View File

@ -30,8 +30,8 @@ import java.util.Map;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.sk89q.worldedit.util.command.CommandUtil.byCleanName;
import static com.sk89q.worldedit.util.command.CommandUtil.getSubCommands;
import static com.sk89q.worldedit.internal.command.CommandUtil.byCleanName;
import static com.sk89q.worldedit.internal.command.CommandUtil.getSubCommands;
/**
* A box to describe usage of a command.

View File

@ -24,7 +24,7 @@ import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.sk89q.worldedit.command.util.AsyncCommandHelper;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.util.command.parametric.ExceptionConverter;
import com.sk89q.worldedit.internal.command.exception.ExceptionConverter;
import com.sk89q.worldedit.util.task.Supervisor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;