mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-12 10:28:35 +00:00
Condense some command packages
This commit is contained in:
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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.internal.command.exception;
|
||||
|
||||
|
||||
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;
|
||||
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* 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.internal.command.exception;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.internal.command.exception;
|
||||
|
||||
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 {
|
||||
|
||||
}
|
@ -0,0 +1,179 @@
|
||||
/*
|
||||
* 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.internal.command.exception;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.sk89q.worldedit.DisallowedItemException;
|
||||
import com.sk89q.worldedit.EmptyClipboardException;
|
||||
import com.sk89q.worldedit.IncompleteRegionException;
|
||||
import com.sk89q.worldedit.InvalidItemException;
|
||||
import com.sk89q.worldedit.MaxBrushRadiusException;
|
||||
import com.sk89q.worldedit.MaxChangedBlocksException;
|
||||
import com.sk89q.worldedit.MaxRadiusException;
|
||||
import com.sk89q.worldedit.UnknownDirectionException;
|
||||
import com.sk89q.worldedit.UnknownItemException;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.WorldEditException;
|
||||
import com.sk89q.worldedit.command.InsufficientArgumentsException;
|
||||
import com.sk89q.worldedit.command.tool.InvalidToolBindException;
|
||||
import com.sk89q.worldedit.internal.expression.ExpressionException;
|
||||
import com.sk89q.worldedit.regions.RegionOperationException;
|
||||
import com.sk89q.worldedit.util.formatting.text.TextComponent;
|
||||
import com.sk89q.worldedit.util.io.file.FileSelectionAbortedException;
|
||||
import com.sk89q.worldedit.util.io.file.FilenameResolutionException;
|
||||
import com.sk89q.worldedit.util.io.file.InvalidFilenameException;
|
||||
import org.enginehub.piston.exception.CommandException;
|
||||
import org.enginehub.piston.exception.UsageException;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* converts WorldEdit exceptions and converts them into {@link CommandException}s.
|
||||
*/
|
||||
public class WorldEditExceptionConverter extends ExceptionConverterHelper {
|
||||
|
||||
private static final Pattern numberFormat = Pattern.compile("^For input string: \"(.*)\"$");
|
||||
private final WorldEdit worldEdit;
|
||||
|
||||
public WorldEditExceptionConverter(WorldEdit worldEdit) {
|
||||
checkNotNull(worldEdit);
|
||||
this.worldEdit = worldEdit;
|
||||
}
|
||||
|
||||
private CommandException newCommandException(String message, Throwable cause) {
|
||||
return new CommandException(TextComponent.of(message), cause, ImmutableList.of());
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(NumberFormatException e) throws CommandException {
|
||||
final Matcher matcher = numberFormat.matcher(e.getMessage());
|
||||
|
||||
if (matcher.matches()) {
|
||||
throw newCommandException("Number expected; string \"" + matcher.group(1)
|
||||
+ "\" given.", e);
|
||||
} else {
|
||||
throw newCommandException("Number expected; string given.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(IncompleteRegionException e) throws CommandException {
|
||||
throw newCommandException("Make a region selection first.", e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(UnknownItemException e) throws CommandException {
|
||||
throw newCommandException("Block name '" + e.getID() + "' was not recognized.", e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(InvalidItemException e) throws CommandException {
|
||||
throw newCommandException(e.getMessage(), e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(DisallowedItemException e) throws CommandException {
|
||||
throw newCommandException("Block '" + e.getID()
|
||||
+ "' not allowed (see WorldEdit configuration).", e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(MaxChangedBlocksException e) throws CommandException {
|
||||
throw newCommandException("Max blocks changed in an operation reached ("
|
||||
+ e.getBlockLimit() + ").", e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(MaxBrushRadiusException e) throws CommandException {
|
||||
throw newCommandException("Maximum brush radius (in configuration): " + worldEdit.getConfiguration().maxBrushRadius, e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(MaxRadiusException e) throws CommandException {
|
||||
throw newCommandException("Maximum radius (in configuration): " + worldEdit.getConfiguration().maxRadius, e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(UnknownDirectionException e) throws CommandException {
|
||||
throw newCommandException("Unknown direction: " + e.getDirection(), e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(InsufficientArgumentsException e) throws CommandException {
|
||||
throw newCommandException(e.getMessage(), e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(RegionOperationException e) throws CommandException {
|
||||
throw newCommandException(e.getMessage(), e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(ExpressionException e) throws CommandException {
|
||||
throw newCommandException(e.getMessage(), e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(EmptyClipboardException e) throws CommandException {
|
||||
throw newCommandException("Your clipboard is empty. Use //copy first.", e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(InvalidFilenameException e) throws CommandException {
|
||||
throw newCommandException("Filename '" + e.getFilename() + "' invalid: "
|
||||
+ e.getMessage(), e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(FilenameResolutionException e) throws CommandException {
|
||||
throw newCommandException(
|
||||
"File '" + e.getFilename() + "' resolution error: " + e.getMessage(), e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(InvalidToolBindException e) throws CommandException {
|
||||
throw newCommandException("Can't bind tool to " + e.getItemType().getName() + ": " + e.getMessage(), e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(FileSelectionAbortedException e) throws CommandException {
|
||||
throw newCommandException("File selection aborted.", e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(WorldEditException e) throws CommandException {
|
||||
throw newCommandException(e.getMessage(), e);
|
||||
}
|
||||
|
||||
@ExceptionMatch
|
||||
public void convert(IllegalArgumentException e) throws CommandException {
|
||||
throw newCommandException(e.getMessage(), e);
|
||||
}
|
||||
|
||||
// Prevent investigation into UsageExceptions
|
||||
@ExceptionMatch
|
||||
public void convert(UsageException e) throws CommandException {
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user