mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-08 23:06:58 -04:00
gpu item in list and extracted component cache for item components to be reusable (e.g. for hdds)
This commit is contained in:
parent
cb7d54c788
commit
2c6259b4a7
@ -1,11 +1,18 @@
|
||||
package li.cil.oc
|
||||
|
||||
import li.cil.oc.common.items.ItemHDD
|
||||
import li.cil.oc.common.items.ItemGraphicsCard
|
||||
import li.cil.oc.common.util.ItemComponentCache
|
||||
import li.cil.oc.server.components.GraphicsCard
|
||||
|
||||
object Items {
|
||||
var gpu: ItemGraphicsCard = null
|
||||
var hdd: ItemHDD = null
|
||||
|
||||
def init() {
|
||||
hdd = new ItemHDD()
|
||||
gpu = new ItemGraphicsCard
|
||||
hdd = new ItemHDD
|
||||
|
||||
ItemComponentCache.register(Config.itemGPUId, nbt => new GraphicsCard(nbt))
|
||||
}
|
||||
}
|
@ -46,5 +46,5 @@ trait IComputerContext {
|
||||
*
|
||||
* @param id the id of the component to get.
|
||||
*/
|
||||
def component[T: TypeTag](id: Int): T
|
||||
def component[T](id: Int): T
|
||||
}
|
@ -21,7 +21,7 @@ class Computer(val owner: AnyRef) extends IComputerContext with IComputer {
|
||||
|
||||
def signal(name: String, args: Any*) = throw new NotImplementedError
|
||||
|
||||
def component[T: TypeTag](id: Int) = throw new NotImplementedError
|
||||
def component[T](id: Int) = throw new NotImplementedError
|
||||
|
||||
// ----------------------------------------------------------------------- //
|
||||
// IComputer
|
||||
|
@ -1,13 +1,11 @@
|
||||
package li.cil.oc.common
|
||||
|
||||
import cpw.mods.fml.common.event._
|
||||
import cpw.mods.fml.common.network.Player
|
||||
import cpw.mods.fml.common.registry.LanguageRegistry
|
||||
import li.cil.oc._
|
||||
import li.cil.oc.api.OpenComputersAPI
|
||||
import li.cil.oc.server.computer.Drivers
|
||||
import li.cil.oc.server.drivers._
|
||||
import net.minecraft.world.World
|
||||
|
||||
class CommonProxy {
|
||||
def preInit(e: FMLPreInitializationEvent): Unit = {
|
||||
|
@ -4,29 +4,6 @@ import li.cil.oc.Config
|
||||
import li.cil.oc.CreativeTab
|
||||
import net.minecraft.item.Item
|
||||
import net.minecraft.world.World
|
||||
import scala.collection.mutable.WeakHashMap
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import li.cil.oc.server.components.Disk
|
||||
import net.minecraft.item.ItemStack
|
||||
import li.cil.oc.server.components.GraphicsCard
|
||||
|
||||
object ItemGraphicsCard {
|
||||
private val instances = WeakHashMap.empty[NBTTagCompound, GraphicsCard]
|
||||
|
||||
def getComponent(item: ItemStack): Option[GraphicsCard] =
|
||||
if (item.itemID == Config.itemGPUId) {
|
||||
val tag = item.getTagCompound match {
|
||||
case null => new NBTTagCompound
|
||||
case tag => tag
|
||||
}
|
||||
instances.get(tag).orElse {
|
||||
val component = new GraphicsCard(tag)
|
||||
instances += tag -> component
|
||||
Some(component)
|
||||
}
|
||||
}
|
||||
else throw new IllegalArgumentException("Invalid item type.")
|
||||
}
|
||||
|
||||
class ItemGraphicsCard extends Item(Config.itemGPUId) {
|
||||
setMaxStackSize(1)
|
||||
|
42
li/cil/oc/common/util/ItemComponentCache.scala
Normal file
42
li/cil/oc/common/util/ItemComponentCache.scala
Normal file
@ -0,0 +1,42 @@
|
||||
package li.cil.oc.common.util
|
||||
|
||||
import scala.collection.mutable._
|
||||
import scala.reflect.runtime.universe._
|
||||
|
||||
import net.minecraft.item.ItemStack
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
|
||||
/**
|
||||
* This singleton is responsible for caching actual item component instances,
|
||||
* that is the "component" object belonging to an ItemStack, based on its NBT
|
||||
* data.
|
||||
*/
|
||||
object ItemComponentCache {
|
||||
private val caches = Map.empty[Int, Cache[_]]
|
||||
|
||||
def get[T](item: ItemStack) = caches.get(item.itemID) match {
|
||||
case None => None
|
||||
case Some(cache) => cache.asInstanceOf[Cache[T]].getComponent(item)
|
||||
}
|
||||
|
||||
def register[T](id: Int, ctor: (NBTTagCompound) => T): Unit =
|
||||
caches += id -> new Cache[T](id, ctor)
|
||||
|
||||
private class Cache[T](val id: Int, val ctor: (NBTTagCompound) => T) {
|
||||
private val instances = WeakHashMap.empty[NBTTagCompound, T]
|
||||
|
||||
def getComponent(item: ItemStack): Option[T] =
|
||||
if (item.itemID == id) {
|
||||
val nbt = item.getTagCompound match {
|
||||
case null => new NBTTagCompound
|
||||
case tag => tag
|
||||
}
|
||||
instances.get(nbt).orElse {
|
||||
val component = ctor(nbt)
|
||||
instances += nbt -> component
|
||||
Some(component)
|
||||
}
|
||||
}
|
||||
else throw new IllegalArgumentException("Invalid item type.")
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package li.cil.oc.server.components
|
||||
|
||||
import li.cil.oc.common.util.INBTSerializable
|
||||
|
||||
trait IComponent {
|
||||
private var _id = 0
|
||||
|
||||
|
@ -147,12 +147,9 @@ class Computer(val owner: IComputerEnvironment) extends IComputerContext with IC
|
||||
})
|
||||
}
|
||||
|
||||
def component[T: TypeTag](id: Int) = components.get(id) match {
|
||||
def component[T](id: Int) = components.get(id) match {
|
||||
case None => throw new IllegalArgumentException("no such component")
|
||||
case Some(component) =>
|
||||
// TODO is this right?
|
||||
if (component.getClass() == typeOf[T]) component.asInstanceOf[T]
|
||||
else throw new IllegalArgumentException("bad component type")
|
||||
case Some(component) => component.asInstanceOf[T]
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------- //
|
||||
|
@ -5,7 +5,7 @@ import li.cil.oc.api.Callback
|
||||
import li.cil.oc.api.ComponentType
|
||||
import li.cil.oc.api.IComputerContext
|
||||
import li.cil.oc.api.IItemDriver
|
||||
import li.cil.oc.common.items.ItemGraphicsCard
|
||||
import li.cil.oc.common.util.ItemComponentCache
|
||||
import li.cil.oc.server.components.GraphicsCard
|
||||
import li.cil.oc.server.components.Screen
|
||||
import net.minecraft.item.ItemStack
|
||||
@ -78,5 +78,5 @@ object GraphicsCardDriver extends IItemDriver {
|
||||
|
||||
def componentType(item: ItemStack) = ComponentType.PCI
|
||||
|
||||
def component(item: ItemStack) = ItemGraphicsCard.getComponent(item)
|
||||
def component(item: ItemStack) = ItemComponentCache.get[GraphicsCard](item)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user