BKCommonLib/Block

From BergerHealer Wiki
Jump to navigation Jump to search

« Go back

Introduction

BlockUtil contains various handy Block-related utilities. It offers:

  • get Block State cast to a certain type (null on failure)
  • get Block Data cast to a certain type (null on failure)
  • Shortcuts for get/setFacing
  • get Block attached face or Block
  • Toggling all levers attached to a block
  • get all Block States in a cuboid area

See also: MaterialUtil for block type equality checks. It contains various material properties, for example, to check whether a material is a type of rails or sign, or whether a block is a power source. It can save some time implementing your own systems if used right.

Block State

Unlike block.getState(), this version won't throw random NPE's when the tile entity is glitched. There is also an overload to cast to a certain type of state, for example, a chest. This can be useful to reduce the amount of code needed to get a chest state. For example:

Chest chest = BlockUtil.getState(block, Chest.class);
if (chest != null) {
    // Work with it
}

Block Material Data

Optimized version of Block.getState().getData() - creates Material Data without first creating a (useless) BlockState instance. It also includes several safety checks to avoid malformed data resulting in unusable MaterialData classes. Glitched signs are fixed right up! Usage is very similar to the getState methods.

Getting block states in an area

This optimized method allows obtaining all nearby chests, signs, furnaces and other tile entities that have specific Block States. This way you can heavily optimize your previous for-loop check. Note that this method is not cross-thread safe - only use it on the main thread.