Skip to content

Instantly share code, notes, and snippets.

@jan-matula
Last active October 21, 2021 17:21
Show Gist options
  • Save jan-matula/b33a4fde474fd75148576ed07e021ae1 to your computer and use it in GitHub Desktop.
Save jan-matula/b33a4fde474fd75148576ed07e021ae1 to your computer and use it in GitHub Desktop.
Minecraft 1.7: Facing values and update suppression

Facing values and update suppression

It is quite commonly known that data-value 6 pistons can cause update suppression or that data-value 6 hoppers and droppers crash the game. Although I will not go into the code of any specific block, this document should explain why these exceptions happen.

Facing values

The facing directions are typically represented in 3 bits as follows:

- +
Y 0b00_0 or 0 0b00_1 or 1
Z 0b01_0 or 2 0b01_1 or 3
X 0b10_0 or 4 0b10_1 or 5

This value is, in most cases, going to be located in the 3 least significant bits of a block's metadata and will be extracted by a bitwise AND. Simply put, the game will always take the first 3 bits of the metadata and this is no matter if the value is greather than 5.

While orientation 7 does not correspond to any real direction, it is an intended facing values. Blocks using these facing values will be placed by the player with orientation 7 and initialize them based on the players position right after the placement.

The Facing utility class and the ArrayIndexOutOfBoundsException

There is a class that basically holds a collection of utility look-up tables.

0 (Y-) 1 (Y+) 2 (Z-) 3 (Z+) 4 (X-) 5 (X+)
Facing.offsetsYForSide -1 +1 0 0 0 0
Facing.offsetsZForSide 0 0 -1 +1 0 0
Facing.offsetsXForSide 0 0 0 0 -1 +1
0 (Y-) 1 (Y+) 2 (Z-) 3 (Z+) 4 (X-) 5 (X+)
Facing.oppositeSide 1 0 3 2 5 4
0 (Y-) 1 (Y+) 2 (Z-) 3 (Z+) 4 (X-) 5 (X+)
Facing.facings "DOWN" "UP" "NORTH" "SOUTH" "WEST" "EAST"

Note that the Facing.facings look-up table is never used anywhere in the game's code.

There is no entry for the orientations 6 and 7. This makes one wonder: What would happen if the game were to try and look-up an offset for one of those orientations? What if we made, let's say, a piston with metadata 6? (Pistons with orientation 7, for example, will just refuse to update.)

In a situation where a program attempts to access an index of an array that simply is not there, Java will throw a ArrayIndexOutOfBoundsException. This can cause a game crash or update suppression in case the exception is caught.

Opportunities for crashes

TODO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment