From 578df68bc7ebe972da652787d298e6f59c72f76a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Tue, 3 Feb 2015 17:10:41 +0100 Subject: [PATCH] Changed GeolyzerEvent.Analyze to take an absolute position instead of the side of the host for more flexibility. --- .../li/cil/oc/api/event/GeolyzerEvent.java | 13 ++++--- .../cil/oc/common/event/GeolyzerHandler.scala | 2 +- .../li/cil/oc/server/component/Geolyzer.scala | 36 ++++++------------- 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/main/java/li/cil/oc/api/event/GeolyzerEvent.java b/src/main/java/li/cil/oc/api/event/GeolyzerEvent.java index 92269f7eb..52c9ab02a 100644 --- a/src/main/java/li/cil/oc/api/event/GeolyzerEvent.java +++ b/src/main/java/li/cil/oc/api/event/GeolyzerEvent.java @@ -3,7 +3,6 @@ package li.cil.oc.api.event; import cpw.mods.fml.common.eventhandler.Cancelable; import cpw.mods.fml.common.eventhandler.Event; import li.cil.oc.api.driver.EnvironmentHost; -import net.minecraftforge.common.util.ForgeDirection; import java.util.HashMap; import java.util.Map; @@ -71,18 +70,22 @@ public abstract class GeolyzerEvent extends Event { */ public static class Analyze extends GeolyzerEvent { /** - * The side of the geolyzer on which the block to scan is located. + * The position of the block to scan. + *

+ * Note: get the world via the host if you need it. */ - public final ForgeDirection side; + public final int x, y, z; /** * The retrieved data for the block being scanned. */ public final Map data = new HashMap(); - public Analyze(EnvironmentHost host, Map options, ForgeDirection side) { + public Analyze(EnvironmentHost host, Map options, int x, int y, int z) { super(host, options); - this.side = side; + this.x = x; + this.y = y; + this.z = z; } } } diff --git a/src/main/scala/li/cil/oc/common/event/GeolyzerHandler.scala b/src/main/scala/li/cil/oc/common/event/GeolyzerHandler.scala index 192b706d4..ac3f01c4c 100644 --- a/src/main/scala/li/cil/oc/common/event/GeolyzerHandler.scala +++ b/src/main/scala/li/cil/oc/common/event/GeolyzerHandler.scala @@ -43,7 +43,7 @@ object GeolyzerHandler { @SubscribeEvent def onGeolyzerAnalyze(e: GeolyzerEvent.Analyze) { val world = e.host.world - val blockPos = BlockPosition(e.host).offset(e.side) + val blockPos = BlockPosition(e.x, e.y, e.z, world) val block = world.getBlock(blockPos) e.data += "name" -> Block.blockRegistry.getNameForObject(block) diff --git a/src/main/scala/li/cil/oc/server/component/Geolyzer.scala b/src/main/scala/li/cil/oc/server/component/Geolyzer.scala index b86c0edd8..26d713852 100644 --- a/src/main/scala/li/cil/oc/server/component/Geolyzer.scala +++ b/src/main/scala/li/cil/oc/server/component/Geolyzer.scala @@ -1,6 +1,5 @@ package li.cil.oc.server.component -import com.google.common.base.Strings import li.cil.oc.Settings import li.cil.oc.api import li.cil.oc.api.driver.EnvironmentHost @@ -17,7 +16,6 @@ import li.cil.oc.util.BlockPosition import li.cil.oc.util.DatabaseAccess import li.cil.oc.util.ExtendedArguments._ import li.cil.oc.util.ExtendedWorld._ -import net.minecraft.block.Block import net.minecraft.entity.player.EntityPlayer import net.minecraft.item.Item import net.minecraft.item.ItemStack @@ -65,7 +63,8 @@ class Geolyzer(val host: EnvironmentHost) extends prefab.ManagedEnvironment { if (!node.tryChangeBuffer(-Settings.get.geolyzerScanCost)) return result(Unit, "not enough energy") - val event = new Analyze(host, options, globalSide) + val globalPos = BlockPosition(host).offset(globalSide) + val event = new Analyze(host, options, globalPos.x, globalPos.y, globalPos.z) MinecraftForge.EVENT_BUS.post(event) if (event.isCanceled) result(Unit, "scan was canceled") else result(event.data) @@ -106,30 +105,15 @@ class Geolyzer(val host: EnvironmentHost) extends prefab.ManagedEnvironment { case machine: api.machine.Machine => (machine.host, message.data) match { case (tablet: internal.Tablet, Array(nbt: NBTTagCompound, stack: ItemStack, player: EntityPlayer, blockPos: BlockPosition, side: ForgeDirection, hitX: java.lang.Float, hitY: java.lang.Float, hitZ: java.lang.Float)) => if (node.tryChangeBuffer(-Settings.get.geolyzerScanCost)) { - // TODO 1.5 replace with event (change event to allow arbitrary coordinates) - val world = player.getEntityWorld - val block = world.getBlock(blockPos) - - if (!Strings.isNullOrEmpty(Block.blockRegistry.getNameForObject(block))) { - nbt.setString("name", Block.blockRegistry.getNameForObject(block)) + val event = new Analyze(host, Map.empty[AnyRef, AnyRef], blockPos.x, blockPos.y, blockPos.z) + MinecraftForge.EVENT_BUS.post(event) + if (!event.isCanceled) { + for ((key, value) <- event.data) value match { + case number: java.lang.Number => nbt.setDouble(key, number.doubleValue()) + case string: String if !string.isEmpty => nbt.setString(key, string) + case _ => // Unsupported, ignore. + } } - nbt.setInteger("metadata", world.getBlockMetadata(blockPos)) - nbt.setFloat("hardness", world.getBlockHardness(blockPos)) - nbt.setInteger("harvestLevel", world.getBlockHarvestLevel(blockPos)) - if (!Strings.isNullOrEmpty(world.getBlockHarvestTool(blockPos))) { - nbt.setString("harvestTool", world.getBlockHarvestTool(blockPos)) - } - nbt.setInteger("color", world.getBlockMapColor(blockPos).colorValue) - -// val event = new Analyze(host, Map.empty[AnyRef, AnyRef], side) -// MinecraftForge.EVENT_BUS.post(event) -// if (!event.isCanceled) { -// for ((key, value) <- event.data) value match { -// case number: java.lang.Number => nbt.setDouble(key, number.doubleValue()) -// case string: String if !string.isEmpty => nbt.setString(key, string) -// case _ => // Unsupported, ignore. -// } -// } } case _ => // Ignore. }