- 欢迎来到Minecraft插件百科!
- 对百科编辑一脸懵逼?帮助:快速入门带您快速熟悉百科编辑!
- 因近日遭受攻击,百科现已限制编辑,有意编辑请加入插件百科企鹅群:223812289
WorldEdit/CraftScript
- 点击此处开始翻译。
- 如本模板出现在原文存档页面,请注意更新主页面后,仍需要去除此处该模板。
- 如当前页面已经没有需要翻译的内容,请删去待翻译模板。
- 有标题的大篇幅文章,如果短时间内无法全部翻译,请先把所有的标题翻译出来,以便之后的贡献者选择与翻译章节内容。
Scripting in WorldEdit allows you to write world manipulation code without having to learn Java or compile your code. Scripts, called CraftScripts in WorldEdit, and are written in JavaScript and go into your craftscripts/ directory. The advantages of writing scripts with WorldEdit are:
- Hook right into WorldEdit's undo/redo system
- Use WorldEdit's block place prioritization
- Accept WorldEdit's powerful block type syntax (///set sign:3)
- Get the region selected by the user
The Mozilla Rhino JavaScript engine bundled with Java 6 and above is used to evaluate the scripts. However, if an independent version of Rhino is found in WorldEdit's classpath, it will be used instead. For maximum portability, write scripts for Java's built-in version of Rhino.
Scripting support was re-added in v2.13 after having been removed in v0.8. However, scripts written for v0.8 and below are not compatible.
Check out WorldEdit's API documentation.
Introduction
Scripts have the following three variables in their global namespace:
context
is an instance of CraftScriptContextplayer
is a copy of the player, an instance of LocalPlayerargv
is a Java array of arguments as strings
To run scripts, use //cs yourscript.js.
Working with blocks
All block editing in WorldEdit is done through an EditSession
. This object handles history and block placement order all automatically. To get an edit session for your own script, use:
var sess = context.remember();
Every time you call that method, you will get a new EditSession
, so be sure to keep one around. To set blocks, rather than giving a block type number, you give a BaseBlock
. The reason is that blocks contain data (and in some cases, complex data like chest contents), and if only block types were passed around, a lot of data would be lost. BaseBlock
is an independent representation of a block; it doesn't know where it is in the world. That means that you can pass a BaseBlock
around everywhere and reuse it. BaseBlock
s remember block type and block data. For more complex blocks such as signs and chests, you will use a BaseBlock
derivative such as SignBlock
.
Note that because BaseBlock
is in the com.sk89q.worldedit.blocks namespace, it had to be imported first. BlockID
has a list of block types. The first argument for setBlock()
is a Vector
indicating the position in the world.
To get blocks, use getBlock()
on EditSession
. You'll get back a BaseBlock
too.
Processing arguments
Arguments are passed in under the argv
variable. If you need to check whether the right number of arguments was provided by the user, you can use CraftScriptContext.checkArgs()
.
If you need to resolve a block name to a BaseBlock
, you can use CraftScriptContext.getBlock()
. That method checks the block blacklist, however. If you need a block for, for example, replacing, you can ignore the blacklist by adding a boolean "true" for a third parameter.
What happens if the user inputs an invalid block? An exception will be raised and if you don't catch it, the user will be informed about their error and your script will be halted.
Working with Java packages
To import a java package, you can use the following syntax:
importPackage(Packages.package.name.here);
You have access to all of Java's vast library as it is installed. WorldEdit's classes are also fully available.