Kinds
The sort of thing an object is.
Introduction
CraftScript has no real concept of a 'type' in the sense that most programming languages do.
An object can be anything (text, a number, a player) or nothing at all.
In fact, objects are regularly mutated internally while code is running.
CraftScript introduces the concept of a kind to determine what you can (and can't) do with an object.
A kind is the kind of thing an object is.
A kind is a family of co-occurring properties that may be employed for the purposes of inference or explanation.
What kind of thing is a list? Well, it's the kind of thing that has a size, that is either empty or not empty, that contains elements, that can have elements added to or removed from it.
What Does a Kind Do?
Every object has properties, which you access via the %object%[%property%]
syntax.
Even null
has a singular property (type
).
CraftScript uses an object's kind to determine what properties are available and, when a script asks for or tries to change one of these properties, what should happen.
In this example we have an object player
, whose name
property we ask for.
What kind of a thing is player
?
It comes from the self-selector @s
, which retrieves the user running the script.
This could be a Minecraft player, or it could be the server console, or a zombie, or something else.
What the name
property means depends on what kind of thing player
is.
If the script knows what player
's kind is then it will use this to determine what name
means. If it does not know it will first attempt to determine what kind of object player
is.
A kind is also used to determine what to print when an object is interpolated into text. If a number value 10
is treated as the #number
decimal kind, it will print as 10.0
, whereas if it is treated as the #integer
whole number kind, it will print as 10
.
Conversion Between Kinds
Kind Treatment
Some objects can be treated as one kind of thing or another. A Minecraft player can be treated as a #commandsender
, a #player
or an #entity
.
While most kinds like these will inherit from one another (e.g. players have all properties that command senders do, plus some additional functions) there are occasions where we might have an object that is both a kind of #x
and a kind of #y
, but these are separate kinds with no overlap.
In this case we can use the convert
property function of a kind to obtain it as either #x
or #y
.
In the above example, where object
is already an #x
and a #y
, our new x
and y
variables both contain the same object: affecting the object x
is the same as affecting the object y
. The difference between the two variables is that x
is able to use properties from the X-kind, whereas y
is able to use properties from the Y-kind.
Kind Transformation
Alternatively, there may be cases where an object needs to be treated as a different kind of thing, which it resembles but is not directly compatible.
Locations and vectors are both similar kinds of things: both can have x, y and z coordinates, both have a technical magnitude, but each has properties that the other lacks.
That said, locations and vectors resemble each other closely enough that we can convert one to the other.
List of Kinds
This is the list of built-in kinds. Domains can register additional kinds or remove existing ones.
All objects have a type
property, which returns the #kind
of the object. Kind-specific properties are listed below. Some kinds inherit properties from another kind (e.g. integers are a kind of number).
String
Text.
Number
A numerical value.
Integer
A whole number value.
Integers inherit from numbers.
The integer
, floor
, ceil
and round
properties all return this value.
Void
The empty, null value.
The empty has no properties.
Library
A library object. Typically the result of importing a library (e.g. math
).
Kind
A kind-reference (e.g. #string
)
Flag
A special type of data that can be one of a fixed set of flags.
Flags have a set of pre-defined possible values.
Collection
A group of several values that can be looped in a for
statement.
List
A list of objects.
A list is a collection that remembers its order. A list inherits all properties of a collection.
Map
A key <-> value map of objects.
Structure
A user-created object with mutable properties.
Event
An event object.
Different events may provide different property sets.
Executable
An executable object, which can be triggered with the run
statement.
Statement
An exectutable object, usually (part of) a line from a script.
Statements inherit all properties of executables.
Vector
An (x, y, z)
vector, representing an offset from (0, 0, 0)
.
Location
An (x, y, z)
position in a Minecraft world. May also contain a yaw and pitch angle.
Material
Represents a Minecraft material type (e.g. stone
, oak_planks
). This is a discrete universe of values.
This inherits all properties of flags.
There are more than a thousand possible materials, so it is unwise to ask for all material values
unless necessary.
Block State
The property states of a block in the world. This represents what a block could be (e.g. waterlogged slab) rather than a specific block.
This inherits all the properties of a material.
Block
A reference to an actual block in the world.
This inherits all the properties of block states and materials.
Block Entity
The tile entity attached to a block, e.g. a furnace to a furnace block. Most blocks do not have a block entity. Individual block entity types may provide extra properties.
This inherits all properties from block.
Command Sender
Something that can send a command, such as a player, entity, command block or the console.
Player
A player.
Inherits all properties of a command sender.
Unknown
The unknown kind, that all miscellaneous objects are.
This has no specific properties.
Last updated