3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-11-04 18:40:06 +01:00

Implemented PlatformManager, Platform over old platform registration method.

Dieser Commit ist enthalten in:
sk89q 2014-04-04 20:53:58 -07:00
Ursprung bed5fa8fdc
Commit 8fb73347e4
19 geänderte Dateien mit 1275 neuen und 373 gelöschten Zeilen

Datei anzeigen

@ -19,28 +19,23 @@
package com.sk89q.worldedit.bukkit; package com.sk89q.worldedit.bukkit;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.sk89q.bukkit.util.CommandInfo; import com.sk89q.bukkit.util.CommandInfo;
import com.sk89q.bukkit.util.CommandRegistration; import com.sk89q.bukkit.util.CommandRegistration;
import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.minecraft.util.commands.CommandsManager; import com.sk89q.minecraft.util.commands.CommandsManager;
import com.sk89q.worldedit.LocalPlayer; import com.sk89q.worldedit.*;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import com.sk89q.worldedit.BiomeTypes; import java.lang.reflect.Method;
import com.sk89q.worldedit.LocalWorld; import java.util.ArrayList;
import com.sk89q.worldedit.ServerInterface; import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class BukkitServerInterface extends ServerInterface { public class BukkitServerInterface extends ServerInterface {
public Server server; public Server server;
@ -119,6 +114,11 @@ public class BukkitServerInterface extends ServerInterface {
dynamicCommands.register(toRegister); dynamicCommands.register(toRegister);
} }
@Override
public LocalConfiguration getConfiguration() {
return plugin.getLocalConfiguration();
}
public void unregisterCommands() { public void unregisterCommands() {
dynamicCommands.unregisterCommands(); dynamicCommands.unregisterCommands();
} }

Datei anzeigen

@ -25,12 +25,12 @@ import com.sk89q.worldedit.*;
import com.sk89q.worldedit.bukkit.selections.CuboidSelection; import com.sk89q.worldedit.bukkit.selections.CuboidSelection;
import com.sk89q.worldedit.bukkit.selections.Polygonal2DSelection; import com.sk89q.worldedit.bukkit.selections.Polygonal2DSelection;
import com.sk89q.worldedit.bukkit.selections.Selection; import com.sk89q.worldedit.bukkit.selections.Selection;
import com.sk89q.worldedit.extension.platform.PlatformRejectionException;
import com.sk89q.worldedit.extent.inventory.BlockBag; import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Polygonal2DRegion; import com.sk89q.worldedit.regions.Polygonal2DRegion;
import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionSelector; import com.sk89q.worldedit.regions.RegionSelector;
import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -40,7 +40,6 @@ import java.io.*;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.logging.Handler;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
/** /**
@ -104,16 +103,22 @@ public class WorldEditPlugin extends JavaPlugin {
// Setup interfaces // Setup interfaces
server = new BukkitServerInterface(this, getServer()); server = new BukkitServerInterface(this, getServer());
controller = new WorldEdit(server, config); controller = WorldEdit.getInstance();
WorldEdit.logger.setParent(Bukkit.getLogger()); try {
api = new WorldEditAPI(this); controller.getPlatformManager().register(server);
getServer().getMessenger().registerIncomingPluginChannel(this, CUI_PLUGIN_CHANNEL, new CUIChannelListener(this)); api = new WorldEditAPI(this);
getServer().getMessenger().registerOutgoingPluginChannel(this, CUI_PLUGIN_CHANNEL); getServer().getMessenger().registerIncomingPluginChannel(this, CUI_PLUGIN_CHANNEL, new CUIChannelListener(this));
// Now we can register events! getServer().getMessenger().registerOutgoingPluginChannel(this, CUI_PLUGIN_CHANNEL);
getServer().getPluginManager().registerEvents(new WorldEditListener(this), this); // Now we can register events!
getServer().getPluginManager().registerEvents(new WorldEditListener(this), this);
getServer().getScheduler().runTaskTimerAsynchronously(this, getServer().getScheduler().runTaskTimerAsynchronously(this,
new SessionTimer(controller, getServer()), 120, 120); new SessionTimer(controller, getServer()), 120, 120);
} catch (PlatformRejectionException e) {
throw new RuntimeException(
"WorldEdit rejected the Bukkit implementation of WorldEdit! This is strange and should " +
"not have happened. Please report this error.", e);
}
} }
private void copyNmsBlockClasses(File target) { private void copyNmsBlockClasses(File target) {
@ -149,9 +154,7 @@ public class WorldEditPlugin extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
controller.clearSessions(); controller.clearSessions();
for (Handler h : controller.commandLogger.getHandlers()) { controller.getPlatformManager().unregister(server);
h.close();
}
config.unload(); config.unload();
server.unregisterCommands(); server.unregisterCommands();
this.getServer().getScheduler().cancelTasks(this); this.getServer().getScheduler().cancelTasks(this);

Datei anzeigen

@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import com.sk89q.worldedit.LocalConfiguration;
import net.minecraft.command.CommandBase; import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender; import net.minecraft.command.ICommandSender;
import net.minecraft.command.ServerCommandManager; import net.minecraft.command.ServerCommandManager;
@ -40,10 +41,12 @@ import com.sk89q.worldedit.ServerInterface;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
public class ForgeServerInterface extends ServerInterface { public class ForgeServerInterface extends ServerInterface {
private MinecraftServer server; private final WorldEditMod mod;
private ForgeBiomeTypes biomes; private final MinecraftServer server;
private final ForgeBiomeTypes biomes;
public ForgeServerInterface() { public ForgeServerInterface(WorldEditMod mod) {
this.mod = mod;
this.server = FMLCommonHandler.instance().getMinecraftServerInstance(); this.server = FMLCommonHandler.instance().getMinecraftServerInstance();
this.biomes = new ForgeBiomeTypes(); this.biomes = new ForgeBiomeTypes();
} }
@ -114,4 +117,9 @@ public class ForgeServerInterface extends ServerInterface {
}); });
} }
} }
@Override
public LocalConfiguration getConfiguration() {
return mod.getConfig();
}
} }

Datei anzeigen

@ -28,6 +28,7 @@ import java.util.jar.JarFile;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import com.sk89q.worldedit.extension.platform.PlatformRejectionException;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
@ -92,8 +93,13 @@ public class WorldEditMod {
@EventHandler @EventHandler
public void serverStarting(FMLServerStartingEvent event) { public void serverStarting(FMLServerStartingEvent event) {
this.server = new ForgeServerInterface(); this.server = new ForgeServerInterface(this);
this.controller = new WorldEdit(this.server, this.config); this.controller = WorldEdit.getInstance();
try {
controller.getPlatformManager().register(server);
} catch (PlatformRejectionException e) {
throw new RuntimeException("Failed to register with WorldEdit", e);
}
} }
public ForgeConfiguration getConfig() { public ForgeConfiguration getConfig() {

Datei anzeigen

@ -21,6 +21,7 @@ package com.sk89q.worldedit;
import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandsManager; import com.sk89q.minecraft.util.commands.CommandsManager;
import com.sk89q.worldedit.extension.platform.Platform;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -29,58 +30,27 @@ import java.util.List;
* *
* @author sk89q * @author sk89q
*/ */
public abstract class ServerInterface { public abstract class ServerInterface implements Platform {
/**
* Resolves an item name to its ID.
*
* @param name The name to look up
* @return The id that corresponds to the name, or -1 if no such ID exists
*/
public abstract int resolveItem(String name);
/** @Override
* Checks if a mob type is valid.
*
* @param type The mob type name to check
* @return Whether the name is a valid mod bype
*/
public abstract boolean isValidMobType(String type);
/**
* Reload WorldEdit configuration.
*/
public abstract void reload();
/**
* Returns all available biomes.
*
* @return
*/
public abstract BiomeTypes getBiomes();
/**
* Schedules the given <code>task</code> to be invoked once every <code>period</code> ticks
* after an initial delay of <code>delay</code> ticks.
*
* @param delay Delay in server ticks before executing first repeat
* @param period Period in server ticks of the task
* @param task Task to be executed
* @return Task id number (-1 if scheduling failed)
*/
public int schedule(long delay, long period, Runnable task) { public int schedule(long delay, long period, Runnable task) {
return -1; return -1;
} }
@Override
public List<LocalWorld> getWorlds() { public List<LocalWorld> getWorlds() {
return Collections.emptyList(); return Collections.emptyList();
} }
@Override
@Deprecated @Deprecated
public void onCommandRegistration(List<Command> commands) { public void onCommandRegistration(List<Command> commands) {
// Do nothing :) // Do nothing :)
} }
@Override
public void onCommandRegistration(List<Command> commands, CommandsManager<LocalPlayer> manager) { public void onCommandRegistration(List<Command> commands, CommandsManager<LocalPlayer> manager) {
onCommandRegistration(commands); onCommandRegistration(commands);
} }
} }

Datei anzeigen

@ -19,17 +19,16 @@
package com.sk89q.worldedit; package com.sk89q.worldedit;
import com.sk89q.minecraft.util.commands.*; import com.sk89q.minecraft.util.commands.CommandsManager;
import com.sk89q.minecraft.util.commands.Console;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.CuboidClipboard.FlipDirection; import com.sk89q.worldedit.CuboidClipboard.FlipDirection;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockType; import com.sk89q.worldedit.blocks.BlockType;
import com.sk89q.worldedit.blocks.ItemType;
import com.sk89q.worldedit.command.*;
import com.sk89q.worldedit.command.tool.*; import com.sk89q.worldedit.command.tool.*;
import com.sk89q.worldedit.event.extent.EditSessionEvent; import com.sk89q.worldedit.event.extent.EditSessionEvent;
import com.sk89q.worldedit.event.platform.CommandEvent;
import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.extension.platform.PlatformManager;
import com.sk89q.worldedit.extension.registry.BlockRegistry; import com.sk89q.worldedit.extension.registry.BlockRegistry;
import com.sk89q.worldedit.extension.registry.MaskRegistry; import com.sk89q.worldedit.extension.registry.MaskRegistry;
import com.sk89q.worldedit.extension.registry.PatternRegistry; import com.sk89q.worldedit.extension.registry.PatternRegistry;
@ -44,35 +43,41 @@ import com.sk89q.worldedit.scripting.CraftScriptEngine;
import com.sk89q.worldedit.scripting.RhinoCraftScriptEngine; import com.sk89q.worldedit.scripting.RhinoCraftScriptEngine;
import com.sk89q.worldedit.session.SessionManager; import com.sk89q.worldedit.session.SessionManager;
import com.sk89q.worldedit.session.request.Request; import com.sk89q.worldedit.session.request.Request;
import com.sk89q.worldedit.util.LogFormat;
import com.sk89q.worldedit.util.eventbus.EventBus; import com.sk89q.worldedit.util.eventbus.EventBus;
import com.sk89q.worldedit.util.logging.WorldEditPrefixHandler;
import javax.script.ScriptException; import javax.script.ScriptException;
import java.io.*; import java.io.*;
import java.lang.reflect.Method; import java.util.HashMap;
import java.util.*; import java.util.HashSet;
import java.util.logging.FileHandler; import java.util.Map;
import java.util.logging.Level; import java.util.Set;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* The current instance of WorldEdit. * The entry point and container for a working implementation of WorldEdit.
* </p>
* An instance handles event handling; block, mask, pattern, etc. registration;
* the management of sessions; the creation of {@link EditSession}s; and more.
* In order to use WorldEdit, at least one {@link Platform} must be registered
* with WorldEdit using {@link PlatformManager#register(Platform)} on the
* manager retrieved using {@link WorldEdit#getPlatformManager()}.
* </p>
* An instance of WorldEdit can be retrieved using the static
* method {@link WorldEdit#getInstance()}, which is shared among all
* platforms within the same classloader hierarchy.
*/ */
public class WorldEdit { public class WorldEdit {
public static final Logger logger = Logger.getLogger("Minecraft.WorldEdit"); public static final Logger logger = Logger.getLogger(WorldEdit.class.getCanonicalName());
public final Logger commandLogger = Logger.getLogger("Minecraft.WorldEdit.CommandLogger");
private static WorldEdit instance; private final static WorldEdit instance = new WorldEdit();
private static String version; private static String version;
private final ServerInterface server;
private final LocalConfiguration config;
private final CommandsManager<LocalPlayer> commands;
private final EventBus eventBus = new EventBus(); private final EventBus eventBus = new EventBus();
private final PlatformManager platformManager = new PlatformManager(this);
private final EditSessionFactory editSessionFactory = new EditSessionFactory.EditSessionFactoryImpl(eventBus); private final EditSessionFactory editSessionFactory = new EditSessionFactory.EditSessionFactoryImpl(eventBus);
private final SessionManager sessions = new SessionManager(this); private final SessionManager sessions = new SessionManager(this);
@ -81,139 +86,41 @@ public class WorldEdit {
private final PatternRegistry patternRegistry = new PatternRegistry(this); private final PatternRegistry patternRegistry = new PatternRegistry(this);
static { static {
WorldEditPrefixHandler.register("com.sk89q.worldedit");
getVersion(); getVersion();
} }
private WorldEdit() {
}
/** /**
* Construct an instance of WorldEdit. * Gets the current instance of this class.
* </p>
* An instance will always be available, but no platform may yet be
* registered with WorldEdit, meaning that a number of operations
* may fail. However, event handlers can be registered.
* *
* @param server * @return an instance of WorldEdit.
* @param config
*/
public WorldEdit(ServerInterface server, final LocalConfiguration config) {
instance = this;
this.server = server;
this.config = config;
if (!config.logFile.equals("")) {
try {
FileHandler logFileHandler;
logFileHandler = new FileHandler(new File(config.getWorkingDirectory(),
config.logFile).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());
}
}
commands = new CommandsManager<LocalPlayer>() {
@Override
protected void checkPermission(LocalPlayer player, Method method) throws CommandException {
if (!player.isPlayer() && !method.isAnnotationPresent(Console.class)) {
throw new UnhandledCommandException();
}
super.checkPermission(player, method);
}
@Override
public boolean hasPermission(LocalPlayer player, String perm) {
return player.hasPermission(perm);
}
@Override
public void invokeMethod(Method parent, String[] args,
LocalPlayer player, Method method, Object instance,
Object[] methodArgs, int level) throws CommandException {
if (config.logCommands) {
final Logging loggingAnnotation = method.getAnnotation(Logging.class);
final Logging.LogMode logMode;
if (loggingAnnotation == null) {
logMode = null;
} else {
logMode = loggingAnnotation.value();
}
String msg = "WorldEdit: " + player.getName();
if (player.isPlayer()) {
msg += " (in \"" + player.getWorld().getName() + "\")";
}
msg += ": " + StringUtil.joinString(args, " ");
if (logMode != null && player.isPlayer()) {
Vector position = player.getPosition();
final LocalSession session = getSession(player);
switch (logMode) {
case PLACEMENT:
try {
position = session.getPlacementPosition(player);
} catch (IncompleteRegionException e) {
break;
}
/* FALL-THROUGH */
case POSITION:
msg += " - Position: " + position;
break;
case ALL:
msg += " - Position: " + position;
/* FALL-THROUGH */
case ORIENTATION_REGION:
msg += " - Orientation: " + player.getCardinalDirection().name();
/* FALL-THROUGH */
case REGION:
try {
msg += " - Region: " + session.getSelection(player.getWorld());
} catch (IncompleteRegionException e) {
break;
}
break;
}
}
commandLogger.info(msg);
}
super.invokeMethod(parent, args, player, method, instance, methodArgs, level);
}
};
commands.setInjector(new SimpleInjector(this));
reg(BiomeCommands.class);
reg(ChunkCommands.class);
reg(ClipboardCommands.class);
reg(GeneralCommands.class);
reg(GenerationCommands.class);
reg(HistoryCommands.class);
reg(NavigationCommands.class);
reg(RegionCommands.class);
reg(ScriptingCommands.class);
reg(SelectionCommands.class);
reg(SnapshotUtilCommands.class);
reg(ToolUtilCommands.class);
reg(ToolCommands.class);
reg(UtilityCommands.class);
}
private void reg(Class<?> clazz) {
server.onCommandRegistration(commands.registerAndReturn(clazz), commands);
}
/**
* Gets the current instance of this class
*
* @return
*/ */
public static WorldEdit getInstance() { public static WorldEdit getInstance() {
return instance; return instance;
} }
/**
* Get the platform manager, where platforms (that implement WorldEdit)
* can be registered and information about registered platforms can
* be queried.
*
* @return the platform manager
*/
public PlatformManager getPlatformManager() {
return platformManager;
}
/** /**
* Get the event bus for WorldEdit. * Get the event bus for WorldEdit.
* </p>
* Event handlers can be registered on the event bus.
* *
* @return the event bus * @return the event bus
*/ */
@ -487,7 +394,7 @@ public class WorldEdit {
String filePath = f.getCanonicalPath(); String filePath = f.getCanonicalPath();
String dirPath = dir.getCanonicalPath(); String dirPath = dir.getCanonicalPath();
if (!filePath.substring(0, dirPath.length()).equals(dirPath) && !config.allowSymlinks) { if (!filePath.substring(0, dirPath.length()).equals(dirPath) && !getConfiguration().allowSymlinks) {
throw new FilenameResolutionException(filename, throw new FilenameResolutionException(filename,
"Path is outside allowable root"); "Path is outside allowable root");
} }
@ -500,27 +407,27 @@ public class WorldEdit {
} }
public int getMaximumPolygonalPoints(LocalPlayer player) { public int getMaximumPolygonalPoints(LocalPlayer player) {
if (player.hasPermission("worldedit.limit.unrestricted") || config.maxPolygonalPoints < 0) { if (player.hasPermission("worldedit.limit.unrestricted") || getConfiguration().maxPolygonalPoints < 0) {
return config.defaultMaxPolygonalPoints; return getConfiguration().defaultMaxPolygonalPoints;
} }
if (config.defaultMaxPolygonalPoints < 0) { if (getConfiguration().defaultMaxPolygonalPoints < 0) {
return config.maxPolygonalPoints; return getConfiguration().maxPolygonalPoints;
} }
return Math.min(config.defaultMaxPolygonalPoints, config.maxPolygonalPoints); return Math.min(getConfiguration().defaultMaxPolygonalPoints, getConfiguration().maxPolygonalPoints);
} }
public int getMaximumPolyhedronPoints(LocalPlayer player) { public int getMaximumPolyhedronPoints(LocalPlayer player) {
if (player.hasPermission("worldedit.limit.unrestricted") || config.maxPolyhedronPoints < 0) { if (player.hasPermission("worldedit.limit.unrestricted") || getConfiguration().maxPolyhedronPoints < 0) {
return config.defaultMaxPolyhedronPoints; return getConfiguration().defaultMaxPolyhedronPoints;
} }
if (config.defaultMaxPolyhedronPoints < 0) { if (getConfiguration().defaultMaxPolyhedronPoints < 0) {
return config.maxPolyhedronPoints; return getConfiguration().maxPolyhedronPoints;
} }
return Math.min(config.defaultMaxPolyhedronPoints, config.maxPolyhedronPoints); return Math.min(getConfiguration().defaultMaxPolyhedronPoints, getConfiguration().maxPolyhedronPoints);
} }
/** /**
@ -530,7 +437,7 @@ public class WorldEdit {
* @throws MaxRadiusException * @throws MaxRadiusException
*/ */
public void checkMaxRadius(double radius) throws MaxRadiusException { public void checkMaxRadius(double radius) throws MaxRadiusException {
if (config.maxRadius > 0 && radius > config.maxRadius) { if (getConfiguration().maxRadius > 0 && radius > getConfiguration().maxRadius) {
throw new MaxRadiusException(); throw new MaxRadiusException();
} }
} }
@ -542,7 +449,7 @@ public class WorldEdit {
* @throws MaxBrushRadiusException * @throws MaxBrushRadiusException
*/ */
public void checkMaxBrushRadius(double radius) throws MaxBrushRadiusException { public void checkMaxBrushRadius(double radius) throws MaxBrushRadiusException {
if (config.maxBrushRadius > 0 && radius > config.maxBrushRadius) { if (getConfiguration().maxBrushRadius > 0 && radius > getConfiguration().maxBrushRadius) {
throw new MaxBrushRadiusException(); throw new MaxBrushRadiusException();
} }
} }
@ -560,7 +467,7 @@ public class WorldEdit {
return f; return f;
} }
return new File(config.getWorkingDirectory(), path); return new File(getConfiguration().getWorkingDirectory(), path);
} }
/** /**
@ -761,14 +668,14 @@ public class WorldEdit {
* @return the commands * @return the commands
*/ */
public Map<String, String> getCommands() { public Map<String, String> getCommands() {
return commands.getCommands(); return getCommandsManager().getCommands();
} }
/** /**
* @return the commands * @return the commands
*/ */
public CommandsManager<LocalPlayer> getCommandsManager() { public CommandsManager<LocalPlayer> getCommandsManager() {
return commands; return getPlatformManager().getCommandManager().getCommands();
} }
/** /**
@ -813,8 +720,8 @@ public class WorldEdit {
* @return * @return
*/ */
public boolean handleArmSwing(LocalPlayer player) { public boolean handleArmSwing(LocalPlayer player) {
if (player.getItemInHand() == config.navigationWand) { if (player.getItemInHand() == getConfiguration().navigationWand) {
if (config.navigationWandMaxDistance <= 0) { if (getConfiguration().navigationWandMaxDistance <= 0) {
return false; return false;
} }
@ -822,7 +729,7 @@ public class WorldEdit {
return false; return false;
} }
WorldVector pos = player.getSolidBlockTrace(config.navigationWandMaxDistance); WorldVector pos = player.getSolidBlockTrace(getConfiguration().navigationWandMaxDistance);
if (pos != null) { if (pos != null) {
player.findFreePosition(pos); player.findFreePosition(pos);
} else { } else {
@ -836,7 +743,7 @@ public class WorldEdit {
Tool tool = session.getTool(player.getItemInHand()); Tool tool = session.getTool(player.getItemInHand());
if (tool != null && tool instanceof DoubleActionTraceTool) { if (tool != null && tool instanceof DoubleActionTraceTool) {
if (tool.canUse(player)) { if (tool.canUse(player)) {
((DoubleActionTraceTool) tool).actSecondary(server, config, player, session); ((DoubleActionTraceTool) tool).actSecondary(getServer(), getConfiguration(), player, session);
return true; return true;
} }
} }
@ -851,8 +758,8 @@ public class WorldEdit {
* @return * @return
*/ */
public boolean handleRightClick(LocalPlayer player) { public boolean handleRightClick(LocalPlayer player) {
if (player.getItemInHand() == config.navigationWand) { if (player.getItemInHand() == getConfiguration().navigationWand) {
if (config.navigationWandMaxDistance <= 0) { if (getConfiguration().navigationWandMaxDistance <= 0) {
return false; return false;
} }
@ -872,7 +779,7 @@ public class WorldEdit {
Tool tool = session.getTool(player.getItemInHand()); Tool tool = session.getTool(player.getItemInHand());
if (tool != null && tool instanceof TraceTool) { if (tool != null && tool instanceof TraceTool) {
if (tool.canUse(player)) { if (tool.canUse(player)) {
((TraceTool) tool).actPrimary(server, config, player, session); ((TraceTool) tool).actPrimary(getServer(), getConfiguration(), player, session);
return true; return true;
} }
} }
@ -890,7 +797,7 @@ public class WorldEdit {
public boolean handleBlockRightClick(LocalPlayer player, WorldVector clicked) { public boolean handleBlockRightClick(LocalPlayer player, WorldVector clicked) {
LocalSession session = getSession(player); LocalSession session = getSession(player);
if (player.getItemInHand() == config.wandItem) { if (player.getItemInHand() == getConfiguration().wandItem) {
if (!session.isToolControlEnabled()) { if (!session.isToolControlEnabled()) {
return false; return false;
} }
@ -910,7 +817,7 @@ public class WorldEdit {
Tool tool = session.getTool(player.getItemInHand()); Tool tool = session.getTool(player.getItemInHand());
if (tool != null && tool instanceof BlockTool) { if (tool != null && tool instanceof BlockTool) {
if (tool.canUse(player)) { if (tool.canUse(player)) {
((BlockTool) tool).actPrimary(server, config, player, session, clicked); ((BlockTool) tool).actPrimary(getServer(), getConfiguration(), player, session, clicked);
return true; return true;
} }
} }
@ -928,7 +835,7 @@ public class WorldEdit {
public boolean handleBlockLeftClick(LocalPlayer player, WorldVector clicked) { public boolean handleBlockLeftClick(LocalPlayer player, WorldVector clicked) {
LocalSession session = getSession(player); LocalSession session = getSession(player);
if (player.getItemInHand() == config.wandItem) { if (player.getItemInHand() == getConfiguration().wandItem) {
if (!session.isToolControlEnabled()) { if (!session.isToolControlEnabled()) {
return false; return false;
} }
@ -948,14 +855,14 @@ public class WorldEdit {
if (player.isHoldingPickAxe() && session.hasSuperPickAxe()) { if (player.isHoldingPickAxe() && session.hasSuperPickAxe()) {
final BlockTool superPickaxe = session.getSuperPickaxe(); final BlockTool superPickaxe = session.getSuperPickaxe();
if (superPickaxe != null && superPickaxe.canUse(player)) { if (superPickaxe != null && superPickaxe.canUse(player)) {
return superPickaxe.actPrimary(server, config, player, session, clicked); return superPickaxe.actPrimary(getServer(), getConfiguration(), player, session, clicked);
} }
} }
Tool tool = session.getTool(player.getItemInHand()); Tool tool = session.getTool(player.getItemInHand());
if (tool != null && tool instanceof DoubleActionBlockTool) { if (tool != null && tool instanceof DoubleActionBlockTool) {
if (tool.canUse(player)) { if (tool.canUse(player)) {
((DoubleActionBlockTool) tool).actSecondary(server, config, player, session, clicked); ((DoubleActionBlockTool) tool).actSecondary(getServer(), getConfiguration(), player, session, clicked);
return true; return true;
} }
} }
@ -963,8 +870,6 @@ public class WorldEdit {
return false; return false;
} }
private static final java.util.regex.Pattern numberFormatExceptionPattern = java.util.regex.Pattern.compile("^For input string: \"(.*)\"$");
/** /**
* *
* @param player * @param player
@ -972,135 +877,13 @@ public class WorldEdit {
* @return whether the command was processed * @return whether the command was processed
*/ */
public boolean handleCommand(LocalPlayer player, String[] split) { public boolean handleCommand(LocalPlayer player, String[] split) {
Request.reset(); CommandEvent event = new CommandEvent(player, split);
getEventBus().post(event);
try { return event.isCancelled();
split = commandDetection(split);
// No command found!
if (!commands.hasCommand(split[0])) {
return false;
}
LocalSession session = getSession(player);
EditSession editSession = session.createEditSession(player);
editSession.enableQueue();
session.tellVersion(player);
long start = System.currentTimeMillis();
try {
commands.execute(split, player, session, player, editSession);
} catch (CommandPermissionsException e) {
player.printError("You don't have permission to do this.");
} catch (MissingNestedCommandException e) {
player.printError(e.getUsage());
} catch (CommandUsageException e) {
player.printError(e.getMessage());
player.printError(e.getUsage());
} catch (PlayerNeededException e) {
player.printError(e.getMessage());
} catch (WrappedCommandException e) {
throw e.getCause();
} catch (UnhandledCommandException e) {
player.printError("Command could not be handled; invalid sender!");
return false;
} finally {
session.remember(editSession);
editSession.flushQueue();
if (config.profile) {
long time = System.currentTimeMillis() - start;
int changed = editSession.getBlockChangeCount();
if (time > 0) {
double throughput = changed / (time / 1000.0);
player.printDebug((time / 1000.0) + "s elapsed (history: "
+ changed + " changed; "
+ Math.round(throughput) + " blocks/sec).");
} else {
player.printDebug((time / 1000.0) + "s elapsed.");
}
}
flushBlockBag(player, editSession);
}
} catch (NumberFormatException e) {
final Matcher matcher = numberFormatExceptionPattern.matcher(e.getMessage());
if (matcher.matches()) {
player.printError("Number expected; string \"" + matcher.group(1) + "\" given.");
} else {
player.printError("Number expected; string given.");
}
} catch (IncompleteRegionException e) {
player.printError("Make a region selection first.");
} catch (UnknownItemException e) {
player.printError("Block name '" + e.getID() + "' was not recognized.");
} catch (InvalidItemException e) {
player.printError(e.getMessage());
} catch (DisallowedItemException e) {
player.printError("Block '" + e.getID() + "' not allowed (see WorldEdit configuration).");
} catch (MaxChangedBlocksException e) {
player.printError("Max blocks changed in an operation reached ("
+ e.getBlockLimit() + ").");
} catch (MaxBrushRadiusException e) {
player.printError("Maximum allowed brush size: " + config.maxBrushRadius);
} catch (MaxRadiusException e) {
player.printError("Maximum allowed size: " + config.maxRadius);
} catch (UnknownDirectionException e) {
player.printError("Unknown direction: " + e.getDirection());
} catch (InsufficientArgumentsException e) {
player.printError(e.getMessage());
} catch (EmptyClipboardException e) {
player.printError("Your clipboard is empty. Use //copy first.");
} catch (InvalidFilenameException e) {
player.printError("Filename '" + e.getFilename() + "' invalid: "
+ e.getMessage());
} catch (FilenameResolutionException e) {
player.printError("File '" + e.getFilename() + "' resolution error: "
+ e.getMessage());
} catch (InvalidToolBindException e) {
player.printError("Can't bind tool to "
+ ItemType.toHeldName(e.getItemId()) + ": " + e.getMessage());
} catch (FileSelectionAbortedException e) {
player.printError("File selection aborted.");
} catch (WorldEditException e) {
player.printError(e.getMessage());
} catch (Throwable excp) {
player.printError("Please report this error: [See console]");
player.printRaw(excp.getClass().getName() + ": " + excp.getMessage());
excp.printStackTrace();
}
return true;
} }
public String[] commandDetection(String[] split) { public String[] commandDetection(String[] split) {
Request.reset(); return getPlatformManager().getCommandManager().commandDetection(split);
split[0] = split[0].substring(1);
// Quick script shortcut
if (split[0].matches("^[^/].*\\.js$")) {
String[] newSplit = new String[split.length + 1];
System.arraycopy(split, 0, newSplit, 1, split.length);
newSplit[0] = "cs";
newSplit[1] = newSplit[1];
split = newSplit;
}
String searchCmd = split[0].toLowerCase();
// Try to detect the command
if (commands.hasCommand(searchCmd)) {
} else if (config.noDoubleSlash && commands.hasCommand("/" + searchCmd)) {
split[0] = "/" + split[0];
} else if (split[0].length() >= 2 && split[0].charAt(0) == '/'
&& commands.hasCommand(searchCmd.substring(1))) {
split[0] = split[0].substring(1);
}
return split;
} }
/** /**
@ -1152,7 +935,7 @@ public class WorldEdit {
LocalSession session = getSession(player); LocalSession session = getSession(player);
CraftScriptContext scriptContext = CraftScriptContext scriptContext =
new CraftScriptContext(this, server, config, session, player, args); new CraftScriptContext(this, getServer(), getConfiguration(), session, player, args);
CraftScriptEngine engine = null; CraftScriptEngine engine = null;
@ -1164,7 +947,7 @@ public class WorldEdit {
return; return;
} }
engine.setTimeLimit(config.scriptTimeout); engine.setTimeLimit(getConfiguration().scriptTimeout);
Map<String, Object> vars = new HashMap<String, Object>(); Map<String, Object> vars = new HashMap<String, Object>();
vars.put("argv", args); vars.put("argv", args);
@ -1196,19 +979,19 @@ public class WorldEdit {
/** /**
* Get Worldedit's configuration. * Get Worldedit's configuration.
* *
* @return * @return a configuration
*/ */
public LocalConfiguration getConfiguration() { public LocalConfiguration getConfiguration() {
return config; return getPlatformManager().getConfiguration();
} }
/** /**
* Get the server interface. * Get the server interface.
* *
* @return * @return the server interface
*/ */
public ServerInterface getServer() { public ServerInterface getServer() {
return server; return getPlatformManager().getServerInterface();
} }
/** /**

Datei anzeigen

@ -0,0 +1,39 @@
/*
* 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.event;
/**
* An abstract implementation of {@link Cancellable} that has all
* of {@link Cancellable}'s methods implemented.
*/
public abstract class AbstractCancellable implements Cancellable {
private boolean cancelled;
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}

Datei anzeigen

@ -0,0 +1,42 @@
/*
* 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.event;
/**
* Marks an event that has a cancellable state. The meaning of cancellation
* depends on the event.
*/
public interface Cancellable {
/**
* Returns whether the event has been cancelled.
*
* @return true if cancelled
*/
boolean isCancelled();
/**
* Set whether the event has been cancelled.
*
* @param cancelled true if cancelled
*/
void setCancelled(boolean cancelled);
}

Datei anzeigen

@ -0,0 +1,67 @@
/*
* 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.event.platform;
import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.event.AbstractCancellable;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* This class is currently only for internal use. Do not post or catch this event.
*/
public class CommandEvent extends AbstractCancellable {
private final LocalPlayer player;
private final String[] args;
/**
* Create a new instance.
*
* @param player the player
* @param args the arguments
*/
public CommandEvent(LocalPlayer player, String[] args) {
checkNotNull(player);
checkNotNull(args);
this.player = player;
this.args = args;
}
/**
* Get the player.
*
* @return the player
*/
public LocalPlayer getPlayer() {
return player;
}
/**
* Get the arguments.
*
* @return the arguments
*/
public String[] getArguments() {
return args;
}
}

Datei anzeigen

@ -0,0 +1,343 @@
/*
* 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.extension.platform;
import com.sk89q.minecraft.util.commands.*;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.ItemType;
import com.sk89q.worldedit.command.*;
import com.sk89q.worldedit.event.platform.CommandEvent;
import com.sk89q.worldedit.session.request.Request;
import com.sk89q.worldedit.util.logging.LogFormat;
import com.sk89q.worldedit.util.eventbus.Subscribe;
import com.sk89q.worldedit.util.logging.DynamicStreamHandler;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.logging.*;
import java.util.regex.Matcher;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Handles the registration and invocation of commands.
* </p>
* This class is primarily for internal usage.
*/
public final class CommandManager {
private static final Logger logger = Logger.getLogger(CommandManager.class.getCanonicalName());
private static final java.util.regex.Pattern numberFormatExceptionPattern = java.util.regex.Pattern.compile("^For input string: \"(.*)\"$");
private final WorldEdit worldEdit;
private final CommandsManager<LocalPlayer> commands;
private final DynamicStreamHandler dynamicHandler = new DynamicStreamHandler();
/**
* Create a new instance.
*
* @param worldEdit the WorldEdit instance
*/
CommandManager(final WorldEdit worldEdit) {
checkNotNull(worldEdit);
this.worldEdit = worldEdit;
// Register this instance for command events
worldEdit.getEventBus().register(this);
// Setup the logger
logger.addHandler(dynamicHandler);
dynamicHandler.setFormatter(new LogFormat());
// Set up the commands manager
commands = new CommandsManagerImpl();
commands.setInjector(new SimpleInjector(worldEdit));
}
void register(Platform platform) {
logger.log(Level.FINE, "Registering commands with " + platform.getClass().getCanonicalName());
LocalConfiguration config = platform.getConfiguration();
boolean logging = config.logCommands;
String path = config.logFile;
// Register log
if (!logging || path.isEmpty()) {
dynamicHandler.setHandler(null);
} else {
File file = new File(config.getWorkingDirectory(), path);
logger.log(Level.INFO, "Logging WorldEdit commands to " + file.getAbsolutePath());
try {
dynamicHandler.setHandler(new FileHandler(file.getAbsolutePath(), true));
} catch (IOException e) {
logger.log(Level.WARNING, "Could not use command log file " + path + ": " + e.getMessage());
}
}
register(platform, BiomeCommands.class);
register(platform, ChunkCommands.class);
register(platform, ClipboardCommands.class);
register(platform, GeneralCommands.class);
register(platform, GenerationCommands.class);
register(platform, HistoryCommands.class);
register(platform, NavigationCommands.class);
register(platform, RegionCommands.class);
register(platform, ScriptingCommands.class);
register(platform, SelectionCommands.class);
register(platform, SnapshotUtilCommands.class);
register(platform, ToolUtilCommands.class);
register(platform, ToolCommands.class);
register(platform, UtilityCommands.class);
}
void unregister() {
dynamicHandler.setHandler(null);
}
private void register(Platform platform, Class<?> clazz) {
platform.onCommandRegistration(commands.registerAndReturn(clazz), commands);
}
public CommandsManager<LocalPlayer> getCommands() {
return commands;
}
public String[] commandDetection(String[] split) {
Request.reset();
split[0] = split[0].substring(1);
// Quick script shortcut
if (split[0].matches("^[^/].*\\.js$")) {
String[] newSplit = new String[split.length + 1];
System.arraycopy(split, 0, newSplit, 1, split.length);
newSplit[0] = "cs";
newSplit[1] = newSplit[1];
split = newSplit;
}
String searchCmd = split[0].toLowerCase();
// Try to detect the command
if (commands.hasCommand(searchCmd)) {
} else if (worldEdit.getConfiguration().noDoubleSlash && commands.hasCommand("/" + searchCmd)) {
split[0] = "/" + split[0];
} else if (split[0].length() >= 2 && split[0].charAt(0) == '/'
&& commands.hasCommand(searchCmd.substring(1))) {
split[0] = split[0].substring(1);
}
return split;
}
@Subscribe
public void handleCommand(CommandEvent event) {
Request.reset();
LocalPlayer player = event.getPlayer();
String[] split = event.getArguments();
try {
split = commandDetection(split);
// No command found!
if (!commands.hasCommand(split[0])) {
return;
}
LocalSession session = worldEdit.getSession(player);
EditSession editSession = session.createEditSession(player);
editSession.enableQueue();
session.tellVersion(player);
long start = System.currentTimeMillis();
try {
commands.execute(split, player, session, player, editSession);
} catch (CommandPermissionsException e) {
player.printError("You don't have permission to do this.");
} catch (MissingNestedCommandException e) {
player.printError(e.getUsage());
} catch (CommandUsageException e) {
player.printError(e.getMessage());
player.printError(e.getUsage());
} catch (PlayerNeededException e) {
player.printError(e.getMessage());
} catch (WrappedCommandException e) {
throw e.getCause();
} catch (UnhandledCommandException e) {
player.printError("Command could not be handled; invalid sender!");
event.setCancelled(true);
return;
} finally {
session.remember(editSession);
editSession.flushQueue();
if (worldEdit.getConfiguration().profile) {
long time = System.currentTimeMillis() - start;
int changed = editSession.getBlockChangeCount();
if (time > 0) {
double throughput = changed / (time / 1000.0);
player.printDebug((time / 1000.0) + "s elapsed (history: "
+ changed + " changed; "
+ Math.round(throughput) + " blocks/sec).");
} else {
player.printDebug((time / 1000.0) + "s elapsed.");
}
}
worldEdit.flushBlockBag(player, editSession);
}
} catch (NumberFormatException e) {
final Matcher matcher = numberFormatExceptionPattern.matcher(e.getMessage());
if (matcher.matches()) {
player.printError("Number expected; string \"" + matcher.group(1) + "\" given.");
} else {
player.printError("Number expected; string given.");
}
} catch (IncompleteRegionException e) {
player.printError("Make a region selection first.");
} catch (UnknownItemException e) {
player.printError("Block name '" + e.getID() + "' was not recognized.");
} catch (InvalidItemException e) {
player.printError(e.getMessage());
} catch (DisallowedItemException e) {
player.printError("Block '" + e.getID() + "' not allowed (see WorldEdit configuration).");
} catch (MaxChangedBlocksException e) {
player.printError("Max blocks changed in an operation reached ("
+ e.getBlockLimit() + ").");
} catch (MaxBrushRadiusException e) {
player.printError("Maximum allowed brush size: " + worldEdit.getConfiguration().maxBrushRadius);
} catch (MaxRadiusException e) {
player.printError("Maximum allowed size: " + worldEdit.getConfiguration().maxRadius);
} catch (UnknownDirectionException e) {
player.printError("Unknown direction: " + e.getDirection());
} catch (InsufficientArgumentsException e) {
player.printError(e.getMessage());
} catch (EmptyClipboardException e) {
player.printError("Your clipboard is empty. Use //copy first.");
} catch (InvalidFilenameException e) {
player.printError("Filename '" + e.getFilename() + "' invalid: "
+ e.getMessage());
} catch (FilenameResolutionException e) {
player.printError("File '" + e.getFilename() + "' resolution error: "
+ e.getMessage());
} catch (InvalidToolBindException e) {
player.printError("Can't bind tool to "
+ ItemType.toHeldName(e.getItemId()) + ": " + e.getMessage());
} catch (FileSelectionAbortedException e) {
player.printError("File selection aborted.");
} catch (WorldEditException e) {
player.printError(e.getMessage());
} catch (Throwable excp) {
player.printError("Please report this error: [See console]");
player.printRaw(excp.getClass().getName() + ": " + excp.getMessage());
excp.printStackTrace();
}
event.setCancelled(true);
}
private class CommandsManagerImpl extends CommandsManager<LocalPlayer> {
@Override
protected void checkPermission(LocalPlayer player, Method method) throws CommandException {
if (!player.isPlayer() && !method.isAnnotationPresent(Console.class)) {
throw new UnhandledCommandException();
}
super.checkPermission(player, method);
}
@Override
public boolean hasPermission(LocalPlayer player, String perm) {
return player.hasPermission(perm);
}
@Override
public void invokeMethod(Method parent, String[] args,
LocalPlayer player, Method method, Object instance,
Object[] methodArgs, int level) throws CommandException {
if (worldEdit.getConfiguration().logCommands) {
final Logging loggingAnnotation = method.getAnnotation(Logging.class);
final Logging.LogMode logMode;
if (loggingAnnotation == null) {
logMode = null;
} else {
logMode = loggingAnnotation.value();
}
String msg = "WorldEdit: " + player.getName();
if (player.isPlayer()) {
msg += " (in \"" + player.getWorld().getName() + "\")";
}
msg += ": " + StringUtil.joinString(args, " ");
if (logMode != null && player.isPlayer()) {
Vector position = player.getPosition();
final LocalSession session = worldEdit.getSessionManager().get(player);
switch (logMode) {
case PLACEMENT:
try {
position = session.getPlacementPosition(player);
} catch (IncompleteRegionException e) {
break;
}
/* FALL-THROUGH */
case POSITION:
msg += " - Position: " + position;
break;
case ALL:
msg += " - Position: " + position;
/* FALL-THROUGH */
case ORIENTATION_REGION:
msg += " - Orientation: " + player.getCardinalDirection().name();
/* FALL-THROUGH */
case REGION:
try {
msg += " - Region: " + session.getSelection(player.getWorld());
} catch (IncompleteRegionException e) {
break;
}
break;
}
}
getLogger().info(msg);
}
super.invokeMethod(parent, args, player, method, instance, methodArgs, level);
}
}
public static Logger getLogger() {
return logger;
}
}

Datei anzeigen

@ -0,0 +1,89 @@
/*
* 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.extension.platform;
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandsManager;
import com.sk89q.worldedit.BiomeTypes;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.LocalWorld;
import java.util.List;
/**
* Represents a platform that WorldEdit has been implemented for.
*/
public interface Platform {
/**
* Resolves an item name to its ID.
*
* @param name The name to look up
* @return The id that corresponds to the name, or -1 if no such ID exists
*/
int resolveItem(String name);
/**
* Checks if a mob type is valid.
*
* @param type The mob type name to check
* @return Whether the name is a valid mod bype
*/
boolean isValidMobType(String type);
/**
* Reload WorldEdit configuration.
*/
void reload();
/**
* Returns all available biomes.
*
* @return an object containing all the biomes
*/
BiomeTypes getBiomes();
/**
* Schedules the given <code>task</code> to be invoked once every <code>period</code> ticks
* after an initial delay of <code>delay</code> ticks.
*
* @param delay Delay in server ticks before executing first repeat
* @param period Period in server ticks of the task
* @param task Task to be executed
* @return Task id number (-1 if scheduling failed)
*/
int schedule(long delay, long period, Runnable task);
List<LocalWorld> getWorlds();
@Deprecated
void onCommandRegistration(List<Command> commands);
void onCommandRegistration(List<Command> commands, CommandsManager<LocalPlayer> manager);
/**
* Get the configuration from this platform.
*
* @return the configuration
*/
LocalConfiguration getConfiguration();
}

Datei anzeigen

@ -0,0 +1,162 @@
/*
* 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.extension.platform;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.ServerInterface;
import com.sk89q.worldedit.WorldEdit;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Manages registered {@link Platform}s for WorldEdit. Platforms are
* implementations of WorldEdit.
* </p>
* This class is thread-safe.
*/
public class PlatformManager {
private static final Logger logger = Logger.getLogger(PlatformManager.class.getCanonicalName());
private final LocalConfiguration defaultConfig = new DefaultConfiguration();
private final List<Platform> platforms = new ArrayList<Platform>();
private final CommandManager commandManager;
private @Nullable Platform primary = null;
/**
* Create a new platform manager.
*
* @param worldEdit the WorldEdit instance
*/
public PlatformManager(WorldEdit worldEdit) {
checkNotNull(worldEdit);
this.commandManager = new CommandManager(worldEdit);
}
/**
* Register a platform with WorldEdit.
*
* @param platform the platform
* @throws PlatformRejectionException thrown if the registration is rejected
*/
public synchronized void register(Platform platform) throws PlatformRejectionException {
checkNotNull(platform);
logger.log(Level.FINE, "Got request to register " + platform.getClass().getCanonicalName() + " with WorldEdit");
platforms.add(platform);
if (this.primary == null) {
commandManager.register(platform);
this.primary = platform;
}
}
/**
* Unregister a platform from WorldEdit.
*
* @param platform the platform
*/
public synchronized boolean unregister(Platform platform) {
checkNotNull(platform);
boolean removed = platforms.remove(platform);
if (removed) {
logger.log(Level.FINE, "Unregistering " + platform.getClass().getCanonicalName() + " from WorldEdit");
if (platform == primary) {
primary = null;
commandManager.unregister();
}
}
return removed;
}
/**
* Get the command manager.
*
* @return the command manager
*/
public CommandManager getCommandManager() {
return commandManager;
}
/**
* Get the current configuration.
* </p>
* If no platform has been registered yet, then a default configuration
* will be returned.
*
* @return the configuration
*/
public LocalConfiguration getConfiguration() {
Platform platform = primary;
if (platform != null) {
return platform.getConfiguration();
} else {
return defaultConfig;
}
}
/**
* Return a {@link Platform}.
*
* @return a {@link Platform}
* @throws IllegalStateException if no platform has been registered
*/
public Platform getPlatform() throws IllegalStateException {
Platform platform = primary;
if (platform != null) {
return platform;
} else {
throw new IllegalStateException("No platform has been registered");
}
}
/**
* Return a legacy {@link ServerInterface}.
*
* @return a {@link ServerInterface}
* @throws IllegalStateException if no platform has been registered
*/
public ServerInterface getServerInterface() throws IllegalStateException {
Platform platform = primary;
if (platform != null) {
if (platform instanceof ServerInterface) {
return (ServerInterface) platform;
} else {
return new ServerInterfaceAdapter(platform);
}
} else {
throw new IllegalStateException("No platform has been registered");
}
}
/**
* A default configuration for when none is set.
*/
private static class DefaultConfiguration extends LocalConfiguration {
@Override
public void load() {
}
}
}

Datei anzeigen

@ -0,0 +1,49 @@
/*
* 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.extension.platform;
import com.sk89q.worldedit.WorldEditException;
/**
* Thrown when a platform registration request is rejected, which may
* be because another platform is already registered.
*/
public class PlatformRejectionException extends WorldEditException {
/**
* Create with a message.
*
* @param message the message
*/
public PlatformRejectionException(String message) {
super(message);
}
/**
* Create with a message and a cause.
*
* @param message the message
* @param cause the cause
*/
public PlatformRejectionException(String message, Throwable cause) {
super(message, cause);
}
}

Datei anzeigen

@ -0,0 +1,93 @@
/*
* 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.extension.platform;
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandsManager;
import com.sk89q.worldedit.*;
import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Adapts {@link Platform}s into the legacy {@link ServerInterface}.
*/
class ServerInterfaceAdapter extends ServerInterface {
private final Platform platform;
/**
* Create a new adapter.
*
* @param platform the platform
*/
ServerInterfaceAdapter(Platform platform) {
checkNotNull(platform);
this.platform = platform;
}
@Override
public int resolveItem(String name) {
return platform.resolveItem(name);
}
@Override
public boolean isValidMobType(String type) {
return platform.isValidMobType(type);
}
@Override
public void reload() {
platform.reload();
}
@Override
public BiomeTypes getBiomes() {
return platform.getBiomes();
}
@Override
public int schedule(long delay, long period, Runnable task) {
return platform.schedule(delay, period, task);
}
@Override
public List<LocalWorld> getWorlds() {
return platform.getWorlds();
}
@Override
@Deprecated
public void onCommandRegistration(List<Command> commands) {
platform.onCommandRegistration(commands);
}
@Override
public void onCommandRegistration(List<Command> commands, CommandsManager<LocalPlayer> manager) {
platform.onCommandRegistration(commands, manager);
}
@Override
public LocalConfiguration getConfiguration() {
return platform.getConfiguration();
}
}

Datei anzeigen

@ -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.util.logging;
import javax.annotation.Nullable;
import java.io.UnsupportedEncodingException;
import java.util.logging.*;
/**
* A {@link StreamHandler} delegate that allows for the swap and disable of
* another handler. When {@link #setHandler(StreamHandler)} is called with
* null, then records passed onto this handler will be dropped. Otherwise,
* the delegate handler will receive those records.
*/
public class DynamicStreamHandler extends StreamHandler {
private @Nullable StreamHandler handler;
private @Nullable Formatter formatter;
private @Nullable Filter filter;
private @Nullable String encoding;
private Level level = Level.ALL;
/**
* Get the delegate handler.
*
* @return the delegate handler (Which may be null)
*/
public @Nullable synchronized StreamHandler getHandler() {
return handler;
}
/**
* Set the handler.
*
* @param handler the delegate handler (which can be null)
*/
public synchronized void setHandler(@Nullable StreamHandler handler) {
if (this.handler != null) {
this.handler.close();
}
this.handler = handler;
if (handler != null) {
handler.setFormatter(formatter);
handler.setFilter(filter);
try {
handler.setEncoding(encoding);
} catch (UnsupportedEncodingException ignore) {
}
handler.setLevel(level);
}
}
@Override
public synchronized void publish(LogRecord record) {
if (handler != null) {
handler.publish(record);
}
}
@Override
public synchronized void close() throws SecurityException {
if (handler != null) {
handler.close();
}
}
@Override
public void setEncoding(@Nullable String encoding) throws SecurityException, UnsupportedEncodingException {
StreamHandler handler = this.handler;
this.encoding = encoding;
if (handler != null) {
handler.setEncoding(encoding);
}
}
@Override
public boolean isLoggable(LogRecord record) {
StreamHandler handler = this.handler;
return handler != null && handler.isLoggable(record);
}
@Override
public synchronized void flush() {
StreamHandler handler = this.handler;
if (handler != null) {
handler.flush();
}
}
@Override
public void setFormatter(@Nullable Formatter newFormatter) throws SecurityException {
StreamHandler handler = this.handler;
this.formatter = newFormatter;
if (handler != null) {
handler.setFormatter(newFormatter);
}
}
@Override
public Formatter getFormatter() {
StreamHandler handler = this.handler;
Formatter formatter = this.formatter;
if (handler != null) {
return handler.getFormatter();
} else if (formatter != null) {
return formatter;
} else {
return new SimpleFormatter();
}
}
@Override
public String getEncoding() {
StreamHandler handler = this.handler;
String encoding = this.encoding;
if (handler != null) {
return handler.getEncoding();
} else {
return encoding;
}
}
@Override
public void setFilter(@Nullable Filter newFilter) throws SecurityException {
StreamHandler handler = this.handler;
this.filter = newFilter;
if (handler != null) {
handler.setFilter(newFilter);
}
}
@Override
public Filter getFilter() {
StreamHandler handler = this.handler;
Filter filter = this.filter;
if (handler != null) {
return handler.getFilter();
} else {
return filter;
}
}
@Override
public synchronized void setLevel(Level newLevel) throws SecurityException {
if (handler != null) {
handler.setLevel(newLevel);
}
this.level = newLevel;
}
@Override
public synchronized Level getLevel() {
if (handler != null) {
return handler.getLevel();
} else {
return level;
}
}
}

Datei anzeigen

@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.worldedit.util; package com.sk89q.worldedit.util.logging;
import java.util.logging.Formatter; import java.util.logging.Formatter;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
@ -26,9 +26,7 @@ import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
/** /**
* Used for formatting. * A standard logging format for WorldEdit.
*
* @author sk89q
*/ */
public class LogFormat extends Formatter { public class LogFormat extends Formatter {
@Override @Override

Datei anzeigen

@ -0,0 +1,59 @@
/*
* 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.logging;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
/**
* Adds a WorldEdit prefix to WorldEdit's logger messages using a handler.
*/
public final class WorldEditPrefixHandler extends Handler {
private WorldEditPrefixHandler() {
}
@Override
public void publish(LogRecord record) {
String message = record.getMessage();
if (!message.startsWith("WorldEdit: ") && !message.startsWith("[WorldEdit] ")) {
record.setMessage("[WorldEdit] " + message);
}
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
/**
* Add the handler to the following logger name.
*
* @param name the logger name
*/
public static void register(String name) {
Logger.getLogger(name).addHandler(new WorldEditPrefixHandler());
}
}

Datei anzeigen

@ -22,6 +22,7 @@ package com.sk89q.worldedit.spout;
import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.minecraft.util.commands.CommandsManager; import com.sk89q.minecraft.util.commands.CommandsManager;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalPlayer; import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.LocalWorld;
import com.sk89q.worldedit.ServerInterface; import com.sk89q.worldedit.ServerInterface;
@ -102,4 +103,9 @@ public class SpoutServerInterface extends ServerInterface {
spoutCommand.closeSubCommand(); spoutCommand.closeSubCommand();
} }
} }
@Override
public LocalConfiguration getConfiguration() {
return plugin.getLocalConfiguration();
}
} }

Datei anzeigen

@ -21,6 +21,7 @@ package com.sk89q.worldedit.spout;
import com.sk89q.util.yaml.YAMLProcessor; import com.sk89q.util.yaml.YAMLProcessor;
import com.sk89q.worldedit.*; import com.sk89q.worldedit.*;
import com.sk89q.worldedit.extension.platform.PlatformRejectionException;
import com.sk89q.worldedit.extent.inventory.BlockBag; import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Polygonal2DRegion; import com.sk89q.worldedit.regions.Polygonal2DRegion;
@ -99,7 +100,12 @@ public class WorldEditPlugin extends CommonPlugin {
// Setup interfaces // Setup interfaces
server = new SpoutServerInterface(this, getEngine()); server = new SpoutServerInterface(this, getEngine());
controller = new WorldEdit(server, config); controller = WorldEdit.getInstance();
try {
controller.getPlatformManager().register(server);
} catch (PlatformRejectionException e) {
throw new RuntimeException("Failed to register with WorldEdit", e);
}
// Now we can register events! // Now we can register events!
registerEvents(); registerEvents();