2011-01-01 01:40:07 +00:00
|
|
|
/*
|
2014-04-03 01:57:25 +00:00
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
2011-01-01 01:40:07 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* 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
|
2011-01-01 01:40:07 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* 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.
|
2011-01-01 01:40:07 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2011-01-01 01:40:07 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-04-03 01:57:25 +00:00
|
|
|
*/
|
2011-01-01 01:40:07 +00:00
|
|
|
|
|
|
|
package com.sk89q.worldedit;
|
|
|
|
|
2018-06-17 05:42:47 +00:00
|
|
|
import static com.sk89q.worldedit.event.platform.Interaction.HIT;
|
|
|
|
import static com.sk89q.worldedit.event.platform.Interaction.OPEN;
|
|
|
|
|
2018-07-24 01:50:57 +00:00
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
import com.google.common.collect.Lists;
|
2014-04-04 00:52:53 +00:00
|
|
|
import com.sk89q.worldedit.blocks.BaseBlock;
|
2015-10-27 06:14:30 +00:00
|
|
|
import com.sk89q.worldedit.blocks.BaseItem;
|
2014-06-27 23:03:29 +00:00
|
|
|
import com.sk89q.worldedit.entity.Player;
|
|
|
|
import com.sk89q.worldedit.event.platform.BlockInteractEvent;
|
2014-06-27 03:07:04 +00:00
|
|
|
import com.sk89q.worldedit.event.platform.InputType;
|
|
|
|
import com.sk89q.worldedit.event.platform.PlayerInputEvent;
|
2014-07-09 00:39:33 +00:00
|
|
|
import com.sk89q.worldedit.extension.factory.BlockFactory;
|
2015-10-27 06:14:30 +00:00
|
|
|
import com.sk89q.worldedit.extension.factory.ItemFactory;
|
2014-07-09 00:39:33 +00:00
|
|
|
import com.sk89q.worldedit.extension.factory.MaskFactory;
|
|
|
|
import com.sk89q.worldedit.extension.factory.PatternFactory;
|
2014-06-28 00:55:39 +00:00
|
|
|
import com.sk89q.worldedit.extension.platform.Actor;
|
2018-06-17 12:04:35 +00:00
|
|
|
import com.sk89q.worldedit.extension.platform.Capability;
|
2014-04-05 03:53:58 +00:00
|
|
|
import com.sk89q.worldedit.extension.platform.Platform;
|
|
|
|
import com.sk89q.worldedit.extension.platform.PlatformManager;
|
2014-04-04 00:52:53 +00:00
|
|
|
import com.sk89q.worldedit.extent.inventory.BlockBag;
|
2012-09-02 20:56:40 +00:00
|
|
|
import com.sk89q.worldedit.scripting.CraftScriptContext;
|
|
|
|
import com.sk89q.worldedit.scripting.CraftScriptEngine;
|
|
|
|
import com.sk89q.worldedit.scripting.RhinoCraftScriptEngine;
|
2014-04-04 02:12:30 +00:00
|
|
|
import com.sk89q.worldedit.session.SessionManager;
|
2014-04-04 00:52:53 +00:00
|
|
|
import com.sk89q.worldedit.session.request.Request;
|
2018-06-16 05:29:48 +00:00
|
|
|
import com.sk89q.worldedit.util.Location;
|
2014-04-03 01:57:25 +00:00
|
|
|
import com.sk89q.worldedit.util.eventbus.EventBus;
|
2014-07-29 14:57:45 +00:00
|
|
|
import com.sk89q.worldedit.util.io.file.FileSelectionAbortedException;
|
|
|
|
import com.sk89q.worldedit.util.io.file.FilenameException;
|
|
|
|
import com.sk89q.worldedit.util.io.file.FilenameResolutionException;
|
|
|
|
import com.sk89q.worldedit.util.io.file.InvalidFilenameException;
|
2014-04-05 03:53:58 +00:00
|
|
|
import com.sk89q.worldedit.util.logging.WorldEditPrefixHandler;
|
2018-07-24 01:50:57 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockType;
|
2014-07-09 21:14:17 +00:00
|
|
|
import com.sk89q.worldedit.world.registry.BundledBlockData;
|
2018-06-17 05:42:47 +00:00
|
|
|
import com.sk89q.worldedit.world.registry.BundledItemData;
|
2018-07-01 12:03:22 +00:00
|
|
|
import com.sk89q.worldedit.world.registry.LegacyMapper;
|
2014-04-03 01:57:25 +00:00
|
|
|
|
2014-07-09 21:14:17 +00:00
|
|
|
import java.io.DataInputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2014-04-05 03:53:58 +00:00
|
|
|
import java.util.HashMap;
|
2018-07-24 01:50:57 +00:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2014-04-05 03:53:58 +00:00
|
|
|
import java.util.Map;
|
2014-07-29 18:04:04 +00:00
|
|
|
import java.util.logging.Level;
|
2014-04-03 01:57:25 +00:00
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2018-06-17 05:42:47 +00:00
|
|
|
import javax.script.ScriptException;
|
2011-01-01 01:40:07 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-05 03:53:58 +00:00
|
|
|
* The entry point and container for a working implementation of WorldEdit.
|
2014-07-29 14:57:45 +00:00
|
|
|
*
|
|
|
|
* <p>An instance handles event handling; block, mask, pattern, etc. registration;
|
2014-04-05 03:53:58 +00:00
|
|
|
* 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
|
2014-07-29 14:57:45 +00:00
|
|
|
* manager retrieved using {@link WorldEdit#getPlatformManager()}.</p>
|
|
|
|
*
|
|
|
|
* <p>An instance of WorldEdit can be retrieved using the static
|
2014-04-05 03:53:58 +00:00
|
|
|
* method {@link WorldEdit#getInstance()}, which is shared among all
|
2014-07-29 14:57:45 +00:00
|
|
|
* platforms within the same classloader hierarchy.</p>
|
2011-01-01 01:40:07 +00:00
|
|
|
*/
|
2011-01-26 18:53:26 +00:00
|
|
|
public class WorldEdit {
|
2014-04-03 01:57:25 +00:00
|
|
|
|
2014-04-05 03:53:58 +00:00
|
|
|
public static final Logger logger = Logger.getLogger(WorldEdit.class.getCanonicalName());
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2014-04-05 03:53:58 +00:00
|
|
|
private final static WorldEdit instance = new WorldEdit();
|
2011-01-31 08:58:29 +00:00
|
|
|
private static String version;
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2014-04-03 01:57:25 +00:00
|
|
|
private final EventBus eventBus = new EventBus();
|
2014-04-05 03:53:58 +00:00
|
|
|
private final PlatformManager platformManager = new PlatformManager(this);
|
2014-04-04 03:48:42 +00:00
|
|
|
private final EditSessionFactory editSessionFactory = new EditSessionFactory.EditSessionFactoryImpl(eventBus);
|
2014-04-04 02:12:30 +00:00
|
|
|
private final SessionManager sessions = new SessionManager(this);
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2014-07-09 00:39:33 +00:00
|
|
|
private final BlockFactory blockFactory = new BlockFactory(this);
|
2015-10-27 06:14:30 +00:00
|
|
|
private final ItemFactory itemFactory = new ItemFactory(this);
|
2014-07-09 00:39:33 +00:00
|
|
|
private final MaskFactory maskFactory = new MaskFactory(this);
|
|
|
|
private final PatternFactory patternFactory = new PatternFactory(this);
|
2014-04-04 00:52:53 +00:00
|
|
|
|
2011-01-31 08:58:29 +00:00
|
|
|
static {
|
2014-04-05 03:53:58 +00:00
|
|
|
WorldEditPrefixHandler.register("com.sk89q.worldedit");
|
2011-01-31 08:58:29 +00:00
|
|
|
getVersion();
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2014-04-05 03:53:58 +00:00
|
|
|
private WorldEdit() {
|
|
|
|
}
|
|
|
|
|
2011-01-01 01:40:07 +00:00
|
|
|
/**
|
2014-04-05 03:53:58 +00:00
|
|
|
* Gets the current instance of this class.
|
2014-07-29 14:57:45 +00:00
|
|
|
*
|
|
|
|
* <p>An instance will always be available, but no platform may yet be
|
2014-04-05 03:53:58 +00:00
|
|
|
* registered with WorldEdit, meaning that a number of operations
|
2014-07-29 14:57:45 +00:00
|
|
|
* may fail. However, event handlers can be registered.</p>
|
2012-03-10 07:11:51 +00:00
|
|
|
*
|
2014-04-05 03:53:58 +00:00
|
|
|
* @return an instance of WorldEdit.
|
2011-01-01 01:40:07 +00:00
|
|
|
*/
|
2014-04-05 03:53:58 +00:00
|
|
|
public static WorldEdit getInstance() {
|
|
|
|
return instance;
|
2011-01-01 01:40:07 +00:00
|
|
|
}
|
2011-11-20 05:02:54 +00:00
|
|
|
|
2012-10-18 10:34:09 +00:00
|
|
|
/**
|
2014-04-05 03:53:58 +00:00
|
|
|
* Get the platform manager, where platforms (that implement WorldEdit)
|
|
|
|
* can be registered and information about registered platforms can
|
|
|
|
* be queried.
|
|
|
|
*
|
|
|
|
* @return the platform manager
|
2012-10-18 10:34:09 +00:00
|
|
|
*/
|
2014-04-05 03:53:58 +00:00
|
|
|
public PlatformManager getPlatformManager() {
|
|
|
|
return platformManager;
|
2012-10-18 10:34:09 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 01:57:25 +00:00
|
|
|
/**
|
|
|
|
* Get the event bus for WorldEdit.
|
2014-07-29 14:57:45 +00:00
|
|
|
*
|
|
|
|
* <p>Event handlers can be registered on the event bus.</p>
|
2014-04-03 01:57:25 +00:00
|
|
|
*
|
|
|
|
* @return the event bus
|
|
|
|
*/
|
|
|
|
public EventBus getEventBus() {
|
|
|
|
return eventBus;
|
|
|
|
}
|
|
|
|
|
2014-04-04 00:52:53 +00:00
|
|
|
/**
|
2014-07-09 00:39:33 +00:00
|
|
|
* Get the block factory from which new {@link BaseBlock}s can be
|
2014-04-04 00:52:53 +00:00
|
|
|
* constructed.
|
|
|
|
*
|
2014-07-09 00:39:33 +00:00
|
|
|
* @return the block factory
|
2014-04-04 00:52:53 +00:00
|
|
|
*/
|
2014-07-09 00:39:33 +00:00
|
|
|
public BlockFactory getBlockFactory() {
|
|
|
|
return blockFactory;
|
2014-04-04 00:52:53 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 06:14:30 +00:00
|
|
|
/**
|
|
|
|
* Get the item factory from which new {@link BaseItem}s can be
|
|
|
|
* constructed.
|
|
|
|
*
|
|
|
|
* @return the item factory
|
|
|
|
*/
|
|
|
|
public ItemFactory getItemFactory() {
|
|
|
|
return itemFactory;
|
|
|
|
}
|
|
|
|
|
2014-04-04 00:52:53 +00:00
|
|
|
/**
|
2014-07-09 00:39:33 +00:00
|
|
|
* Get the mask factory from which new {@link com.sk89q.worldedit.function.mask.Mask}s
|
2014-04-04 00:52:53 +00:00
|
|
|
* can be constructed.
|
|
|
|
*
|
2014-07-09 00:39:33 +00:00
|
|
|
* @return the mask factory
|
2014-04-04 00:52:53 +00:00
|
|
|
*/
|
2014-07-09 00:39:33 +00:00
|
|
|
public MaskFactory getMaskFactory() {
|
|
|
|
return maskFactory;
|
2014-04-04 00:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-09 00:39:33 +00:00
|
|
|
* Get the pattern factory from which new {@link com.sk89q.worldedit.function.pattern.Pattern}s
|
2014-04-04 00:52:53 +00:00
|
|
|
* can be constructed.
|
|
|
|
*
|
2014-07-09 00:39:33 +00:00
|
|
|
* @return the pattern factory
|
2014-04-04 00:52:53 +00:00
|
|
|
*/
|
2014-07-09 00:39:33 +00:00
|
|
|
public PatternFactory getPatternFactory() {
|
|
|
|
return patternFactory;
|
2014-04-04 00:52:53 +00:00
|
|
|
}
|
|
|
|
|
2011-11-20 05:02:54 +00:00
|
|
|
/**
|
2014-04-04 02:12:30 +00:00
|
|
|
* Return the session manager.
|
2011-08-29 21:24:35 +00:00
|
|
|
*
|
2014-04-04 02:12:30 +00:00
|
|
|
* @return the session manager
|
|
|
|
*/
|
|
|
|
public SessionManager getSessionManager() {
|
|
|
|
return sessions;
|
|
|
|
}
|
|
|
|
|
2011-01-31 04:40:22 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2014-04-05 04:02:56 +00:00
|
|
|
* On success, a {@code java.io.File} object will be returned.
|
2012-03-10 07:11:51 +00:00
|
|
|
*
|
2014-04-05 04:02:56 +00:00
|
|
|
* @param player the player
|
2011-01-31 04:40:22 +00:00
|
|
|
* @param dir sub-directory to look in
|
|
|
|
* @param filename filename (user-submitted)
|
|
|
|
* @param defaultExt append an extension if missing one, null to not use
|
2011-01-31 05:32:52 +00:00
|
|
|
* @param extensions list of extensions, null for any
|
2014-04-05 04:02:56 +00:00
|
|
|
* @return a file
|
|
|
|
* @throws FilenameException thrown if the filename is invalid
|
2011-01-31 04:40:22 +00:00
|
|
|
*/
|
2014-06-28 08:01:49 +00:00
|
|
|
public File getSafeSaveFile(Player player, File dir, String filename, String defaultExt, String... extensions) throws FilenameException {
|
2011-01-31 05:32:52 +00:00
|
|
|
return getSafeFile(player, dir, filename, defaultExt, extensions, true);
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-31 05:32:52 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2014-04-05 04:02:56 +00:00
|
|
|
* On success, a {@code java.io.File} object will be returned.
|
2012-03-10 07:11:51 +00:00
|
|
|
*
|
2014-04-05 04:02:56 +00:00
|
|
|
* @param player the player
|
2011-01-31 05:32:52 +00:00
|
|
|
* @param dir sub-directory to look in
|
|
|
|
* @param filename filename (user-submitted)
|
|
|
|
* @param defaultExt append an extension if missing one, null to not use
|
|
|
|
* @param extensions list of extensions, null for any
|
2014-04-05 04:02:56 +00:00
|
|
|
* @return a file
|
|
|
|
* @throws FilenameException thrown if the filename is invalid
|
2011-01-31 05:32:52 +00:00
|
|
|
*/
|
2014-06-28 08:01:49 +00:00
|
|
|
public File getSafeOpenFile(Player player, File dir, String filename, String defaultExt, String... extensions) throws FilenameException {
|
2011-01-31 05:32:52 +00:00
|
|
|
return getSafeFile(player, dir, filename, defaultExt, extensions, false);
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-31 05:32:52 +00:00
|
|
|
/**
|
|
|
|
* Get a safe path to a file.
|
2012-03-10 07:11:51 +00:00
|
|
|
*
|
2014-04-05 04:02:56 +00:00
|
|
|
* @param player the player
|
|
|
|
* @param dir sub-directory to look in
|
|
|
|
* @param filename filename (user-submitted)
|
|
|
|
* @param defaultExt append an extension if missing one, null to not use
|
|
|
|
* @param extensions list of extensions, null for any
|
|
|
|
* @param isSave true if the purpose is for saving
|
|
|
|
* @return a file
|
|
|
|
* @throws FilenameException thrown if the filename is invalid
|
2011-01-31 05:32:52 +00:00
|
|
|
*/
|
2014-06-28 08:01:49 +00:00
|
|
|
private File getSafeFile(Player player, File dir, String filename, String defaultExt, String[] extensions, boolean isSave) throws FilenameException {
|
2011-12-27 00:28:51 +00:00
|
|
|
if (extensions != null && (extensions.length == 1 && extensions[0] == null)) extensions = null;
|
2011-11-27 04:29:49 +00:00
|
|
|
|
2011-01-31 04:40:22 +00:00
|
|
|
File f;
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-31 04:40:22 +00:00
|
|
|
if (filename.equals("#")) {
|
2011-01-31 05:32:52 +00:00
|
|
|
if (isSave) {
|
|
|
|
f = player.openFileSaveDialog(extensions);
|
|
|
|
} else {
|
|
|
|
f = player.openFileOpenDialog(extensions);
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-31 04:40:22 +00:00
|
|
|
if (f == null) {
|
|
|
|
throw new FileSelectionAbortedException("No file selected");
|
|
|
|
}
|
|
|
|
} else {
|
2018-07-24 01:50:57 +00:00
|
|
|
List<String> exts = extensions == null ? ImmutableList.of(defaultExt) : Lists.asList(defaultExt, extensions);
|
|
|
|
return getSafeFileWithExtensions(dir, filename, exts, isSave);
|
2011-01-31 04:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
String filePath = f.getCanonicalPath();
|
|
|
|
String dirPath = dir.getCanonicalPath();
|
|
|
|
|
2014-04-05 03:53:58 +00:00
|
|
|
if (!filePath.substring(0, dirPath.length()).equals(dirPath) && !getConfiguration().allowSymlinks) {
|
2011-01-31 06:06:06 +00:00
|
|
|
throw new FilenameResolutionException(filename,
|
2011-01-31 04:40:22 +00:00
|
|
|
"Path is outside allowable root");
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-31 04:40:22 +00:00
|
|
|
return f;
|
|
|
|
} catch (IOException e) {
|
2011-01-31 06:06:06 +00:00
|
|
|
throw new FilenameResolutionException(filename,
|
2011-01-31 04:40:22 +00:00
|
|
|
"Failed to resolve path");
|
|
|
|
}
|
|
|
|
}
|
2011-01-29 10:05:22 +00:00
|
|
|
|
2018-07-24 01:50:57 +00:00
|
|
|
private File getSafeFileWithExtensions(File dir, String filename, List<String> exts, boolean isSave) throws InvalidFilenameException {
|
|
|
|
if (isSave) {
|
|
|
|
// First is default, only use that.
|
|
|
|
if (exts.size() != 1) {
|
|
|
|
exts = exts.subList(0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
File result = null;
|
|
|
|
for (Iterator<String> iter = exts.iterator(); iter.hasNext() && (result == null || !result.exists());) {
|
|
|
|
result = getSafeFileWithExtension(dir, filename, iter.next());
|
|
|
|
}
|
|
|
|
if (result == null) {
|
|
|
|
throw new InvalidFilenameException(filename, "Invalid characters or extension missing");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private File getSafeFileWithExtension(File dir, String filename, String extension) {
|
|
|
|
if (extension != null && filename.lastIndexOf('.') == -1) {
|
|
|
|
filename += "." + extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!checkFilename(filename)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new File(dir, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean checkFilename(String filename) {
|
|
|
|
return filename.matches("^[A-Za-z0-9_\\- \\./\\\\'\\$@~!%\\^\\*\\(\\)\\[\\]\\+\\{\\},\\?]+\\.[A-Za-z0-9]+$");
|
|
|
|
}
|
|
|
|
|
2018-07-17 14:42:09 +00:00
|
|
|
/**
|
|
|
|
* Load the bundled mappings.
|
|
|
|
*/
|
|
|
|
public void loadMappings() {
|
|
|
|
BundledBlockData.getInstance(); // Load block registry
|
|
|
|
BundledItemData.getInstance(); // Load item registry
|
|
|
|
LegacyMapper.getInstance(); // Load item registry
|
|
|
|
}
|
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
/**
|
|
|
|
* Checks to see if the specified radius is within bounds.
|
|
|
|
*
|
2014-04-05 04:02:56 +00:00
|
|
|
* @param radius the radius
|
2011-01-29 10:05:22 +00:00
|
|
|
* @throws MaxRadiusException
|
|
|
|
*/
|
2011-08-15 12:35:21 +00:00
|
|
|
public void checkMaxRadius(double radius) throws MaxRadiusException {
|
2014-04-05 03:53:58 +00:00
|
|
|
if (getConfiguration().maxRadius > 0 && radius > getConfiguration().maxRadius) {
|
2011-01-29 10:05:22 +00:00
|
|
|
throw new MaxRadiusException();
|
|
|
|
}
|
2011-01-01 01:40:07 +00:00
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2013-08-03 01:33:11 +00:00
|
|
|
/**
|
|
|
|
* Checks to see if the specified brush radius is within bounds.
|
|
|
|
*
|
2014-04-05 04:02:56 +00:00
|
|
|
* @param radius the radius
|
2013-08-03 01:33:11 +00:00
|
|
|
* @throws MaxBrushRadiusException
|
|
|
|
*/
|
|
|
|
public void checkMaxBrushRadius(double radius) throws MaxBrushRadiusException {
|
2014-04-05 03:53:58 +00:00
|
|
|
if (getConfiguration().maxBrushRadius > 0 && radius > getConfiguration().maxBrushRadius) {
|
2013-08-03 01:33:11 +00:00
|
|
|
throw new MaxBrushRadiusException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-29 18:14:04 +00:00
|
|
|
/**
|
|
|
|
* Get a file relative to the defined working directory. If the specified
|
|
|
|
* path is absolute, then the working directory is not used.
|
2012-03-10 07:11:51 +00:00
|
|
|
*
|
2014-04-05 04:02:56 +00:00
|
|
|
* @param path the subpath under the working directory
|
|
|
|
* @return a working directory
|
2011-01-29 18:14:04 +00:00
|
|
|
*/
|
|
|
|
public File getWorkingDirectoryFile(String path) {
|
|
|
|
File f = new File(path);
|
|
|
|
if (f.isAbsolute()) {
|
|
|
|
return f;
|
|
|
|
}
|
2013-11-29 22:35:06 +00:00
|
|
|
|
2014-04-05 03:53:58 +00:00
|
|
|
return new File(getConfiguration().getWorkingDirectory(), path);
|
2011-01-29 18:14:04 +00:00
|
|
|
}
|
2011-01-01 01:40:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the direction vector for a player's direction. May return
|
|
|
|
* null if a direction could not be found.
|
2012-03-10 07:11:51 +00:00
|
|
|
*
|
2014-04-05 04:02:56 +00:00
|
|
|
* @param player the player
|
|
|
|
* @param dirStr the direction string
|
|
|
|
* @return a direction vector
|
|
|
|
* @throws UnknownDirectionException thrown if the direction is not known
|
2011-01-01 01:40:07 +00:00
|
|
|
*/
|
2014-06-27 23:03:29 +00:00
|
|
|
public Vector getDirection(Player player, String dirStr) throws UnknownDirectionException {
|
2011-10-24 23:03:37 +00:00
|
|
|
dirStr = dirStr.toLowerCase();
|
2011-09-05 01:53:39 +00:00
|
|
|
|
2011-10-24 23:03:37 +00:00
|
|
|
final PlayerDirection dir = getPlayerDirection(player, dirStr);
|
2011-09-05 01:53:39 +00:00
|
|
|
|
2011-10-24 23:03:37 +00:00
|
|
|
switch (dir) {
|
|
|
|
case WEST:
|
|
|
|
case EAST:
|
|
|
|
case SOUTH:
|
|
|
|
case NORTH:
|
|
|
|
case UP:
|
|
|
|
case DOWN:
|
|
|
|
return dir.vector();
|
2011-09-05 01:53:39 +00:00
|
|
|
|
|
|
|
default:
|
2011-10-24 23:03:37 +00:00
|
|
|
throw new UnknownDirectionException(dir.name());
|
2011-09-05 01:53:39 +00:00
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
}
|
2011-03-21 14:01:15 +00:00
|
|
|
|
2014-04-05 04:02:56 +00:00
|
|
|
/**
|
|
|
|
* Get the direction vector for a player's direction. May return
|
|
|
|
* null if a direction could not be found.
|
|
|
|
*
|
|
|
|
* @param player the player
|
|
|
|
* @param dirStr the direction string
|
|
|
|
* @return a direction enum value
|
|
|
|
* @throws UnknownDirectionException thrown if the direction is not known
|
|
|
|
*/
|
2014-06-27 23:03:29 +00:00
|
|
|
private PlayerDirection getPlayerDirection(Player player, String dirStr) throws UnknownDirectionException {
|
2011-10-24 23:03:37 +00:00
|
|
|
final PlayerDirection dir;
|
2011-03-21 14:01:15 +00:00
|
|
|
|
2011-09-05 01:53:39 +00:00
|
|
|
switch (dirStr.charAt(0)) {
|
|
|
|
case 'w':
|
2011-10-24 23:03:37 +00:00
|
|
|
dir = PlayerDirection.WEST;
|
|
|
|
break;
|
2011-09-05 01:53:39 +00:00
|
|
|
|
|
|
|
case 'e':
|
2011-10-24 23:03:37 +00:00
|
|
|
dir = PlayerDirection.EAST;
|
|
|
|
break;
|
2011-09-05 01:53:39 +00:00
|
|
|
|
|
|
|
case 's':
|
|
|
|
if (dirStr.indexOf('w') > 0) {
|
2011-10-24 23:03:37 +00:00
|
|
|
return PlayerDirection.SOUTH_WEST;
|
2011-03-21 14:01:15 +00:00
|
|
|
}
|
|
|
|
|
2011-09-05 01:53:39 +00:00
|
|
|
if (dirStr.indexOf('e') > 0) {
|
2011-10-24 23:03:37 +00:00
|
|
|
return PlayerDirection.SOUTH_EAST;
|
2011-09-05 01:53:39 +00:00
|
|
|
}
|
2011-10-24 23:03:37 +00:00
|
|
|
dir = PlayerDirection.SOUTH;
|
|
|
|
break;
|
2011-09-05 01:53:39 +00:00
|
|
|
|
|
|
|
case 'n':
|
|
|
|
if (dirStr.indexOf('w') > 0) {
|
2011-10-24 23:03:37 +00:00
|
|
|
return PlayerDirection.NORTH_WEST;
|
2011-09-05 01:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dirStr.indexOf('e') > 0) {
|
2011-10-24 23:03:37 +00:00
|
|
|
return PlayerDirection.NORTH_EAST;
|
2011-09-05 01:53:39 +00:00
|
|
|
}
|
2011-10-24 23:03:37 +00:00
|
|
|
dir = PlayerDirection.NORTH;
|
|
|
|
break;
|
2011-09-05 01:53:39 +00:00
|
|
|
|
|
|
|
case 'u':
|
2011-10-24 23:03:37 +00:00
|
|
|
dir = PlayerDirection.UP;
|
|
|
|
break;
|
2011-09-05 01:53:39 +00:00
|
|
|
|
|
|
|
case 'd':
|
2011-10-24 23:03:37 +00:00
|
|
|
dir = PlayerDirection.DOWN;
|
|
|
|
break;
|
|
|
|
|
2011-10-24 23:07:08 +00:00
|
|
|
case 'm': // me
|
|
|
|
case 'f': // forward
|
2011-10-24 23:03:37 +00:00
|
|
|
dir = player.getCardinalDirection(0);
|
|
|
|
break;
|
2011-09-05 01:53:39 +00:00
|
|
|
|
2011-10-24 23:07:08 +00:00
|
|
|
case 'b': // back
|
|
|
|
dir = player.getCardinalDirection(180);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l': // left
|
|
|
|
dir = player.getCardinalDirection(-90);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r': // right
|
|
|
|
dir = player.getCardinalDirection(90);
|
|
|
|
break;
|
|
|
|
|
2011-09-05 01:53:39 +00:00
|
|
|
default:
|
|
|
|
throw new UnknownDirectionException(dirStr);
|
|
|
|
}
|
2011-10-24 23:03:37 +00:00
|
|
|
return dir;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-19 10:03:41 +00:00
|
|
|
/**
|
|
|
|
* Flush a block bag's changes to a player.
|
2012-03-10 07:11:51 +00:00
|
|
|
*
|
2014-06-28 08:01:49 +00:00
|
|
|
* @param actor the actor
|
2014-04-05 04:02:56 +00:00
|
|
|
* @param editSession the edit session
|
2011-01-19 10:03:41 +00:00
|
|
|
*/
|
2014-06-28 00:55:39 +00:00
|
|
|
public void flushBlockBag(Actor actor, EditSession editSession) {
|
2011-01-19 10:03:41 +00:00
|
|
|
BlockBag blockBag = editSession.getBlockBag();
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-19 10:03:41 +00:00
|
|
|
if (blockBag != null) {
|
|
|
|
blockBag.flushChanges();
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2018-07-01 13:38:56 +00:00
|
|
|
Map<BlockType, Integer> missingBlocks = editSession.popMissingBlocks();
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2014-06-28 00:55:39 +00:00
|
|
|
if (!missingBlocks.isEmpty()) {
|
2011-01-19 10:03:41 +00:00
|
|
|
StringBuilder str = new StringBuilder();
|
|
|
|
str.append("Missing these blocks: ");
|
|
|
|
int size = missingBlocks.size();
|
|
|
|
int i = 0;
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2018-07-01 13:38:56 +00:00
|
|
|
for (BlockType id : missingBlocks.keySet()) {
|
|
|
|
str.append(id.getName());
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2014-04-05 04:02:56 +00:00
|
|
|
str.append(" [Amt: ").append(missingBlocks.get(id)).append("]");
|
2012-12-29 00:26:57 +00:00
|
|
|
|
2011-07-15 07:00:48 +00:00
|
|
|
++i;
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-19 10:03:41 +00:00
|
|
|
if (i != size) {
|
|
|
|
str.append(", ");
|
|
|
|
}
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2014-06-28 00:55:39 +00:00
|
|
|
actor.printError(str.toString());
|
2011-01-19 10:03:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-01 01:40:07 +00:00
|
|
|
/**
|
2011-08-06 14:22:02 +00:00
|
|
|
* Called on arm swing.
|
2012-03-10 07:11:51 +00:00
|
|
|
*
|
2014-04-05 04:02:56 +00:00
|
|
|
* @param player the player
|
|
|
|
* @return true if the swing was handled
|
2011-01-01 01:40:07 +00:00
|
|
|
*/
|
2014-06-28 08:01:49 +00:00
|
|
|
public boolean handleArmSwing(Player player) {
|
2014-06-27 03:07:04 +00:00
|
|
|
PlayerInputEvent event = new PlayerInputEvent(player, InputType.PRIMARY);
|
|
|
|
getEventBus().post(event);
|
|
|
|
return event.isCancelled();
|
2011-01-26 18:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called on right click (not on a block).
|
2012-03-10 07:11:51 +00:00
|
|
|
*
|
2014-04-05 04:02:56 +00:00
|
|
|
* @param player the player
|
|
|
|
* @return true if the right click was handled
|
2011-01-26 18:52:53 +00:00
|
|
|
*/
|
2014-06-28 08:01:49 +00:00
|
|
|
public boolean handleRightClick(Player player) {
|
2014-06-27 03:07:04 +00:00
|
|
|
PlayerInputEvent event = new PlayerInputEvent(player, InputType.SECONDARY);
|
|
|
|
getEventBus().post(event);
|
|
|
|
return event.isCancelled();
|
2011-01-01 01:40:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called on right click.
|
|
|
|
*
|
2014-04-05 04:02:56 +00:00
|
|
|
* @param player the player
|
|
|
|
* @param clicked the clicked block
|
2011-01-01 01:40:07 +00:00
|
|
|
* @return false if you want the action to go through
|
|
|
|
*/
|
2018-06-16 05:29:48 +00:00
|
|
|
public boolean handleBlockRightClick(Player player, Location clicked) {
|
|
|
|
BlockInteractEvent event = new BlockInteractEvent(player, clicked, OPEN);
|
2014-06-27 00:07:08 +00:00
|
|
|
getEventBus().post(event);
|
|
|
|
return event.isCancelled();
|
2011-01-01 01:40:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called on left click.
|
|
|
|
*
|
2014-04-05 04:02:56 +00:00
|
|
|
* @param player the player
|
|
|
|
* @param clicked the clicked block
|
2011-01-01 01:40:07 +00:00
|
|
|
* @return false if you want the action to go through
|
|
|
|
*/
|
2018-06-16 05:29:48 +00:00
|
|
|
public boolean handleBlockLeftClick(Player player, Location clicked) {
|
|
|
|
BlockInteractEvent event = new BlockInteractEvent(player, clicked, HIT);
|
2014-06-26 23:56:40 +00:00
|
|
|
getEventBus().post(event);
|
|
|
|
return event.isCancelled();
|
2011-01-01 01:40:07 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 10:41:08 +00:00
|
|
|
/**
|
|
|
|
* Executes a WorldEdit script.
|
2012-03-10 07:11:51 +00:00
|
|
|
*
|
2014-04-05 04:02:56 +00:00
|
|
|
* @param player the player
|
|
|
|
* @param f the script file to execute
|
|
|
|
* @param args arguments for the script
|
2012-03-10 07:11:51 +00:00
|
|
|
* @throws WorldEditException
|
2011-01-22 10:41:08 +00:00
|
|
|
*/
|
2014-06-28 08:01:49 +00:00
|
|
|
public void runScript(Player player, File f, String[] args) throws WorldEditException {
|
2014-04-04 00:52:53 +00:00
|
|
|
Request.reset();
|
|
|
|
|
2011-11-23 01:29:48 +00:00
|
|
|
String filename = f.getPath();
|
2011-01-22 10:41:08 +00:00
|
|
|
int index = filename.lastIndexOf(".");
|
2018-06-17 05:42:47 +00:00
|
|
|
String ext = filename.substring(index + 1);
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-22 10:41:08 +00:00
|
|
|
if (!ext.equalsIgnoreCase("js")) {
|
|
|
|
player.printError("Only .js scripts are currently supported");
|
|
|
|
return;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-22 10:41:08 +00:00
|
|
|
String script;
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-22 10:41:08 +00:00
|
|
|
try {
|
2011-01-22 21:34:05 +00:00
|
|
|
InputStream file;
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-22 21:34:05 +00:00
|
|
|
if (!f.exists()) {
|
2014-04-05 04:02:56 +00:00
|
|
|
file = WorldEdit.class.getResourceAsStream("craftscripts/" + filename);
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-22 21:34:05 +00:00
|
|
|
if (file == null) {
|
|
|
|
player.printError("Script does not exist: " + filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file = new FileInputStream(f);
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-22 21:34:05 +00:00
|
|
|
DataInputStream in = new DataInputStream(file);
|
2011-01-22 10:41:08 +00:00
|
|
|
byte[] data = new byte[in.available()];
|
|
|
|
in.readFully(data);
|
2011-01-22 21:34:05 +00:00
|
|
|
in.close();
|
2011-01-22 10:41:08 +00:00
|
|
|
script = new String(data, 0, data.length, "utf-8");
|
|
|
|
} catch (IOException e) {
|
|
|
|
player.printError("Script read error: " + e.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2014-06-28 08:01:49 +00:00
|
|
|
LocalSession session = getSessionManager().get(player);
|
2018-06-17 12:04:35 +00:00
|
|
|
CraftScriptContext scriptContext = new CraftScriptContext(this, getPlatformManager().queryCapability(Capability.USER_COMMANDS),
|
|
|
|
getConfiguration(), session, player, args);
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2018-06-17 05:42:47 +00:00
|
|
|
CraftScriptEngine engine;
|
2011-01-31 03:49:57 +00:00
|
|
|
|
2011-01-22 21:34:05 +00:00
|
|
|
try {
|
|
|
|
engine = new RhinoCraftScriptEngine();
|
|
|
|
} catch (NoClassDefFoundError e) {
|
2011-05-01 02:04:03 +00:00
|
|
|
player.printError("Failed to find an installed script engine.");
|
|
|
|
player.printError("Please see http://wiki.sk89q.com/wiki/WorldEdit/Installation");
|
|
|
|
return;
|
2011-01-22 10:41:08 +00:00
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2014-04-05 03:53:58 +00:00
|
|
|
engine.setTimeLimit(getConfiguration().scriptTimeout);
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2018-06-16 06:36:55 +00:00
|
|
|
Map<String, Object> vars = new HashMap<>();
|
2011-01-22 21:34:05 +00:00
|
|
|
vars.put("argv", args);
|
|
|
|
vars.put("context", scriptContext);
|
|
|
|
vars.put("player", player);
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-22 10:41:08 +00:00
|
|
|
try {
|
2011-01-22 21:34:05 +00:00
|
|
|
engine.evaluate(script, filename, vars);
|
2011-01-22 10:41:08 +00:00
|
|
|
} catch (ScriptException e) {
|
2011-11-23 01:29:48 +00:00
|
|
|
player.printError("Failed to execute:");
|
2011-01-22 21:34:05 +00:00
|
|
|
player.printRaw(e.getMessage());
|
2014-07-29 18:04:04 +00:00
|
|
|
logger.log(Level.WARNING, "Failed to execute script", e);
|
2018-06-16 06:36:55 +00:00
|
|
|
} catch (NumberFormatException | WorldEditException e) {
|
2011-01-22 23:38:04 +00:00
|
|
|
throw e;
|
|
|
|
} catch (Throwable e) {
|
2011-01-23 00:23:04 +00:00
|
|
|
player.printError("Failed to execute (see console):");
|
2011-01-22 23:38:04 +00:00
|
|
|
player.printRaw(e.getClass().getCanonicalName());
|
2014-07-29 18:04:04 +00:00
|
|
|
logger.log(Level.WARNING, "Failed to execute script", e);
|
2011-01-22 10:41:08 +00:00
|
|
|
} finally {
|
2011-01-22 23:38:04 +00:00
|
|
|
for (EditSession editSession : scriptContext.getEditSessions()) {
|
2011-01-31 04:41:34 +00:00
|
|
|
editSession.flushQueue();
|
2011-01-22 10:41:08 +00:00
|
|
|
session.remember(editSession);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
/**
|
|
|
|
* Get Worldedit's configuration.
|
2012-03-10 07:11:51 +00:00
|
|
|
*
|
2014-04-05 03:53:58 +00:00
|
|
|
* @return a configuration
|
2011-01-29 10:05:22 +00:00
|
|
|
*/
|
|
|
|
public LocalConfiguration getConfiguration() {
|
2014-04-05 03:53:58 +00:00
|
|
|
return getPlatformManager().getConfiguration();
|
2011-01-29 10:05:22 +00:00
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2012-10-18 10:34:09 +00:00
|
|
|
/**
|
2014-04-03 01:57:25 +00:00
|
|
|
* Get a factory for {@link EditSession}s.
|
2012-10-18 10:34:09 +00:00
|
|
|
*/
|
|
|
|
public EditSessionFactory getEditSessionFactory() {
|
2014-04-03 01:57:25 +00:00
|
|
|
return editSessionFactory;
|
2012-10-18 10:34:09 +00:00
|
|
|
}
|
|
|
|
|
2011-01-31 08:58:29 +00:00
|
|
|
/**
|
|
|
|
* Get the version.
|
|
|
|
*
|
2014-04-05 04:02:56 +00:00
|
|
|
* @return the version of WorldEdit
|
2011-01-31 08:58:29 +00:00
|
|
|
*/
|
|
|
|
public static String getVersion() {
|
|
|
|
if (version != null) {
|
|
|
|
return version;
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-31 08:58:29 +00:00
|
|
|
Package p = WorldEdit.class.getPackage();
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-31 08:58:29 +00:00
|
|
|
if (p == null) {
|
|
|
|
p = Package.getPackage("com.sk89q.worldedit");
|
|
|
|
}
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-31 08:58:29 +00:00
|
|
|
if (p == null) {
|
|
|
|
version = "(unknown)";
|
|
|
|
} else {
|
|
|
|
version = p.getImplementationVersion();
|
2011-11-23 01:29:48 +00:00
|
|
|
|
2011-01-31 08:58:29 +00:00
|
|
|
if (version == null) {
|
|
|
|
version = "(unknown)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return version;
|
|
|
|
}
|
2011-11-02 16:10:07 +00:00
|
|
|
|
2011-01-01 01:40:07 +00:00
|
|
|
}
|