Skip to content

Instantly share code, notes, and snippets.

@marvin-roesch
Created August 22, 2014 14:41
Show Gist options
  • Save marvin-roesch/e0ed38dff8a2eccd193f to your computer and use it in GitHub Desktop.
Save marvin-roesch/e0ed38dff8a2eccd193f to your computer and use it in GitHub Desktop.
class BlockRift extends BaseBlock(Names.Blocks.Rift, Core.CreativeTab, Material.rock) with TileProvider[TileRift] with TileRendering[TileRift] {
initTileProvider()
initTileRendering()
}
trait TileProvider[T <: TileEntity] extends BaseBlock {
this: TileProvider[T] =>
def initTileProvider()(implicit ev: ClassTag[T]): Unit = {
_clazz = ev.runtimeClass.asInstanceOf[Class[T]]
}
/**
* @param meta check if there is a TE for the given metadata
* @return true, if the supplied teClass is not null (by default)
*/
override def hasTileEntity(meta: Int): Boolean = true
/**
* By default, creates a new TE from the teClass
* @param world a world object
* @param meta the meta of the block to get the TE for
* @return a TileEntity instance, if hasTileEntity returns true
*/
override def createTileEntity(world: World, meta: Int): TileEntity = _clazz.newInstance()
override def onBlockEventReceived(world: World, x: Int, y: Int, z: Int, eventId: Int, eventArgument: Int): Boolean = {
val tileentity: TileEntity = world.getTileEntity(x, y, z)
if (tileentity != null) tileentity.receiveClientEvent(eventId, eventArgument) else false
}
def tileClass = _clazz
private var _clazz: Class[T] = _
}
object TileRendering {
type Tile = T forSome {type T <: TileEntity}
}
trait TileRendering[T <: TileRenderer[Tile]] extends BaseBlock with TileProvider[Tile] {
def initTileRendering()(implicit ev: ClassTag[T]): Unit = {
if (FMLCommonHandler.instance().getSide.isClient)
init(ev)
}
@SideOnly(Side.CLIENT)
private def init(ev: ClassTag[T]): Unit = {
_clazz = ev.runtimeClass.asInstanceOf[Class[T]]
}
@SideOnly(Side.CLIENT)
def registerRenderer(): Unit = {
val renderer = _clazz.newInstance()
ClientRegistry.bindTileEntitySpecialRenderer(tileClass, renderer)
}
override def getRenderType: Int = -1
override def renderAsNormalBlock(): Boolean = false
override def isOpaqueCube: Boolean = false
private var _clazz: Class[T] = _
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment