Files
bdlib/src/net/bdew/lib/rotate/BaseRotateableBlock.scala
T
2014-01-13 20:40:45 +02:00

92 lines
3.4 KiB
Scala

/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.rotate
import cpw.mods.fml.relauncher.Side
import cpw.mods.fml.relauncher.SideOnly
import net.minecraft.block.Block
import net.minecraft.entity.EntityLivingBase
import net.minecraft.item.ItemStack
import net.minecraft.util.Icon
import net.minecraft.util.MathHelper
import net.minecraft.world.IBlockAccess
import net.minecraft.world.World
import net.minecraftforge.common.ForgeDirection
import java.util.EnumSet
/**
* Basic logic for block rotatation
*/
trait BaseRotateableBlock extends Block {
/**
* Override this method to provide your textures, also called when rendering as item
*/
def getIcon(meta: Int, kind: IconType.Value): Icon
/**
* Override this method to provide your textures, if you need to check other state
*/
def getIcon(world: IBlockAccess, x: Int, y: Int, z: Int, kind: IconType.Value): Icon = getIcon(world.getBlockMetadata(x, y, z), kind)
/**
* Set of valid rotations, default is all of them
*/
def getValidFacings: EnumSet[ForgeDirection] = {
return EnumSet.allOf(classOf[ForgeDirection])
}
/**
* Rotation to show when it's unavailable (like rendering item in inventory)
*/
def getDefaultFacing: ForgeDirection = ForgeDirection.UP
/**
* Those will be overrided to provide concrete storage
*/
def setFacing(world: World, x: Int, y: Int, z: Int, facing: ForgeDirection)
def getFacing(world: IBlockAccess, x: Int, y: Int, z: Int): ForgeDirection
override def rotateBlock(worldObj: World, x: Int, y: Int, z: Int, axis: ForgeDirection): Boolean = {
if (getValidFacings.contains(axis)) {
setFacing(worldObj, x, y, z, axis)
return true
}
return false
}
@SideOnly(Side.CLIENT)
override def getIcon(side: Int, meta: Int): Icon = getIcon(meta, IconType.fromSideAndDir(side, getDefaultFacing))
@SideOnly(Side.CLIENT)
override def getBlockTexture(world: IBlockAccess, x: Int, y: Int, z: Int, side: Int): Icon = getIcon(world, x, y, z, IconType.fromSideAndDir(side, getFacing(world, x, y, z)))
override def getValidRotations(worldObj: World, x: Int, y: Int, z: Int): Array[ForgeDirection] = getValidFacings.toArray(Array.empty[ForgeDirection])
override def onBlockPlacedBy(world: World, x: Int, y: Int, z: Int, ent: EntityLivingBase, stack: ItemStack) {
val validRotations = getValidFacings
var dir = getDefaultFacing
if (ent != null) {
val pitch: Int = Math.round(ent.rotationPitch)
val yaw: Int = MathHelper.floor_double(ent.rotationYaw * 4.0F / 360.0F + 0.5D) & 3
dir = yaw match {
case _ if pitch >= 50 && validRotations.contains(ForgeDirection.UP) => ForgeDirection.UP
case _ if pitch <= -50 && validRotations.contains(ForgeDirection.DOWN) => ForgeDirection.DOWN
case 0 if validRotations.contains(ForgeDirection.NORTH) => ForgeDirection.NORTH
case 1 if validRotations.contains(ForgeDirection.EAST) => ForgeDirection.EAST
case 2 if validRotations.contains(ForgeDirection.SOUTH) => ForgeDirection.SOUTH
case 3 if validRotations.contains(ForgeDirection.WEST) => ForgeDirection.WEST
case _ => dir
}
}
setFacing(world, x, y, z, dir)
super.onBlockPlacedBy(world, x, y, z, ent, stack)
}
}