From ca5d95cff5305c3c20f61ab88a2fa8440e45d9f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sat, 14 May 2016 13:23:11 +0200 Subject: [PATCH] Only pass X and Y to RackMountable.onActivate, adjusted to the actual surface area of the mountable (in [0, 1] for both, on the 14x3 pixel surface). Closes #1794. --- .../cil/oc/api/component/RackMountable.java | 9 +++---- .../scala/li/cil/oc/common/block/Rack.scala | 26 ++++++++++++++++--- .../oc/common/component/TerminalServer.scala | 2 +- .../server/component/DiskDriveMountable.scala | 2 +- .../li/cil/oc/server/component/Server.scala | 2 +- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/main/java/li/cil/oc/api/component/RackMountable.java b/src/main/java/li/cil/oc/api/component/RackMountable.java index 2bfd30095..a94820c2c 100644 --- a/src/main/java/li/cil/oc/api/component/RackMountable.java +++ b/src/main/java/li/cil/oc/api/component/RackMountable.java @@ -7,7 +7,6 @@ import li.cil.oc.api.util.StateAware; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.nbt.NBTTagCompound; -import net.minecraftforge.common.util.ForgeDirection; /** * Use this interface on environments provided by drivers for items that can @@ -63,11 +62,9 @@ public interface RackMountable extends ManagedEnvironment, StateAware { * pointlessly compressed fashion (because MC is a dummy like that). * * @param player the player activating the mountable. - * @param side the side (in global coordinate space) of the rack that was activated. - * @param hitX the relative x coordinate of the activation. - * @param hitY the relative y coordinate of the activation. - * @param hitZ the relative z coordinate of the activation. + * @param hitX the relative x coordinate of the activation on the mountable. + * @param hitY the relative y coordinate of the activation on the mountable. * @return whether the activation was handled (e.g. GUI opened). */ - boolean onActivate(EntityPlayer player, ForgeDirection side, float hitX, float hitY, float hitZ); + boolean onActivate(EntityPlayer player, float hitX, float hitY); } diff --git a/src/main/scala/li/cil/oc/common/block/Rack.scala b/src/main/scala/li/cil/oc/common/block/Rack.scala index 113739d50..5e40c1985 100644 --- a/src/main/scala/li/cil/oc/common/block/Rack.scala +++ b/src/main/scala/li/cil/oc/common/block/Rack.scala @@ -10,6 +10,7 @@ import li.cil.oc.common.tileentity import net.minecraft.client.renderer.texture.IIconRegister import net.minecraft.entity.player.EntityPlayer import net.minecraft.util.IIcon +import net.minecraft.util.Vec3 import net.minecraft.world.IBlockAccess import net.minecraft.world.World import net.minecraftforge.common.util.ForgeDirection @@ -73,14 +74,31 @@ class Rack extends RedstoneAware with traits.SpecialBlock with traits.PowerAccep override def onBlockActivated(world: World, x: Int, y: Int, z: Int, player: EntityPlayer, side: ForgeDirection, hitX: Float, hitY: Float, hitZ: Float): Boolean = { world.getTileEntity(x, y, z) match { case rack: tileentity.Rack => rack.slotAt(side, hitX, hitY, hitZ) match { - case Some(slot) => rack.getMountable(slot) match { - case mountable: RackMountable if mountable.onActivate(player, side, hitX, hitY, hitZ) => return true // Activation handled by mountable. - case _ => - } + case Some(slot) => + val hitVec = Vec3.createVectorHelper(hitX, hitY, hitZ) + val rotation = side match { + case ForgeDirection.WEST => Math.toRadians(90).toFloat + case ForgeDirection.NORTH => Math.toRadians(180).toFloat + case ForgeDirection.EAST => Math.toRadians(270).toFloat + case _ => 0 + } + val localHitVec = rotate(hitVec.addVector(-0.5, -0.5, -0.5), rotation).addVector(0.5, 0.5, 0.5) + localHitVec.xCoord = ((if (side.offsetX != 0) 1 - localHitVec.xCoord else localHitVec.xCoord) * 16 - 1) / 14f + localHitVec.yCoord = ((1 - localHitVec.yCoord) * 16 - 2 - 3 * slot) / 3f + rack.getMountable(slot) match { + case mountable: RackMountable if mountable.onActivate(player, localHitVec.xCoord.toFloat, localHitVec.yCoord.toFloat) => return true // Activation handled by mountable. + case _ => + } case _ => } case _ => } super.onBlockActivated(world, x, y, z, player, side, hitX, hitY, hitZ) } + + def rotate(v: Vec3, t: Float): Vec3 = { + val cos = Math.cos(t) + val sin = Math.sin(t) + Vec3.createVectorHelper(v.xCoord * cos - v.zCoord * sin, v.yCoord, v.xCoord * sin + v.zCoord * cos) + } } diff --git a/src/main/scala/li/cil/oc/common/component/TerminalServer.scala b/src/main/scala/li/cil/oc/common/component/TerminalServer.scala index da5df8e09..5c7d82ccf 100644 --- a/src/main/scala/li/cil/oc/common/component/TerminalServer.scala +++ b/src/main/scala/li/cil/oc/common/component/TerminalServer.scala @@ -117,7 +117,7 @@ class TerminalServer(val rack: api.internal.Rack, val slot: Int) extends Environ override def getConnectableAt(index: Int): RackBusConnectable = null - override def onActivate(player: EntityPlayer, side: ForgeDirection, hitX: Float, hitY: Float, hitZ: Float): Boolean = { + override def onActivate(player: EntityPlayer, hitX: Float, hitY: Float): Boolean = { val stack = player.getHeldItem if (api.Items.get(stack) == api.Items.get(Constants.ItemName.Terminal)) { if (!world.isRemote) { diff --git a/src/main/scala/li/cil/oc/server/component/DiskDriveMountable.scala b/src/main/scala/li/cil/oc/server/component/DiskDriveMountable.scala index 6035e9a25..6886ad2eb 100644 --- a/src/main/scala/li/cil/oc/server/component/DiskDriveMountable.scala +++ b/src/main/scala/li/cil/oc/server/component/DiskDriveMountable.scala @@ -147,7 +147,7 @@ class DiskDriveMountable(val rack: api.internal.Rack, val slot: Int) extends pre override def getConnectableAt(index: Int): RackBusConnectable = null - override def onActivate(player: EntityPlayer, side: ForgeDirection, hitX: Float, hitY: Float, hitZ: Float): Boolean = { + override def onActivate(player: EntityPlayer, hitX: Float, hitY: Float): Boolean = { if (player.isSneaking) { val isDiskInDrive = getStackInSlot(0) != null val isHoldingDisk = isItemValidForSlot(0, player.getHeldItem) diff --git a/src/main/scala/li/cil/oc/server/component/Server.scala b/src/main/scala/li/cil/oc/server/component/Server.scala index 200c76001..fb7e08163 100644 --- a/src/main/scala/li/cil/oc/server/component/Server.scala +++ b/src/main/scala/li/cil/oc/server/component/Server.scala @@ -157,7 +157,7 @@ class Server(val rack: api.internal.Rack, val slot: Int) extends Environment wit case Some(busConnectable: RackBusConnectable) => busConnectable }.apply(index) - override def onActivate(player: EntityPlayer, side: ForgeDirection, hitX: Float, hitY: Float, hitZ: Float): Boolean = { + override def onActivate(player: EntityPlayer, hitX: Float, hitY: Float): Boolean = { if (!player.getEntityWorld.isRemote) { if (player.isSneaking) { if (!machine.isRunning && isUseableByPlayer(player)) {