mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-10 15:56:41 -04:00
Fixed crash when trying to change APU architecture. Closes #1404.
This commit is contained in:
parent
81c2b7d1cc
commit
a0ce592382
@ -3,7 +3,10 @@ package li.cil.oc.common.item.traits
|
||||
import java.util
|
||||
|
||||
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.server.machine.Machine
|
||||
import li.cil.oc.util.Tooltip
|
||||
import net.minecraft.entity.player.EntityPlayer
|
||||
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 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) = {
|
||||
if (player.isSneaking) {
|
||||
if (!world.isRemote) {
|
||||
val architectures = DriverCPU.allArchitectures.toList
|
||||
if (architectures.nonEmpty) {
|
||||
val currentIndex = architectures.indexOf(DriverCPU.architecture(stack))
|
||||
val newIndex = (currentIndex + 1) % architectures.length
|
||||
val archClass = architectures(newIndex)
|
||||
val archName = DriverCPU.getArchitectureName(archClass)
|
||||
DriverCPU.setArchitecture(stack, archClass)
|
||||
player.addChatMessage(new ChatComponentTranslation(Settings.namespace + "tooltip.CPU.Architecture", archName))
|
||||
api.Driver.driverFor(stack) match {
|
||||
case driver: MutableProcessor =>
|
||||
val architectures = driver.allArchitectures.toList
|
||||
if (architectures.nonEmpty) {
|
||||
val currentIndex = architectures.indexOf(driver.architecture(stack))
|
||||
val newIndex = (currentIndex + 1) % architectures.length
|
||||
val archClass = architectures(newIndex)
|
||||
val archName = Machine.getArchitectureName(archClass)
|
||||
driver.setArchitecture(stack, archClass)
|
||||
player.addChatMessage(new ChatComponentTranslation(Settings.namespace + "tooltip.CPU.Architecture", archName))
|
||||
}
|
||||
player.swingItem()
|
||||
case _ => // No known driver for this processor.
|
||||
}
|
||||
player.swingItem()
|
||||
}
|
||||
}
|
||||
stack
|
||||
|
@ -12,6 +12,7 @@ import li.cil.oc.common.Slot
|
||||
import li.cil.oc.common.Tier
|
||||
import li.cil.oc.common.item
|
||||
import li.cil.oc.common.item.Delegator
|
||||
import li.cil.oc.server.machine.Machine
|
||||
import li.cil.oc.server.machine.luac.NativeLuaArchitecture
|
||||
import net.minecraft.item.ItemStack
|
||||
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 (!stack.hasTagCompound) stack.setTagCompound(new NBTTagCompound())
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -980,6 +980,13 @@ object Machine extends MachineAPI {
|
||||
|
||||
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)
|
||||
|
||||
/** Possible states of the computer, and in particular its executor. */
|
||||
|
@ -5,7 +5,7 @@ import li.cil.oc.api
|
||||
import li.cil.oc.api.driver.item.MutableProcessor
|
||||
import li.cil.oc.api.driver.item.Processor
|
||||
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 scala.collection.convert.WrapAsScala._
|
||||
@ -117,7 +117,7 @@ class ComputerAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
|
||||
case (stack, processor: Processor) => Seq(processor.architecture(stack))
|
||||
} match {
|
||||
case Some(architectures) =>
|
||||
lua.pushValue(architectures.map(DriverCPU.getArchitectureName))
|
||||
lua.pushValue(architectures.map(Machine.getArchitectureName))
|
||||
case _ =>
|
||||
lua.newTable()
|
||||
}
|
||||
@ -128,7 +128,7 @@ class ComputerAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
|
||||
lua.pushScalaFunction(lua => {
|
||||
machine.host.internalComponents.map(stack => (stack, api.Driver.driverFor(stack))).collectFirst {
|
||||
case (stack, processor: Processor) =>
|
||||
lua.pushString(DriverCPU.getArchitectureName(processor.architecture(stack)))
|
||||
lua.pushString(Machine.getArchitectureName(processor.architecture(stack)))
|
||||
1
|
||||
}.getOrElse(0)
|
||||
})
|
||||
@ -137,7 +137,7 @@ class ComputerAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
|
||||
lua.pushScalaFunction(lua => {
|
||||
val archName = lua.checkString(1)
|
||||
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) =>
|
||||
if (archClass != processor.architecture(stack)) {
|
||||
processor.setArchitecture(stack, archClass)
|
||||
|
@ -5,7 +5,7 @@ import li.cil.oc.api
|
||||
import li.cil.oc.api.driver.item.MutableProcessor
|
||||
import li.cil.oc.api.driver.item.Processor
|
||||
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.repack.org.luaj.vm2.LuaValue
|
||||
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: Processor) => Seq(processor.architecture(stack))
|
||||
} 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()
|
||||
}
|
||||
})
|
||||
|
||||
computer.set("getArchitecture", (args: Varargs) => {
|
||||
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)
|
||||
})
|
||||
|
||||
computer.set("setArchitecture", (args: Varargs) => {
|
||||
val archName = args.checkjstring(1)
|
||||
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) =>
|
||||
if (archClass != processor.architecture(stack)) {
|
||||
processor.setArchitecture(stack, archClass)
|
||||
|
Loading…
x
Reference in New Issue
Block a user