From 530d5add82347efdd82d62ee6735cadf376e175c Mon Sep 17 00:00:00 2001 From: bdew Date: Wed, 11 Mar 2015 18:00:32 +0200 Subject: [PATCH] Moved block rotation from player camera logic into a separate object --- .../bdew/lib/rotate/BaseRotatableBlock.scala | 16 +--------- src/net/bdew/lib/rotate/RotatedHelper.scala | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 src/net/bdew/lib/rotate/RotatedHelper.scala diff --git a/src/net/bdew/lib/rotate/BaseRotatableBlock.scala b/src/net/bdew/lib/rotate/BaseRotatableBlock.scala index 65d0496..dfa4dde 100644 --- a/src/net/bdew/lib/rotate/BaseRotatableBlock.scala +++ b/src/net/bdew/lib/rotate/BaseRotatableBlock.scala @@ -69,21 +69,7 @@ trait BaseRotatableBlock extends Block { 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 - } - } + val dir = RotatedHelper.getFacingFromEntity(ent, getValidFacings, getDefaultFacing) setFacing(world, x, y, z, dir) super.onBlockPlacedBy(world, x, y, z, ent, stack) } diff --git a/src/net/bdew/lib/rotate/RotatedHelper.scala b/src/net/bdew/lib/rotate/RotatedHelper.scala new file mode 100644 index 0000000..eb8ad55 --- /dev/null +++ b/src/net/bdew/lib/rotate/RotatedHelper.scala @@ -0,0 +1,32 @@ +/* + * Copyright (c) bdew, 2013 - 2015 + * 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 + * http://bdew.net/minecraft-mod-public-license/ + */ + +package net.bdew.lib.rotate + +import java.util + +import net.minecraft.entity.EntityLivingBase +import net.minecraft.util.MathHelper +import net.minecraftforge.common.util.ForgeDirection + +object RotatedHelper { + def getFacingFromEntity(ent: EntityLivingBase, validRotations: util.EnumSet[ForgeDirection], default: ForgeDirection): ForgeDirection = { + val pitch = Math.round(ent.rotationPitch) + val yaw = MathHelper.floor_double(ent.rotationYaw * 4.0F / 360.0F + 0.5D) & 3 + 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 _ => default + } + } +}