Extended wrench system to allow simple "isWrench" checks without context / checking for usability.

This commit is contained in:
Florian Nücke 2015-09-19 14:39:56 +02:00
parent a6e6e29ac2
commit 29af7e8927
5 changed files with 45 additions and 9 deletions

View File

@ -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;

View File

@ -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.
* <p/>
* This is used to determine whether certain item stacks are wrench items,
* which is used, for example, when "itemizing" a drone.
* <p/>
* The returned value must <tt>true</tt> if the item stack is a wrench,
* <tt>false</tt> otherwise.
* <p/>
* Signature of callbacks must be:
* <pre>
* boolean callback(ItemStack stack)
* </pre>
* <p/>
* Callbacks must be declared as <tt>packagePath.className.methodName</tt>.
* For example: <tt>com.example.Integration.callbackMethod</tt>.
*
* @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.
* <p/>

View File

@ -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) {

View File

@ -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) {

View File

@ -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)))
}