electrodynamics/src/main/scala/edx/basic/blocks/TileTurntable.scala

106 lines
3 KiB
Scala
Raw Normal View History

2015-01-14 12:06:03 +01:00
package edx.basic.blocks
2014-03-23 10:19:47 +01:00
import codechicken.multipart.TileMultipart
2014-09-07 05:50:03 +02:00
import cpw.mods.fml.relauncher.{Side, SideOnly}
2015-01-14 12:06:03 +01:00
import edx.core.Reference
2014-07-19 01:48:40 +02:00
import net.minecraft.block.Block
2014-03-23 10:19:47 +01:00
import net.minecraft.block.material.Material
2014-07-14 20:46:49 +02:00
import net.minecraft.client.renderer.texture.IIconRegister
import net.minecraft.util.IIcon
2014-09-07 05:50:03 +02:00
import net.minecraft.world.{IBlockAccess, World}
2014-07-14 20:46:49 +02:00
import net.minecraftforge.common.util.ForgeDirection
2015-01-26 12:40:32 +01:00
import resonantengine.api.tile.IRotatable
2015-01-26 13:28:38 +01:00
import resonantengine.lib.modcontent.block.ResonantBlock
2015-01-26 12:40:32 +01:00
import resonantengine.lib.transform.vector.Vector3
import resonantengine.prefab.block.impl.TRotatable
2014-09-07 06:08:26 +02:00
object TileTurntable
2014-09-07 05:50:03 +02:00
{
var top: IIcon = null
2014-07-14 20:46:49 +02:00
}
2015-01-26 11:17:24 +01:00
class TileTurntable extends ResonantBlock(Material.piston) with TRotatable
2014-09-07 05:50:03 +02:00
{
textureName = "turntable_side"
tickRandomly = true
2014-11-30 07:12:32 +01:00
rotationMask = 0x3F
2014-03-23 10:19:47 +01:00
2014-09-07 05:50:03 +02:00
override def tickRate(par1World: World): Int = 5
2014-03-23 10:19:47 +01:00
2014-09-07 05:50:03 +02:00
@SideOnly(Side.CLIENT)
override def registerIcons(iconReg: IIconRegister)
{
super.registerIcons(iconReg)
TileTurntable.top = iconReg.registerIcon(Reference.prefix + "turntable")
}
2014-03-23 10:19:47 +01:00
2014-09-07 05:50:03 +02:00
override def update()
{
2021-04-05 14:41:30 +02:00
updateTurntableState(world, x.toInt, y.toInt, z.toInt)
2014-09-07 05:50:03 +02:00
}
2014-03-23 10:19:47 +01:00
2014-09-07 05:50:03 +02:00
private def updateTurntableState(world: World, x: Int, y: Int, z: Int)
{
if (world.isBlockIndirectlyGettingPowered(x, y, z))
{
try
{
val facing: ForgeDirection = ForgeDirection.getOrientation(world.getBlockMetadata(x, y, z))
val position = new Vector3(x, y, z) + facing
val tileEntity = position.getTileEntity(world)
val block = position.getBlock(world)
if (!(tileEntity.isInstanceOf[TileMultipart]))
2014-07-14 20:46:49 +02:00
{
2014-09-07 05:50:03 +02:00
if (tileEntity.isInstanceOf[IRotatable])
{
val blockRotation = tileEntity.asInstanceOf[IRotatable].getDirection
tileEntity.asInstanceOf[IRotatable].setDirection(blockRotation.getRotation(facing.getOpposite))
}
else if (block != null)
{
block.rotateBlock(world, position.xi, position.yi, position.zi, facing.getOpposite)
}
2014-07-14 20:46:49 +02:00
2014-09-07 05:50:03 +02:00
world.markBlockForUpdate(position.xi, position.yi, position.zi)
world.playSoundEffect(x + 0.5D, y + 0.5D, z + 0.5D, "tile.piston.in", 0.5F, world.rand.nextFloat * 0.15F + 0.6F)
2014-03-23 10:19:47 +01:00
}
2014-09-07 05:50:03 +02:00
}
catch
2014-07-14 20:46:49 +02:00
{
2014-09-07 05:50:03 +02:00
case e: Exception =>
{
System.out.println("Error while rotating a block near " + x + "x " + y + "y " + z + "z " + (if (world != null && world.provider != null) world.provider.dimensionId + "d" else "null:world"))
e.printStackTrace
}
2014-07-22 08:32:42 +02:00
}
}
2014-09-07 05:50:03 +02:00
}
2014-07-22 08:32:42 +02:00
2014-09-07 05:50:03 +02:00
@SideOnly(Side.CLIENT)
override def getIcon(access: IBlockAccess, side: Int): IIcon =
{
if (side == metadata)
{
return TileTurntable.top
}
2014-07-22 08:32:42 +02:00
2014-09-07 05:50:03 +02:00
return getIcon
}
2014-07-22 08:32:42 +02:00
2014-09-07 05:50:03 +02:00
@SideOnly(Side.CLIENT)
override def getIcon(side: Int, meta: Int): IIcon =
{
if (side == 1)
{
return TileTurntable.top
2014-03-23 10:19:47 +01:00
}
2014-09-07 05:50:03 +02:00
return getIcon
}
override def onNeighborChanged(block: Block)
{
scheduleTick(10)
}
2014-03-24 18:46:33 +01:00
}