mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-17 11:15:12 -04:00
actually applying type converters; added itemstack converter
This commit is contained in:
parent
e3d0bfa2c4
commit
ed0090e60e
@ -27,5 +27,5 @@ public interface Converter {
|
||||
* @param value the value to convert.
|
||||
* @param output the map conversion results are accumulated into.
|
||||
*/
|
||||
void convert(final Object value, Map output);
|
||||
void convert(Object value, Map<Object, Object> output);
|
||||
}
|
||||
|
@ -60,6 +60,8 @@ class Proxy {
|
||||
api.Driver.add(driver.item.UpgradeSolarGenerator)
|
||||
api.Driver.add(driver.item.WirelessNetworkCard)
|
||||
|
||||
api.Driver.add(driver.converter.ItemStack)
|
||||
|
||||
Recipes.init()
|
||||
GameRegistry.registerCraftingHandler(CraftingHandler)
|
||||
|
||||
|
@ -1,11 +1,12 @@
|
||||
package li.cil.oc.server.driver
|
||||
|
||||
import li.cil.oc.api
|
||||
import java.util
|
||||
import java.util.logging.Level
|
||||
import li.cil.oc.api.driver.Converter
|
||||
import li.cil.oc.{OpenComputers, api}
|
||||
import net.minecraft.item.ItemStack
|
||||
import net.minecraft.world.World
|
||||
import scala.Some
|
||||
import scala.collection.mutable.ArrayBuffer
|
||||
import li.cil.oc.api.driver.Converter
|
||||
|
||||
/**
|
||||
* This class keeps track of registered drivers and provides installation logic
|
||||
@ -58,4 +59,44 @@ private[oc] object Registry extends api.detail.DriverAPI {
|
||||
case Some(driver) => Some(driver)
|
||||
}
|
||||
else None
|
||||
|
||||
def convert(value: Array[AnyRef]) = value.map(convertRecursively)
|
||||
|
||||
def convertRecursively(value: AnyRef): AnyRef = value match {
|
||||
case null | Unit | None => null
|
||||
case arg: java.lang.Boolean => arg
|
||||
case arg: java.lang.Byte => arg
|
||||
case arg: java.lang.Character => arg
|
||||
case arg: java.lang.Short => arg
|
||||
case arg: java.lang.Integer => arg
|
||||
case arg: java.lang.Long => arg
|
||||
case arg: java.lang.Float => arg
|
||||
case arg: java.lang.Double => arg
|
||||
case arg: java.lang.String => arg
|
||||
|
||||
case arg: Array[Boolean] => arg
|
||||
case arg: Array[Byte] => arg
|
||||
case arg: Array[Character] => arg
|
||||
case arg: Array[Short] => arg
|
||||
case arg: Array[Integer] => arg
|
||||
case arg: Array[Long] => arg
|
||||
case arg: Array[Float] => arg
|
||||
case arg: Array[Double] => arg
|
||||
case arg: Array[String] => arg
|
||||
|
||||
case arg: Array[_] => arg.map {
|
||||
case (value: AnyRef) => convertRecursively(value)
|
||||
}
|
||||
case arg: Map[_, _] => arg.map {
|
||||
case (key: AnyRef, value: AnyRef) => convertRecursively(key) -> convertRecursively(value)
|
||||
}
|
||||
|
||||
case arg =>
|
||||
val result = new util.HashMap[AnyRef, AnyRef]()
|
||||
converters.foreach(converter => try converter.convert(arg, result) catch {
|
||||
case t: Throwable => OpenComputers.log.log(Level.WARNING, "Type converter threw an exception.", t)
|
||||
})
|
||||
if (result.isEmpty) null
|
||||
else result
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
package li.cil.oc.server.driver.converter
|
||||
|
||||
import java.util
|
||||
import li.cil.oc.api
|
||||
import net.minecraft.item
|
||||
import scala.collection.convert.WrapAsScala._
|
||||
|
||||
object ItemStack extends api.driver.Converter {
|
||||
override def convert(value: AnyRef, output: util.Map[AnyRef, AnyRef]) = {
|
||||
value match {
|
||||
case stack: item.ItemStack =>
|
||||
output += "id" -> Int.box(stack.itemID)
|
||||
output += "damage" -> Int.box(stack.getItemDamage)
|
||||
output += "maxDamage" -> Int.box(stack.getMaxDamage)
|
||||
output += "size" -> Int.box(stack.stackSize)
|
||||
output += "maxSize" -> Int.box(stack.getMaxStackSize)
|
||||
output += "hasTag" -> Boolean.box(stack.hasTagCompound)
|
||||
output += "name" -> stack.getUnlocalizedName
|
||||
if (stack.hasDisplayName) {
|
||||
output += "label" -> stack.getDisplayName
|
||||
}
|
||||
case _ =>
|
||||
}
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ import li.cil.oc.api.machine.Robot
|
||||
import li.cil.oc.api.network
|
||||
import li.cil.oc.api.network.{Node => ImmutableNode, _}
|
||||
import li.cil.oc.server.component.machine.Machine
|
||||
import li.cil.oc.server.driver.CompoundBlockEnvironment
|
||||
import li.cil.oc.server.driver.{Registry, CompoundBlockEnvironment}
|
||||
import li.cil.oc.server.network.Component.{PeripheralCallback, ComponentCallback}
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import scala.Some
|
||||
@ -108,7 +108,7 @@ trait Component extends network.Component with Node {
|
||||
def invoke(method: String, context: Context, arguments: AnyRef*) =
|
||||
callbacks.get(method) match {
|
||||
case Some(callback) => hosts(method) match {
|
||||
case Some(environment) => callback(environment, context, new Component.VarArgs(Seq(arguments: _*)))
|
||||
case Some(environment) => Registry.convert(callback(environment, context, new Component.VarArgs(Seq(arguments: _*))))
|
||||
case _ => throw new NoSuchMethodException()
|
||||
}
|
||||
case _ => throw new NoSuchMethodException()
|
||||
|
Loading…
x
Reference in New Issue
Block a user