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 String[] allowedBlocks;
private boolean mapScriptCommands = false;
/**
* Construct an instance of the plugin.
@ -65,7 +66,7 @@ public WorldEdit() {
commands.put("/editload", "[Filename] - Load .schematic into clipboard");
commands.put("/editsave", "[Filename] - Save clipboard to .schematic");
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 void enable() {
}
allowedBlocks = properties.getString("allowed-blocks", DEFAULT_ALLOWED_BLOCKS).split(",");
mapScriptCommands = properties.getBoolean("map-script-commands", true);
etc controller = etc.getInstance();
@ -200,6 +202,21 @@ public boolean onCommand(Player player, String[] split) {
if (etc.getInstance().canUseCommand(player.getName(), split[0])) {
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;
@ -418,13 +435,17 @@ private boolean handleEditCommand(Player player, String[] split)
return true;
// Run an editscript
} else if (split[0].equalsIgnoreCase("/script")) {
// Run a script
} else if (split[0].equalsIgnoreCase("/editscript")) {
checkArgs(split, 1);
String filename = split[1].replace("\0", "") + ".js";
String[] args = new String[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;
}
@ -612,7 +633,8 @@ private int fillY(EditSession editSession, int x, int cy,
* @param args
*/
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 f = new File("editscripts", filename);
@ -621,9 +643,9 @@ private boolean runScript(Player player, WorldEditSession session,
String dirPath = dir.getCanonicalPath();
if (!filePath.substring(0, dirPath.length()).equals(dirPath)) {
player.sendMessage(Colors.Rose + "Script file does not exist.");
throw new NoSuchScriptException();
} else if (!f.exists()) {
player.sendMessage(Colors.Rose + "Script file does not exist.");
throw new NoSuchScriptException();
} else {
// Read file
StringBuffer buffer = new StringBuffer();

View File

@ -1,5 +1,3 @@
package com.sk89q.worldedit;
// $Id$
/*
* WorldEdit
@ -19,6 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit;
/**
*
* @author sk89q

View File

@ -1,5 +1,3 @@
package com.sk89q.worldedit;
// $Id$
/*
* WorldEdit
@ -19,6 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit;
/**
* Raised when a region is not fully defined.
*

View File

@ -1,5 +1,3 @@
package com.sk89q.worldedit;
// $Id$
/*
* WorldEdit
@ -19,6 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit;
/**
*
* @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$
/*
* WorldEdit
@ -19,6 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit;
/**
* Thrown when no item exist by the ID.
*