2014-06-27 23:03:29 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2013-06-18 21:51:42 +00:00
|
|
|
|
|
|
|
package com.sk89q.worldedit.util;
|
|
|
|
|
2014-06-27 23:03:29 +00:00
|
|
|
import com.sk89q.minecraft.util.commands.CommandContext;
|
|
|
|
import com.sk89q.minecraft.util.commands.CommandException;
|
|
|
|
import com.sk89q.minecraft.util.commands.Logging;
|
|
|
|
import com.sk89q.worldedit.*;
|
2014-06-28 01:28:31 +00:00
|
|
|
import com.sk89q.worldedit.entity.Player;
|
|
|
|
import com.sk89q.worldedit.extension.platform.Actor;
|
2014-06-27 23:03:29 +00:00
|
|
|
import com.sk89q.worldedit.util.command.parametric.AbstractInvokeListener;
|
|
|
|
import com.sk89q.worldedit.util.command.parametric.InvokeHandler;
|
|
|
|
import com.sk89q.worldedit.util.command.parametric.ParameterData;
|
|
|
|
import com.sk89q.worldedit.util.command.parametric.ParameterException;
|
|
|
|
|
2013-06-18 21:51:42 +00:00
|
|
|
import java.io.Closeable;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.logging.Handler;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2014-06-27 23:03:29 +00:00
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
2013-06-18 21:51:42 +00:00
|
|
|
|
|
|
|
/**
|
2014-06-27 23:03:29 +00:00
|
|
|
* Logs called commands to a logger.
|
2013-06-18 21:51:42 +00:00
|
|
|
*/
|
2014-06-27 23:03:29 +00:00
|
|
|
public class CommandLoggingHandler extends AbstractInvokeListener implements InvokeHandler, Closeable {
|
|
|
|
|
2013-06-18 21:51:42 +00:00
|
|
|
private final WorldEdit worldEdit;
|
2014-06-27 23:03:29 +00:00
|
|
|
private final Logger logger;
|
2013-06-18 21:51:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance.
|
2014-06-27 23:03:29 +00:00
|
|
|
*
|
|
|
|
* @param worldEdit an instance of WorldEdit
|
|
|
|
* @param logger the logger to send messages to
|
2013-06-18 21:51:42 +00:00
|
|
|
*/
|
2014-06-27 23:03:29 +00:00
|
|
|
public CommandLoggingHandler(WorldEdit worldEdit, Logger logger) {
|
|
|
|
checkNotNull(worldEdit);
|
|
|
|
checkNotNull(logger);
|
2013-06-18 21:51:42 +00:00
|
|
|
this.worldEdit = worldEdit;
|
2014-06-27 23:03:29 +00:00
|
|
|
this.logger = logger;
|
2013-06-18 21:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-27 23:03:29 +00:00
|
|
|
public void preProcess(Object object, Method method, ParameterData[] parameters, CommandContext context) throws CommandException, ParameterException {
|
2013-06-18 21:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-27 23:03:29 +00:00
|
|
|
public void preInvoke(Object object, Method method, ParameterData[] parameters, Object[] args, CommandContext context) throws CommandException {
|
2013-06-18 21:51:42 +00:00
|
|
|
Logging loggingAnnotation = method.getAnnotation(Logging.class);
|
|
|
|
Logging.LogMode logMode;
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
|
|
|
|
if (loggingAnnotation == null) {
|
|
|
|
logMode = null;
|
|
|
|
} else {
|
|
|
|
logMode = loggingAnnotation.value();
|
|
|
|
}
|
|
|
|
|
2014-06-28 01:28:31 +00:00
|
|
|
Actor sender = context.getLocals().get(Actor.class);
|
|
|
|
Player player;
|
|
|
|
|
2013-06-18 21:51:42 +00:00
|
|
|
if (sender == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-28 01:28:31 +00:00
|
|
|
if (sender instanceof Player) {
|
|
|
|
player = (Player) sender;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-18 21:51:42 +00:00
|
|
|
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()) {
|
2014-06-28 01:28:31 +00:00
|
|
|
Vector position = player.getPosition();
|
|
|
|
LocalSession session = worldEdit.getSessionManager().get(player);
|
2013-06-18 21:51:42 +00:00
|
|
|
|
|
|
|
switch (logMode) {
|
|
|
|
case PLACEMENT:
|
|
|
|
try {
|
2014-06-28 01:28:31 +00:00
|
|
|
position = session.getPlacementPosition(player);
|
2013-06-18 21:51:42 +00:00
|
|
|
} catch (IncompleteRegionException e) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALL-THROUGH */
|
|
|
|
|
|
|
|
case POSITION:
|
|
|
|
builder.append(" - Position: " + position);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ALL:
|
|
|
|
builder.append(" - Position: " + position);
|
|
|
|
/* FALL-THROUGH */
|
|
|
|
|
|
|
|
case ORIENTATION_REGION:
|
2014-06-28 01:28:31 +00:00
|
|
|
builder.append(" - Orientation: " + player.getCardinalDirection().name());
|
2013-06-18 21:51:42 +00:00
|
|
|
/* FALL-THROUGH */
|
|
|
|
|
|
|
|
case REGION:
|
|
|
|
try {
|
|
|
|
builder.append(" - Region: ")
|
|
|
|
.append(session.getSelection(sender.getWorld()));
|
|
|
|
} catch (IncompleteRegionException e) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:03:29 +00:00
|
|
|
logger.info(builder.toString());
|
2013-06-18 21:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-27 23:03:29 +00:00
|
|
|
public void postInvoke(Object object, Method method, ParameterData[] parameters, Object[] args, CommandContext context) throws CommandException {
|
2013-06-18 21:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InvokeHandler createInvokeHandler() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
2014-06-27 23:03:29 +00:00
|
|
|
for (Handler h : logger.getHandlers()) {
|
2013-06-18 21:51:42 +00:00
|
|
|
h.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|