From 29af7e8927efb2a751b2e206e418304dcf78d0a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sat, 19 Sep 2015 14:39:56 +0200 Subject: [PATCH] Extended wrench system to allow simple "isWrench" checks without context / checking for usability. --- src/main/java/li/cil/oc/api/API.java | 2 +- src/main/java/li/cil/oc/api/IMC.java | 23 +++++++++++++++++++ src/main/scala/li/cil/oc/common/IMC.scala | 12 +++++++--- .../scala/li/cil/oc/common/entity/Drone.scala | 3 ++- .../li/cil/oc/integration/util/Wrench.scala | 14 +++++++---- 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/main/java/li/cil/oc/api/API.java b/src/main/java/li/cil/oc/api/API.java index 1abead2ae..cb1c13b76 100644 --- a/src/main/java/li/cil/oc/api/API.java +++ b/src/main/java/li/cil/oc/api/API.java @@ -12,7 +12,7 @@ import li.cil.oc.api.detail.*; */ public class API { public static final String ID_OWNER = "OpenComputers|Core"; - public static final String VERSION = "5.6.1"; + public static final String VERSION = "5.6.2"; public static DriverAPI driver = null; public static FileSystemAPI fileSystem = null; diff --git a/src/main/java/li/cil/oc/api/IMC.java b/src/main/java/li/cil/oc/api/IMC.java index 39beb10e3..fecc03eef 100644 --- a/src/main/java/li/cil/oc/api/IMC.java +++ b/src/main/java/li/cil/oc/api/IMC.java @@ -238,6 +238,29 @@ public final class IMC { FMLInterModComms.sendMessage(MOD_ID, "registerWrenchTool", callback); } + /** + * Register a callback for checking if an item is a wrench. + *

+ * This is used to determine whether certain item stacks are wrench items, + * which is used, for example, when "itemizing" a drone. + *

+ * The returned value must true if the item stack is a wrench, + * false otherwise. + *

+ * Signature of callbacks must be: + *

+     * boolean callback(ItemStack stack)
+     * 
+ *

+ * Callbacks must be declared as packagePath.className.methodName. + * For example: com.example.Integration.callbackMethod. + * + * @param callback the callback to register as a wrench tool tester. + */ + public static void registerWrenchToolCheck(String callback) { + FMLInterModComms.sendMessage(MOD_ID, "registerWrenchToolCheck", callback); + } + /** * Register a handler for items that can be charged. *

diff --git a/src/main/scala/li/cil/oc/common/IMC.scala b/src/main/scala/li/cil/oc/common/IMC.scala index 9a0703129..8ab417147 100644 --- a/src/main/scala/li/cil/oc/common/IMC.scala +++ b/src/main/scala/li/cil/oc/common/IMC.scala @@ -56,9 +56,15 @@ object IMC { } } else if (message.key == "registerWrenchTool" && message.isStringMessage) { - OpenComputers.log.info(s"Registering new wrench tool '${message.getStringValue}' from mod ${message.getSender}.") - try Wrench.add(getStaticMethod(message.getStringValue, classOf[EntityPlayer], classOf[Int], classOf[Int], classOf[Int], classOf[Boolean])) catch { - case t: Throwable => OpenComputers.log.warn("Failed registering wrench tool.", t) + OpenComputers.log.info(s"Registering new wrench tool usage '${message.getStringValue}' from mod ${message.getSender}.") + try Wrench.addUsage(getStaticMethod(message.getStringValue, classOf[EntityPlayer], classOf[Int], classOf[Int], classOf[Int], classOf[Boolean])) catch { + case t: Throwable => OpenComputers.log.warn("Failed registering wrench usage.", t) + } + } + else if (message.key == "registerWrenchToolCheck" && message.isStringMessage) { + OpenComputers.log.info(s"Registering new wrench tool check '${message.getStringValue}' from mod ${message.getSender}.") + try Wrench.addUsage(getStaticMethod(message.getStringValue, classOf[ItemStack])) catch { + case t: Throwable => OpenComputers.log.warn("Failed registering wrench check.", t) } } else if (message.key == "registerItemCharge" && message.isNBTMessage) { diff --git a/src/main/scala/li/cil/oc/common/entity/Drone.scala b/src/main/scala/li/cil/oc/common/entity/Drone.scala index 52c31ba6e..1b4071d57 100644 --- a/src/main/scala/li/cil/oc/common/entity/Drone.scala +++ b/src/main/scala/li/cil/oc/common/entity/Drone.scala @@ -23,6 +23,7 @@ import li.cil.oc.common.GuiType import li.cil.oc.common.inventory.ComponentInventory import li.cil.oc.common.inventory.Inventory import li.cil.oc.common.item.data.DroneData +import li.cil.oc.integration.util.Wrench import li.cil.oc.server.agent import li.cil.oc.server.component import li.cil.oc.util.BlockPosition @@ -463,7 +464,7 @@ class Drone(val world: World) extends Entity(world) with MachineHost with intern override def interactFirst(player: EntityPlayer) = { if (player.isSneaking) { - if (api.Items.get(player.getCurrentEquippedItem) == api.Items.get(Constants.ItemName.Wrench)) { + if (Wrench.isWrench(player.getCurrentEquippedItem)) { kill() } else if (!world.isRemote && !machine.isRunning) { diff --git a/src/main/scala/li/cil/oc/integration/util/Wrench.scala b/src/main/scala/li/cil/oc/integration/util/Wrench.scala index 8ac1af5f3..893aaafde 100644 --- a/src/main/scala/li/cil/oc/integration/util/Wrench.scala +++ b/src/main/scala/li/cil/oc/integration/util/Wrench.scala @@ -5,17 +5,23 @@ import java.lang.reflect.Method import li.cil.oc.common.IMC import li.cil.oc.util.BlockPosition import net.minecraft.entity.player.EntityPlayer +import net.minecraft.item.ItemStack import scala.collection.mutable object Wrench { - private val wrenches = mutable.LinkedHashSet.empty[Method] + private val usages = mutable.LinkedHashSet.empty[Method] + private val checks = mutable.LinkedHashSet.empty[Method] - def add(wrench: Method): Unit = wrenches += wrench + def addUsage(wrench: Method): Unit = usages += wrench + + def addCheck(checker: Method): Unit = checks += checker + + def isWrench(stack: ItemStack): Boolean = stack != null && checks.exists(IMC.tryInvokeStatic(_, stack)(false)) def holdsApplicableWrench(player: EntityPlayer, position: BlockPosition): Boolean = - player.getCurrentEquippedItem != null && wrenches.exists(IMC.tryInvokeStatic(_, player, int2Integer(position.x), int2Integer(position.y), int2Integer(position.z), boolean2Boolean(false))(false)) + player.getCurrentEquippedItem != null && usages.exists(IMC.tryInvokeStatic(_, player, int2Integer(position.x), int2Integer(position.y), int2Integer(position.z), boolean2Boolean(false))(false)) def wrenchUsed(player: EntityPlayer, position: BlockPosition): Unit = - if (player.getCurrentEquippedItem != null) wrenches.foreach(IMC.tryInvokeStaticVoid(_, player, int2Integer(position.x), int2Integer(position.y), int2Integer(position.z), boolean2Boolean(true))) + if (player.getCurrentEquippedItem != null) usages.foreach(IMC.tryInvokeStaticVoid(_, player, int2Integer(position.x), int2Integer(position.y), int2Integer(position.z), boolean2Boolean(true))) }