Spellscript
Spellscript is a programming language for Minecraft, to be introduced in Hack/Mine v0.6.4 and planned for eventual status as an independent Minecraft mod. Spellscript allows you to thoroughly mod Minecraft while in-game, no recompilation required. In other words, there's no installation hassle, no fiddling with jars, etc-- just writing or copy/pasting code into an in-game text editor, and you're done.
For a tutorial on how to use Spellscript, see Spellscript Tutorial. For a general-purpose language reference, read on...
Contents |
Spellscript Hooks in Hack/Mine
Hack/Mine outsources a growing amount of its functionality to Spellscript, such that users can thoroughly customize their RPG experience. Admins may currently employ Spellscript in the following ways:
In-Game Hooks
- Spellscript-Blocks
- Script-Scheduling
External Hooks
- Command-line Access
- Files in the ".minecraft/spells" directory
Language Mechanics
Spellscript is best construed as a healthy mix of Java and Python. Like Java, it is statically typed and features single-inheritance. Like Python, lexical scopes are signified by indentation and syntax is relatively terse. Where Java would normally require explicit typing, Spellscript does its best to infer type implicitly. Where Python's flexibility allows unsound behavior, Spellscript introduces robustness to keep things sane.
Type System
Spellscript features classes, strings, lists, and function pointers (which follow a pass-by-reference model), and primitive types (which follow a pass-by-value model). The primitive types are bool, int, float, and double. To understand their usage, it's best to look at some code.
def someFunction(int i):
return i * 2
class MyClass:
def __init__(self):
pass
def someMethod(self):
return 2
def String someOtherMethod(self):
return "Derpalerp!"
bool b = True
int i = 3
float f = 2f
double d = 1d
string s = "Hello world!"
string t = None
MyClass m = MyClass()
int$($int) funcPtr = someFunction
A notable distinction from Python is that variables must be declared before they are used. A variable declaration simply goes <type> <name> [= <expression>].
Function and class definitions are similar to those of Python; def signifies a function declaration, and class signifies a class declaration. Function return types may optionally be specified (unless it couldn't be inferred, in which case it is required.)
Literals
Possible literals for Spellscript types go as follows:
| Type | Literals |
|---|---|
| bool | True, False |
| int | 0, -15, 2, 0xf931, etc |
| float | 3.1412f, 1f, -3.f, 0F, .1F, etc |
| double | 3.1412, 1., -3d, 0D, etc |
| string | None, "hello world", "oh hai!", "", etc |
Note that all reference-types (strings, class instances, function pointers, etc) may be assigned None.
Operations
The operations available in Spellscript, and their order of precedence, are as follows:
| Operator | Description | Order |
|---|---|---|
| =, +=, -=, *=, /=, %= | Assignment, Compound Assignment | Right-to-Left |
| is, is not, ==, != | Equality | Right-to-Left |
| or | Boolean OR | Right-to-Left |
| and | Boolean AND | Right-to-Left |
| < <= >= > | Comparison | Right-to-Left |
| + - | Addition, Subtraction | Left-to-Right |
| * / % | Multiplication, Division, Modulo | Left-to-Right |
| not x, -x | Inversion, Negation | Right-to-Left |
| ** | Exponentiation | Right-to-Left |
| x.y, x[y], (x) | Method/Field Reference, Array Indexing, Parenthesis | Left-to-Right |