Wizards of Lua

The Art of Spell Crafting


Block - All There is to Know About a Block

The Block class is a basic unit of structure in Minecraft.

An instance of this class represents either one of the following types:

  1. A live block reference - that is a block at a specific world position. It can be accessed by spell.block. ‘Live’ means that whenever the block at that position changes, the internal state of this object will change as well.

  2. An immutable block value - it’s a block that exists independently of the world. It can be created, e.g. by calling Blocks.get() or Block:copy().

Both types are ‘unmodifiable’, meaning that you can’t change their internal states directly. Instead, if you want to change a block in the world, you will need to assign a new value to the spell.block field. This will copy the state of the right-hand value into the block at the given spell’s position.

The Block class is a subtype of the Object class and inherits all its properties and functions.

Here is an overview of the Block properties:

Property Type read / write
data table r
material Material r
name string r
nbt table r

Here is an overview of the Block functions:

Function Parameters Results
 asItem  amount Item
 copy  Block
 withData  data Block
 withNbt  nbt Block

Properties

Below you find short descriptions about each of the properties and some examples about how to use them in your spells.


data : table

The ‘data’ value is a table of block-specifc key-value pairs that provide human readable information about the block’s data. For example, a grass block has a property called ‘snowy’ which can be true or false, and a furnace has a property called ‘facing’ which can be one of ‘north’, ‘east’, ‘south’, and ‘west’.


material : Material

The ‘material’ give you some insights in how this block behaves. Please have a look into the Material Book for more information.


name : string

This is the basic name of the block, e.g. ‘grass’, ‘stone’, or ‘air’.


nbt : table

The ‘nbt’ value (short for Named Binary Tag) is a table of block-specifc key-value pairs about the block’s entity. Only a small amount of blocks do have a block entity. For example, the sign’s entity contains information about its text, and the chest’s entity contains information about its content.


Functions

Below you find short descriptions about each of the functions and some examples about how to use them in your spells.


asItem (amount) -> Item

The ‘asItem’ function returns this block as an item of the given amount.

Example

Creating an item from the block at the spell’s current position and putting it into the wizard’s offhand.

item=spell.block:asItem(); spell.owner.offhand=item

Creating a full stack of of the block at the spell’s current position and putting it into the wizard’s offhand.

item=spell.block:asItem(64); spell.owner.offhand=item

copy () -> Block

the ‘copy’ function returns a copy of this block.


withData (data) -> Block

The ‘withData’ function returns a modified copy of the given block with the given table values as the block’s data.

Example

Creating a smooth diorite block and placing it at the spell’s position.

spell.block = Blocks.get( "stone"):withData(
  { variant = "smooth_diorite"}
)

Example

Creating a bundle of full grown wheat on top of the block at the spell’s position.

spell:move( "up")
spell.block = Blocks.get( "wheat"):withData(
  { age = 7}
)

withNbt (nbt) -> Block

The ‘withNbt’ function returns a modified copy of this block with the given table values for the block’s entity.

Example

Creating a standing sign with the name of the current spell’s owner written onto it and placing it at the spell’s position.

spell.block = Blocks.get( "standing_sign"):withNbt( {
  Text1 = '{"text":"'..spell.owner.name..'"}'
})

Example

Creating a wall sign showing the current time.

spell:move("back")
spell.rotationYaw=spell.rotationYaw+180
spell.block=Blocks.get("wall_sign"):withData({facing=spell.facing})
while true do
  local time=Time.getDate("HH:mm:ss")
  spell.block=spell.block:withNbt({Text1= '{"text":"'..time..'"}'})
  sleep(20)
end

Example

Putting a stack of 64 wheat bundles into slot no. 5 of the chest (or the shulker box) at the spell’s position.

spell.block = spell.block:withNbt( {
  Items = {
    { Count = 64, Damage = 0, Slot = 5,
      id = "minecraft:wheat"
    }
  }
})