Changed WorldEdit to use the new dispatcher.

This commit is contained in:
Albert Pham
2013-06-18 14:51:42 -07:00
parent 04c31262f7
commit 142f5c8e5c
17 changed files with 882 additions and 381 deletions

View File

@ -0,0 +1,164 @@
// $Id$
package com.sk89q.worldedit.util;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.minecraft.util.commands.Logging;
import com.sk89q.rebar.command.parametric.AbstractInvokeListener;
import com.sk89q.rebar.command.parametric.InvokeHandler;
import com.sk89q.rebar.command.parametric.ParameterData;
import com.sk89q.rebar.command.parametric.ParameterException;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.LogFormat;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEdit;
/**
* Logs called commands to a file.
*/
public class CommandLoggingHandler
extends AbstractInvokeListener
implements InvokeHandler, Closeable {
private static final Logger logger =
Logger.getLogger(CommandLoggingHandler.class.getCanonicalName());
private final WorldEdit worldEdit;
private final LocalConfiguration config;
private final Logger commandLogger =
Logger.getLogger("Minecraft.WorldEdit.CommandLogger");
/**
* Create a new instance.
*
* @param worldEdit WorldEdit instance
* @param config the configuration
*/
public CommandLoggingHandler(WorldEdit worldEdit, LocalConfiguration config) {
this.worldEdit = worldEdit;
this.config = config;
if (!config.logFile.equals("")) {
try {
FileHandler logFileHandler;
File file = new File(config.getWorkingDirectory(), config.logFile);
logFileHandler = new FileHandler(file.getAbsolutePath(), true);
logFileHandler.setFormatter(new LogFormat());
commandLogger.addHandler(logFileHandler);
} catch (IOException e) {
logger.log(Level.WARNING, "Could not use command log file "
+ config.logFile + ": " + e.getMessage());
}
}
}
@Override
public void preProcess(Object object, Method method,
ParameterData[] parameters, CommandContext context)
throws CommandException, ParameterException {
}
@Override
public void preInvoke(Object object, Method method, ParameterData[] parameters,
Object[] args, CommandContext context) throws CommandException {
if (!config.logCommands) {
return;
}
Logging loggingAnnotation = method.getAnnotation(Logging.class);
Logging.LogMode logMode;
StringBuilder builder = new StringBuilder();
if (loggingAnnotation == null) {
logMode = null;
} else {
logMode = loggingAnnotation.value();
}
LocalPlayer sender = context.getLocals().get(LocalPlayer.class);
if (sender == null) {
return;
}
builder.append("WorldEdit: ").append(sender.getName());
if (sender.isPlayer()) {
builder.append(" (in \"" + sender.getWorld().getName() + "\")");
}
builder.append(": ").append(context.getCommand());
if (context.argsLength() > 0) {
builder.append(" ").append(context.getJoinedStrings(0));
}
if (logMode != null && sender.isPlayer()) {
Vector position = sender.getPosition();
LocalSession session = worldEdit.getSession(sender);
switch (logMode) {
case PLACEMENT:
try {
position = session.getPlacementPosition(sender);
} catch (IncompleteRegionException e) {
break;
}
/* FALL-THROUGH */
case POSITION:
builder.append(" - Position: " + position);
break;
case ALL:
builder.append(" - Position: " + position);
/* FALL-THROUGH */
case ORIENTATION_REGION:
builder.append(" - Orientation: "
+ sender.getCardinalDirection().name());
/* FALL-THROUGH */
case REGION:
try {
builder.append(" - Region: ")
.append(session.getSelection(sender.getWorld()));
} catch (IncompleteRegionException e) {
break;
}
break;
}
}
commandLogger.info(builder.toString());
}
@Override
public void postInvoke(Object object, Method method, ParameterData[] parameters,
Object[] args, CommandContext context) throws CommandException {
}
@Override
public InvokeHandler createInvokeHandler() {
return this;
}
@Override
public void close() {
for (Handler h : commandLogger.getHandlers()) {
h.close();
}
}
}

View File

@ -0,0 +1,40 @@
// $Id$
/*
* This file is a part of WorldEdit.
* Copyright (c) sk89q <http://www.sk89q.com>
* Copyright (c) the 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
* (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
* GNU 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;
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.rebar.command.parametric.PermissionsHandler;
import com.sk89q.worldedit.LocalPlayer;
public class CommandPermissionsHandler extends PermissionsHandler {
public CommandPermissionsHandler() {
}
@Override
protected boolean hasPermission(CommandContext context, String permission) {
LocalPlayer sender = context.getLocals().get(LocalPlayer.class);
if (sender == null) {
return true;
} else {
return sender.hasPermission(permission);
}
}
}

View File

@ -0,0 +1,177 @@
// $Id$
/*
* This file is a part of WorldEdit.
* Copyright (c) sk89q <http://www.sk89q.com>
* Copyright (c) the 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
* (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
* GNU 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;
import com.sk89q.rebar.command.parametric.BindingBehavior;
import com.sk89q.rebar.command.parametric.BindingHelper;
import com.sk89q.rebar.command.parametric.BindingMatch;
import com.sk89q.rebar.command.parametric.ParameterException;
import com.sk89q.rebar.command.parametric.ArgumentStack;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.UnknownDirectionException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.annotation.Direction;
import com.sk89q.worldedit.annotation.Selection;
import com.sk89q.worldedit.masks.Mask;
import com.sk89q.worldedit.patterns.Pattern;
import com.sk89q.worldedit.regions.Region;
/**
* Binds standard WorldEdit classes such as {@link LocalPlayer} and {@link LocalSession}.
*/
public class WorldEditBinding extends BindingHelper {
private final WorldEdit worldEdit;
/**
* Create a new instance.
*
* @param worldEdit the WorldEdit instance to bind to
*/
public WorldEditBinding(WorldEdit worldEdit) {
this.worldEdit = worldEdit;
}
/**
* Gets a selection from a {@link ArgumentStack}.
*
* @param context the context
* @param selection the annotation
* @return a selection
* @throws IncompleteRegionException if no selection is available
* @throws ParameterException on other error
*/
@BindingMatch(classifier = Selection.class,
type = Region.class,
behavior = BindingBehavior.PROVIDES)
public Object getSelection(ArgumentStack context, Selection selection)
throws IncompleteRegionException, ParameterException {
LocalPlayer sender = getLocalPlayer(context);
LocalSession session = worldEdit.getSession(sender);
return session.getSelection(sender.getWorld());
}
/**
* Gets an {@link EditSession} from a {@link ArgumentStack}.
*
* @param context the context
* @return an edit session
* @throws ParameterException on other error
*/
@BindingMatch(type = EditSession.class,
behavior = BindingBehavior.PROVIDES)
public EditSession getEditSession(ArgumentStack context) throws ParameterException {
LocalPlayer sender = getLocalPlayer(context);
LocalSession session = worldEdit.getSession(sender);
EditSession editSession = session.createEditSession(sender);
editSession.enableQueue();
context.getContext().getLocals().put(EditSession.class, editSession);
session.tellVersion(sender);
return editSession;
}
/**
* Gets an {@link LocalSession} from a {@link ArgumentStack}.
*
* @param context the context
* @return a local session
* @throws ParameterException on error
*/
@BindingMatch(type = LocalSession.class,
behavior = BindingBehavior.PROVIDES)
public LocalSession getLocalSession(ArgumentStack context) throws ParameterException {
LocalPlayer sender = getLocalPlayer(context);
return worldEdit.getSession(sender);
}
/**
* Gets an {@link LocalPlayer} from a {@link ArgumentStack}.
*
* @param context the context
* @return a local player
* @throws ParameterException on error
*/
@BindingMatch(type = LocalPlayer.class,
behavior = BindingBehavior.PROVIDES)
public LocalPlayer getLocalPlayer(ArgumentStack context) throws ParameterException {
LocalPlayer sender = context.getContext().getLocals().get(LocalPlayer.class);
if (sender == null) {
throw new ParameterException("No player to get a session for");
}
return sender;
}
/**
* Gets an {@link Pattern} from a {@link ArgumentStack}.
*
* @param context the context
* @return a pattern
* @throws ParameterException on error
* @throws WorldEditException on error
*/
@BindingMatch(type = Pattern.class,
behavior = BindingBehavior.CONSUMES,
consumedCount = 1)
public Pattern getPattern(ArgumentStack context)
throws ParameterException, WorldEditException {
return worldEdit.getBlockPattern(getLocalPlayer(context), context.next());
}
/**
* Gets an {@link Mask} from a {@link ArgumentStack}.
*
* @param context the context
* @return a pattern
* @throws ParameterException on error
* @throws WorldEditException on error
*/
@BindingMatch(type = Mask.class,
behavior = BindingBehavior.CONSUMES,
consumedCount = 1)
public Mask getMask(ArgumentStack context)
throws ParameterException, WorldEditException {
return worldEdit.getBlockMask(getLocalPlayer(context),
getLocalSession(context), context.next());
}
/**
* Get a direction from the player.
*
* @param context the context
* @param direction the direction annotation
* @return a pattern
* @throws ParameterException on error
* @throws UnknownDirectionException on an unknown direction
*/
@BindingMatch(classifier = Direction.class,
type = Vector.class,
behavior = BindingBehavior.CONSUMES,
consumedCount = 1)
public Vector getDirection(ArgumentStack context, Direction direction)
throws ParameterException, UnknownDirectionException {
LocalPlayer sender = getLocalPlayer(context);
return worldEdit.getDirection(sender, context.next());
}
}

View File

@ -0,0 +1,163 @@
// $Id$
/*
* This file is a part of WorldEdit.
* Copyright (c) sk89q <http://www.sk89q.com>
* Copyright (c) the 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
* (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
* GNU 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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.rebar.command.parametric.ExceptionConverterHelper;
import com.sk89q.rebar.command.parametric.ExceptionMatch;
import com.sk89q.worldedit.DisallowedItemException;
import com.sk89q.worldedit.EmptyClipboardException;
import com.sk89q.worldedit.FileSelectionAbortedException;
import com.sk89q.worldedit.FilenameResolutionException;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.InvalidFilenameException;
import com.sk89q.worldedit.InvalidItemException;
import com.sk89q.worldedit.InvalidToolBindException;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.MaxRadiusException;
import com.sk89q.worldedit.PlayerNeededException;
import com.sk89q.worldedit.UnknownDirectionException;
import com.sk89q.worldedit.UnknownItemException;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.ItemType;
import com.sk89q.worldedit.commands.InsufficientArgumentsException;
import com.sk89q.worldedit.expression.ExpressionException;
import com.sk89q.worldedit.regions.RegionOperationException;
/**
* 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 LocalConfiguration config;
public WorldEditExceptionConverter(LocalConfiguration config) {
this.config = config;
}
@ExceptionMatch
public void convert(PlayerNeededException e) throws CommandException {
throw new CommandException(e.getMessage());
}
@ExceptionMatch
public void convert(NumberFormatException e) throws CommandException {
final Matcher matcher = numberFormat.matcher(e.getMessage());
if (matcher.matches()) {
throw new CommandException("Number expected; string \"" + matcher.group(1)
+ "\" given.");
} else {
throw new CommandException("Number expected; string given.");
}
}
@ExceptionMatch
public void convert(IncompleteRegionException e) throws CommandException {
throw new CommandException("Make a region selection first.");
}
@ExceptionMatch
public void convert(UnknownItemException e) throws CommandException {
throw new CommandException("Block name '" + e.getID() + "' was not recognized.");
}
@ExceptionMatch
public void convert(InvalidItemException e) throws CommandException {
throw new CommandException(e.getMessage());
}
@ExceptionMatch
public void convert(DisallowedItemException e) throws CommandException {
throw new CommandException("Block '" + e.getID()
+ "' not allowed (see WorldEdit configuration).");
}
@ExceptionMatch
public void convert(MaxChangedBlocksException e) throws CommandException {
throw new CommandException("Max blocks changed in an operation reached ("
+ e.getBlockLimit() + ").");
}
@ExceptionMatch
public void convert(MaxRadiusException e) throws CommandException {
throw new CommandException("Maximum radius: " + config.maxRadius);
}
@ExceptionMatch
public void convert(UnknownDirectionException e) throws CommandException {
throw new CommandException("Unknown direction: " + e.getDirection());
}
@ExceptionMatch
public void convert(InsufficientArgumentsException e) throws CommandException {
throw new CommandException(e.getMessage());
}
@ExceptionMatch
public void convert(RegionOperationException e) throws CommandException {
throw new CommandException(e.getMessage());
}
@ExceptionMatch
public void convert(ExpressionException e) throws CommandException {
throw new CommandException(e.getMessage());
}
@ExceptionMatch
public void convert(EmptyClipboardException e) throws CommandException {
throw new CommandException("Your clipboard is empty. Use //copy first.");
}
@ExceptionMatch
public void convert(InvalidFilenameException e) throws CommandException {
throw new CommandException("Filename '" + e.getFilename() + "' invalid: "
+ e.getMessage());
}
@ExceptionMatch
public void convert(FilenameResolutionException e) throws CommandException {
throw new CommandException(
"File '" + e.getFilename() + "' resolution error: " + e.getMessage());
}
@ExceptionMatch
public void convert(InvalidToolBindException e) throws CommandException {
throw new CommandException("Can't bind tool to "
+ ItemType.toHeldName(e.getItemId()) + ": " + e.getMessage());
}
@ExceptionMatch
public void convert(FileSelectionAbortedException e) throws CommandException {
throw new CommandException("File selection aborted.");
}
@ExceptionMatch
public void convert(WorldEditException e) throws CommandException {
throw new CommandException(e.getMessage());
}
}