2011-01-22 10:41:08 +00:00
|
|
|
// $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.scripting;
|
|
|
|
|
2011-01-22 23:38:04 +00:00
|
|
|
import java.io.File;
|
2011-01-22 10:41:08 +00:00
|
|
|
import java.util.ArrayList;
|
2011-01-22 23:38:04 +00:00
|
|
|
import java.util.Collections;
|
2011-01-22 10:41:08 +00:00
|
|
|
import java.util.List;
|
2011-01-22 23:38:04 +00:00
|
|
|
import java.util.Set;
|
2011-01-22 10:41:08 +00:00
|
|
|
import com.sk89q.worldedit.DisallowedItemException;
|
|
|
|
import com.sk89q.worldedit.EditSession;
|
2011-01-31 04:40:22 +00:00
|
|
|
import com.sk89q.worldedit.FilenameException;
|
2011-01-22 10:41:08 +00:00
|
|
|
import com.sk89q.worldedit.LocalConfiguration;
|
|
|
|
import com.sk89q.worldedit.LocalPlayer;
|
|
|
|
import com.sk89q.worldedit.LocalSession;
|
|
|
|
import com.sk89q.worldedit.ServerInterface;
|
|
|
|
import com.sk89q.worldedit.UnknownItemException;
|
2011-01-26 18:53:26 +00:00
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
2011-01-22 10:41:08 +00:00
|
|
|
import com.sk89q.worldedit.blocks.BaseBlock;
|
2011-01-29 10:05:22 +00:00
|
|
|
import com.sk89q.worldedit.commands.InsufficientArgumentsException;
|
2011-01-22 23:38:04 +00:00
|
|
|
import com.sk89q.worldedit.patterns.Pattern;
|
2011-01-22 10:41:08 +00:00
|
|
|
|
2011-01-22 23:38:04 +00:00
|
|
|
/**
|
|
|
|
* The context given to scripts.
|
|
|
|
*
|
|
|
|
* @author sk89q
|
|
|
|
*/
|
2011-01-22 21:34:05 +00:00
|
|
|
public class CraftScriptContext extends CraftScriptEnvironment {
|
2011-01-22 10:41:08 +00:00
|
|
|
private List<EditSession> editSessions = new ArrayList<EditSession>();
|
2011-01-22 23:38:04 +00:00
|
|
|
private String[] args;
|
2011-01-22 10:41:08 +00:00
|
|
|
|
2011-01-26 18:53:26 +00:00
|
|
|
public CraftScriptContext(WorldEdit controller,
|
2011-01-22 18:53:53 +00:00
|
|
|
ServerInterface server, LocalConfiguration config,
|
2011-01-22 23:38:04 +00:00
|
|
|
LocalSession session, LocalPlayer player, String[] args) {
|
2011-01-22 18:53:53 +00:00
|
|
|
super(controller, server, config, session, player);
|
2011-01-22 23:38:04 +00:00
|
|
|
this.args = args;
|
2011-01-22 10:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 23:38:04 +00:00
|
|
|
/**
|
|
|
|
* Get an edit session. Every subsequent call returns a new edit session.
|
|
|
|
* Usually you only need to use one edit session.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public EditSession remember() {
|
2011-01-22 10:41:08 +00:00
|
|
|
EditSession editSession =
|
|
|
|
new EditSession(server, player.getWorld(),
|
|
|
|
session.getBlockChangeLimit(), session.getBlockBag(player));
|
2011-01-31 04:41:34 +00:00
|
|
|
editSession.enableQueue();
|
2011-01-22 10:41:08 +00:00
|
|
|
editSessions.add(editSession);
|
|
|
|
return editSession;
|
|
|
|
}
|
|
|
|
|
2011-01-22 23:38:04 +00:00
|
|
|
/**
|
|
|
|
* Get the player.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2011-01-22 10:41:08 +00:00
|
|
|
public LocalPlayer getPlayer() {
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
2011-01-22 23:38:04 +00:00
|
|
|
/**
|
|
|
|
* Get the player's session.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2011-01-22 10:41:08 +00:00
|
|
|
public LocalSession getSession() {
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2011-01-22 23:38:04 +00:00
|
|
|
/**
|
|
|
|
* Get the configuration for WorldEdit.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2011-01-22 10:41:08 +00:00
|
|
|
public LocalConfiguration getConfiguration() {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2011-01-22 23:38:04 +00:00
|
|
|
/**
|
|
|
|
* Get a list of edit sessions that have been created.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public List<EditSession> getEditSessions() {
|
|
|
|
return Collections.unmodifiableList(editSessions);
|
2011-01-22 10:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 23:38:04 +00:00
|
|
|
/**
|
|
|
|
* Print a regular message to the user.
|
|
|
|
*
|
|
|
|
* @param msg
|
|
|
|
*/
|
2011-01-22 10:41:08 +00:00
|
|
|
public void print(String msg) {
|
|
|
|
player.print(msg);
|
|
|
|
}
|
|
|
|
|
2011-01-22 23:38:04 +00:00
|
|
|
/**
|
|
|
|
* Print an error message to the user.
|
|
|
|
*
|
|
|
|
* @param msg
|
|
|
|
*/
|
2011-01-22 10:41:08 +00:00
|
|
|
public void error(String msg) {
|
|
|
|
player.printError(msg);
|
|
|
|
}
|
|
|
|
|
2011-01-22 23:38:04 +00:00
|
|
|
/**
|
|
|
|
* Print an raw message to the user.
|
|
|
|
*
|
|
|
|
* @param msg
|
|
|
|
*/
|
|
|
|
public void printRaw(String msg) {
|
|
|
|
player.printRaw(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks to make sure that there are enough but not too many arguments.
|
|
|
|
*
|
|
|
|
* @param args
|
|
|
|
* @param min
|
|
|
|
* @param max -1 for no maximum
|
|
|
|
* @param usage usage string
|
|
|
|
* @throws InsufficientArgumentsException
|
|
|
|
*/
|
|
|
|
public void checkArgs(int min, int max, String usage)
|
|
|
|
throws InsufficientArgumentsException {
|
|
|
|
if (args.length <= min || (max != -1 && args.length - 1 > max)) {
|
|
|
|
throw new InsufficientArgumentsException("Usage: " + usage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an item ID from an item name or an item ID number.
|
|
|
|
*
|
|
|
|
* @param arg
|
|
|
|
* @param allAllowed true to ignore blacklists
|
|
|
|
* @return
|
|
|
|
* @throws UnknownItemException
|
|
|
|
* @throws DisallowedItemException
|
|
|
|
*/
|
|
|
|
public BaseBlock getBlock(String arg, boolean allAllowed)
|
|
|
|
throws UnknownItemException, DisallowedItemException {
|
|
|
|
return controller.getBlock(player, arg, allAllowed);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a block.
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* @return
|
|
|
|
* @throws UnknownItemException
|
|
|
|
* @throws DisallowedItemException
|
|
|
|
*/
|
|
|
|
public BaseBlock getBlock(String id)
|
|
|
|
throws UnknownItemException, DisallowedItemException {
|
|
|
|
return controller.getBlock(player, id, false);
|
2011-01-22 10:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 23:38:04 +00:00
|
|
|
/**
|
|
|
|
* Get a list of blocks as a set. This returns a Pattern.
|
|
|
|
*
|
|
|
|
* @param list
|
|
|
|
* @return pattern
|
|
|
|
*/
|
|
|
|
public Pattern getBlockPattern(String list)
|
|
|
|
throws UnknownItemException, DisallowedItemException {
|
|
|
|
return controller.getBlockPattern(player, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of blocks as a set.
|
|
|
|
*
|
|
|
|
* @param list
|
|
|
|
* @param allBlocksAllowed
|
|
|
|
* @return set
|
|
|
|
*/
|
|
|
|
public Set<Integer> getBlockIDs(String list, boolean allBlocksAllowed)
|
|
|
|
throws UnknownItemException, DisallowedItemException {
|
|
|
|
return controller.getBlockIDs(player, list, allBlocksAllowed);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the path to a file. This method will check to see if the filename
|
|
|
|
* has valid characters and has an extension. It also prevents directory
|
|
|
|
* traversal exploits by checking the root directory and the file directory.
|
2011-01-31 04:40:22 +00:00
|
|
|
* On success, a <code>java.io.File</code> object will be returned.
|
2011-01-22 23:38:04 +00:00
|
|
|
*
|
|
|
|
* <p>Use this method if you need to read a file from a directory.</p>
|
|
|
|
*
|
2011-01-31 04:40:22 +00:00
|
|
|
* @param folder sub-directory to look in
|
2011-01-22 23:38:04 +00:00
|
|
|
* @param filename filename (user-submitted)
|
|
|
|
* @return
|
2011-01-31 04:40:22 +00:00
|
|
|
* @throws FilenameException
|
2011-01-22 23:38:04 +00:00
|
|
|
*/
|
2011-01-31 05:32:52 +00:00
|
|
|
@Deprecated
|
2011-01-31 04:40:22 +00:00
|
|
|
public File getSafeFile(String folder, String filename) throws FilenameException {
|
|
|
|
File dir = controller.getWorkingDirectoryFile(folder);
|
2011-01-31 05:32:52 +00:00
|
|
|
return controller.getSafeOpenFile(player, dir, filename, null, null);
|
2011-01-31 04:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-31 05:32:52 +00:00
|
|
|
* Gets the path to a file for opening. This method will check to see if the
|
|
|
|
* filename has valid characters and has an extension. It also prevents
|
|
|
|
* directory traversal exploits by checking the root directory and the file
|
|
|
|
* directory. On success, a <code>java.io.File</code> object will be
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
* <p>Use this method if you need to read a file from a directory.</p>
|
|
|
|
*
|
|
|
|
* @param folder sub-directory to look in
|
|
|
|
* @param filename filename (user-submitted)
|
|
|
|
* @param defaultExt default extension to append if there is none
|
|
|
|
* @param exts list of extensions for file open dialog, null for no filter
|
|
|
|
* @return
|
|
|
|
* @throws FilenameException
|
|
|
|
*/
|
|
|
|
public File getSafeOpenFile(String folder, String filename,
|
|
|
|
String defaultExt, String[] exts)
|
|
|
|
throws FilenameException {
|
|
|
|
File dir = controller.getWorkingDirectoryFile(folder);
|
|
|
|
return controller.getSafeOpenFile(player, dir, filename, defaultExt, exts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the path to a file for saving. This method will check to see if the
|
|
|
|
* filename has valid characters and has an extension. It also prevents
|
|
|
|
* directory traversal exploits by checking the root directory and the file
|
|
|
|
* directory. On success, a <code>java.io.File</code> object will be
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
* <p>Use this method if you need to read a file from a directory.</p>
|
2011-01-31 04:40:22 +00:00
|
|
|
*
|
|
|
|
* @param folder sub-directory to look in
|
|
|
|
* @param filename filename (user-submitted)
|
|
|
|
* @param defaultExt default extension to append if there is none
|
2011-01-31 05:32:52 +00:00
|
|
|
* @param exts list of extensions for file save dialog, null for no filter
|
2011-01-31 04:40:22 +00:00
|
|
|
* @return
|
|
|
|
* @throws FilenameException
|
|
|
|
*/
|
2011-01-31 05:32:52 +00:00
|
|
|
public File getSafeSaveFile(String folder, String filename,
|
|
|
|
String defaultExt, String[] exts)
|
2011-01-31 04:40:22 +00:00
|
|
|
throws FilenameException {
|
|
|
|
File dir = controller.getWorkingDirectoryFile(folder);
|
2011-01-31 05:32:52 +00:00
|
|
|
return controller.getSafeSaveFile(player, dir, filename, defaultExt, exts);
|
2011-01-22 10:41:08 +00:00
|
|
|
}
|
|
|
|
}
|