Added direct script->command mapping support; fixed some comments in exception files.

This commit is contained in:
sk89q 2010-10-03 13:23:43 -07:00
parent 33fb2abb54
commit ddfb7f21be
6 changed files with 65 additions and 15 deletions

View File

@ -41,6 +41,7 @@ public class WorldEdit extends Plugin {
private PropertiesFile properties; private PropertiesFile properties;
private String[] allowedBlocks; private String[] allowedBlocks;
private boolean mapScriptCommands = false;
/** /**
* Construct an instance of the plugin. * Construct an instance of the plugin.
@ -65,7 +66,7 @@ public class WorldEdit extends Plugin {
commands.put("/editload", "[Filename] - Load .schematic into clipboard"); commands.put("/editload", "[Filename] - Load .schematic into clipboard");
commands.put("/editsave", "[Filename] - Save clipboard to .schematic"); commands.put("/editsave", "[Filename] - Save clipboard to .schematic");
commands.put("/editfill", "<ID> <Radius> <Depth> - Fill a hole"); commands.put("/editfill", "<ID> <Radius> <Depth> - Fill a hole");
commands.put("/script", "[Filename] <Args...> - Run a WorldEdit script"); commands.put("/editscript", "[Filename] <Args...> - Run a WorldEdit script");
} }
/** /**
@ -166,6 +167,7 @@ public class WorldEdit extends Plugin {
} }
allowedBlocks = properties.getString("allowed-blocks", DEFAULT_ALLOWED_BLOCKS).split(","); allowedBlocks = properties.getString("allowed-blocks", DEFAULT_ALLOWED_BLOCKS).split(",");
mapScriptCommands = properties.getBoolean("map-script-commands", true);
etc controller = etc.getInstance(); etc controller = etc.getInstance();
@ -200,6 +202,21 @@ public class WorldEdit extends Plugin {
if (etc.getInstance().canUseCommand(player.getName(), split[0])) { if (etc.getInstance().canUseCommand(player.getName(), split[0])) {
return handleEditCommand(player, split); return handleEditCommand(player, split);
} }
} else {
// See if there is a script by the same name
if (mapScriptCommands) {
if (etc.getInstance().canUseCommand(player.getName(), "/editscript")) {
String filename = split[0].substring(1) + ".js";
String[] args = new String[split.length - 1];
System.arraycopy(split, 1, args, 0, split.length - 1);
try {
return runScript(player, getSession(player), new EditSession(),
filename, args);
} catch (NoSuchScriptException nse) {
return false;
}
}
}
} }
return false; return false;
@ -418,13 +435,17 @@ public class WorldEdit extends Plugin {
return true; return true;
// Run an editscript // Run a script
} else if (split[0].equalsIgnoreCase("/script")) { } else if (split[0].equalsIgnoreCase("/editscript")) {
checkArgs(split, 1); checkArgs(split, 1);
String filename = split[1].replace("\0", "") + ".js"; String filename = split[1].replace("\0", "") + ".js";
String[] args = new String[split.length - 2]; String[] args = new String[split.length - 2];
System.arraycopy(split, 2, args, 0, split.length - 2); System.arraycopy(split, 2, args, 0, split.length - 2);
runScript(player, session, editSession, filename, args); try {
runScript(player, session, editSession, filename, args);
} catch (NoSuchScriptException e) {
player.sendMessage(Colors.Rose + "Script file does not exist.");
}
return true; return true;
} }
@ -612,7 +633,8 @@ public class WorldEdit extends Plugin {
* @param args * @param args
*/ */
private boolean runScript(Player player, WorldEditSession session, private boolean runScript(Player player, WorldEditSession session,
EditSession editSession, String filename, String[] args) { EditSession editSession, String filename, String[] args) throws
NoSuchScriptException {
File dir = new File("editscripts"); File dir = new File("editscripts");
File f = new File("editscripts", filename); File f = new File("editscripts", filename);
@ -621,9 +643,9 @@ public class WorldEdit extends Plugin {
String dirPath = dir.getCanonicalPath(); String dirPath = dir.getCanonicalPath();
if (!filePath.substring(0, dirPath.length()).equals(dirPath)) { if (!filePath.substring(0, dirPath.length()).equals(dirPath)) {
player.sendMessage(Colors.Rose + "Script file does not exist."); throw new NoSuchScriptException();
} else if (!f.exists()) { } else if (!f.exists()) {
player.sendMessage(Colors.Rose + "Script file does not exist."); throw new NoSuchScriptException();
} else { } else {
// Read file // Read file
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();

View File

@ -1,5 +1,3 @@
package com.sk89q.worldedit;
// $Id$ // $Id$
/* /*
* WorldEdit * WorldEdit
@ -19,6 +17,8 @@ package com.sk89q.worldedit;
* 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;
/** /**
* *
* @author sk89q * @author sk89q

View File

@ -1,5 +1,3 @@
package com.sk89q.worldedit;
// $Id$ // $Id$
/* /*
* WorldEdit * WorldEdit
@ -19,6 +17,8 @@ package com.sk89q.worldedit;
* 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;
/** /**
* Raised when a region is not fully defined. * Raised when a region is not fully defined.
* *

View File

@ -1,5 +1,3 @@
package com.sk89q.worldedit;
// $Id$ // $Id$
/* /*
* WorldEdit * WorldEdit
@ -19,6 +17,8 @@ package com.sk89q.worldedit;
* 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;
/** /**
* *
* @author sk89q * @author sk89q

View File

@ -0,0 +1,28 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit;
/**
*
* @author Albert
*/
public class NoSuchScriptException extends Exception {
}

View File

@ -1,5 +1,3 @@
package com.sk89q.worldedit;
// $Id$ // $Id$
/* /*
* WorldEdit * WorldEdit
@ -19,6 +17,8 @@ package com.sk89q.worldedit;
* 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;
/** /**
* Thrown when no item exist by the ID. * Thrown when no item exist by the ID.
* *