Fixed crash when trying to change APU architecture. Closes #1404.

This commit is contained in:
Florian Nücke 2015-09-05 23:00:09 +02:00
parent 81c2b7d1cc
commit a0ce592382
5 changed files with 34 additions and 26 deletions

View File

@ -3,7 +3,10 @@ package li.cil.oc.common.item.traits
import java.util import java.util
import li.cil.oc.Settings import li.cil.oc.Settings
import li.cil.oc.api
import li.cil.oc.api.driver.item.MutableProcessor
import li.cil.oc.integration.opencomputers.DriverCPU import li.cil.oc.integration.opencomputers.DriverCPU
import li.cil.oc.server.machine.Machine
import li.cil.oc.util.Tooltip import li.cil.oc.util.Tooltip
import net.minecraft.entity.player.EntityPlayer import net.minecraft.entity.player.EntityPlayer
import net.minecraft.item.ItemStack import net.minecraft.item.ItemStack
@ -19,22 +22,26 @@ trait CPULike extends Delegate {
override protected def tooltipData: Seq[Any] = Seq(Settings.get.cpuComponentSupport(cpuTier)) override protected def tooltipData: Seq[Any] = Seq(Settings.get.cpuComponentSupport(cpuTier))
override protected def tooltipExtended(stack: ItemStack, tooltip: util.List[String]) { override protected def tooltipExtended(stack: ItemStack, tooltip: util.List[String]) {
tooltip.addAll(Tooltip.get("CPU.Architecture", DriverCPU.getArchitectureName(DriverCPU.architecture(stack)))) tooltip.addAll(Tooltip.get("CPU.Architecture", Machine.getArchitectureName(DriverCPU.architecture(stack))))
} }
override def onItemRightClick(stack: ItemStack, world: World, player: EntityPlayer) = { override def onItemRightClick(stack: ItemStack, world: World, player: EntityPlayer) = {
if (player.isSneaking) { if (player.isSneaking) {
if (!world.isRemote) { if (!world.isRemote) {
val architectures = DriverCPU.allArchitectures.toList api.Driver.driverFor(stack) match {
case driver: MutableProcessor =>
val architectures = driver.allArchitectures.toList
if (architectures.nonEmpty) { if (architectures.nonEmpty) {
val currentIndex = architectures.indexOf(DriverCPU.architecture(stack)) val currentIndex = architectures.indexOf(driver.architecture(stack))
val newIndex = (currentIndex + 1) % architectures.length val newIndex = (currentIndex + 1) % architectures.length
val archClass = architectures(newIndex) val archClass = architectures(newIndex)
val archName = DriverCPU.getArchitectureName(archClass) val archName = Machine.getArchitectureName(archClass)
DriverCPU.setArchitecture(stack, archClass) driver.setArchitecture(stack, archClass)
player.addChatMessage(new ChatComponentTranslation(Settings.namespace + "tooltip.CPU.Architecture", archName)) player.addChatMessage(new ChatComponentTranslation(Settings.namespace + "tooltip.CPU.Architecture", archName))
} }
player.swingItem() player.swingItem()
case _ => // No known driver for this processor.
}
} }
} }
stack stack

View File

@ -12,6 +12,7 @@ import li.cil.oc.common.Slot
import li.cil.oc.common.Tier import li.cil.oc.common.Tier
import li.cil.oc.common.item import li.cil.oc.common.item
import li.cil.oc.common.item.Delegator import li.cil.oc.common.item.Delegator
import li.cil.oc.server.machine.Machine
import li.cil.oc.server.machine.luac.NativeLuaArchitecture import li.cil.oc.server.machine.luac.NativeLuaArchitecture
import net.minecraft.item.ItemStack import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound import net.minecraft.nbt.NBTTagCompound
@ -66,13 +67,6 @@ abstract class DriverCPU extends Item with MutableProcessor {
if (!worksWith(stack)) throw new IllegalArgumentException("Unsupported processor type.") if (!worksWith(stack)) throw new IllegalArgumentException("Unsupported processor type.")
if (!stack.hasTagCompound) stack.setTagCompound(new NBTTagCompound()) if (!stack.hasTagCompound) stack.setTagCompound(new NBTTagCompound())
stack.getTagCompound.setString(Settings.namespace + "archClass", architecture.getName) stack.getTagCompound.setString(Settings.namespace + "archClass", architecture.getName)
stack.getTagCompound.setString(Settings.namespace + "archName", getArchitectureName(architecture)) stack.getTagCompound.setString(Settings.namespace + "archName", Machine.getArchitectureName(architecture))
}
// TODO Move to Machine API in 1.6
def getArchitectureName(architecture: Class[_ <: Architecture]) =
architecture.getAnnotation(classOf[Architecture.Name]) match {
case annotation: Architecture.Name => annotation.value
case _ => architecture.getSimpleName
} }
} }

View File

@ -980,6 +980,13 @@ object Machine extends MachineAPI {
override def architectures = checked.toSeq override def architectures = checked.toSeq
// TODO Expose in Machine API in 1.6
def getArchitectureName(architecture: Class[_ <: Architecture]) =
architecture.getAnnotation(classOf[Architecture.Name]) match {
case annotation: Architecture.Name => annotation.value
case _ => architecture.getSimpleName
}
override def create(host: MachineHost) = new Machine(host) override def create(host: MachineHost) = new Machine(host)
/** Possible states of the computer, and in particular its executor. */ /** Possible states of the computer, and in particular its executor. */

View File

@ -5,7 +5,7 @@ import li.cil.oc.api
import li.cil.oc.api.driver.item.MutableProcessor import li.cil.oc.api.driver.item.MutableProcessor
import li.cil.oc.api.driver.item.Processor import li.cil.oc.api.driver.item.Processor
import li.cil.oc.api.network.Connector import li.cil.oc.api.network.Connector
import li.cil.oc.integration.opencomputers.DriverCPU import li.cil.oc.server.machine.Machine
import li.cil.oc.util.ExtendedLuaState.extendLuaState import li.cil.oc.util.ExtendedLuaState.extendLuaState
import scala.collection.convert.WrapAsScala._ import scala.collection.convert.WrapAsScala._
@ -117,7 +117,7 @@ class ComputerAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
case (stack, processor: Processor) => Seq(processor.architecture(stack)) case (stack, processor: Processor) => Seq(processor.architecture(stack))
} match { } match {
case Some(architectures) => case Some(architectures) =>
lua.pushValue(architectures.map(DriverCPU.getArchitectureName)) lua.pushValue(architectures.map(Machine.getArchitectureName))
case _ => case _ =>
lua.newTable() lua.newTable()
} }
@ -128,7 +128,7 @@ class ComputerAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
lua.pushScalaFunction(lua => { lua.pushScalaFunction(lua => {
machine.host.internalComponents.map(stack => (stack, api.Driver.driverFor(stack))).collectFirst { machine.host.internalComponents.map(stack => (stack, api.Driver.driverFor(stack))).collectFirst {
case (stack, processor: Processor) => case (stack, processor: Processor) =>
lua.pushString(DriverCPU.getArchitectureName(processor.architecture(stack))) lua.pushString(Machine.getArchitectureName(processor.architecture(stack)))
1 1
}.getOrElse(0) }.getOrElse(0)
}) })
@ -137,7 +137,7 @@ class ComputerAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
lua.pushScalaFunction(lua => { lua.pushScalaFunction(lua => {
val archName = lua.checkString(1) val archName = lua.checkString(1)
machine.host.internalComponents.map(stack => (stack, api.Driver.driverFor(stack))).collectFirst { machine.host.internalComponents.map(stack => (stack, api.Driver.driverFor(stack))).collectFirst {
case (stack, processor: MutableProcessor) => processor.allArchitectures.find(arch => DriverCPU.getArchitectureName(arch) == archName) match { case (stack, processor: MutableProcessor) => processor.allArchitectures.find(arch => Machine.getArchitectureName(arch) == archName) match {
case Some(archClass) => case Some(archClass) =>
if (archClass != processor.architecture(stack)) { if (archClass != processor.architecture(stack)) {
processor.setArchitecture(stack, archClass) processor.setArchitecture(stack, archClass)

View File

@ -5,7 +5,7 @@ import li.cil.oc.api
import li.cil.oc.api.driver.item.MutableProcessor import li.cil.oc.api.driver.item.MutableProcessor
import li.cil.oc.api.driver.item.Processor import li.cil.oc.api.driver.item.Processor
import li.cil.oc.api.network.Connector import li.cil.oc.api.network.Connector
import li.cil.oc.integration.opencomputers.DriverCPU import li.cil.oc.server.machine.Machine
import li.cil.oc.util.ScalaClosure._ import li.cil.oc.util.ScalaClosure._
import li.cil.repack.org.luaj.vm2.LuaValue import li.cil.repack.org.luaj.vm2.LuaValue
import li.cil.repack.org.luaj.vm2.Varargs import li.cil.repack.org.luaj.vm2.Varargs
@ -65,21 +65,21 @@ class ComputerAPI(owner: LuaJLuaArchitecture) extends LuaJAPI(owner) {
case (stack, processor: MutableProcessor) => processor.allArchitectures.toSeq case (stack, processor: MutableProcessor) => processor.allArchitectures.toSeq
case (stack, processor: Processor) => Seq(processor.architecture(stack)) case (stack, processor: Processor) => Seq(processor.architecture(stack))
} match { } match {
case Some(architectures) => LuaValue.listOf(architectures.map(DriverCPU.getArchitectureName).map(LuaValue.valueOf).toArray) case Some(architectures) => LuaValue.listOf(architectures.map(Machine.getArchitectureName).map(LuaValue.valueOf).toArray)
case _ => LuaValue.tableOf() case _ => LuaValue.tableOf()
} }
}) })
computer.set("getArchitecture", (args: Varargs) => { computer.set("getArchitecture", (args: Varargs) => {
machine.host.internalComponents.map(stack => (stack, api.Driver.driverFor(stack))).collectFirst { machine.host.internalComponents.map(stack => (stack, api.Driver.driverFor(stack))).collectFirst {
case (stack, processor: Processor) => LuaValue.valueOf(DriverCPU.getArchitectureName(processor.architecture(stack))) case (stack, processor: Processor) => LuaValue.valueOf(Machine.getArchitectureName(processor.architecture(stack)))
}.getOrElse(LuaValue.NONE) }.getOrElse(LuaValue.NONE)
}) })
computer.set("setArchitecture", (args: Varargs) => { computer.set("setArchitecture", (args: Varargs) => {
val archName = args.checkjstring(1) val archName = args.checkjstring(1)
machine.host.internalComponents.map(stack => (stack, api.Driver.driverFor(stack))).collectFirst { machine.host.internalComponents.map(stack => (stack, api.Driver.driverFor(stack))).collectFirst {
case (stack, processor: MutableProcessor) => processor.allArchitectures.find(arch => DriverCPU.getArchitectureName(arch) == archName) match { case (stack, processor: MutableProcessor) => processor.allArchitectures.find(arch => Machine.getArchitectureName(arch) == archName) match {
case Some(archClass) => case Some(archClass) =>
if (archClass != processor.architecture(stack)) { if (archClass != processor.architecture(stack)) {
processor.setArchitecture(stack, archClass) processor.setArchitecture(stack, archClass)