From d65bd298868c948c7ced7a70196a16f5cd99c794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Mon, 3 Mar 2014 20:23:28 +0100 Subject: [PATCH] some more fixes and adjustments; cleanup --- .../li/cil/oc/api/machine/Architecture.java | 4 +-- .../java/li/cil/oc/api/machine/Machine.java | 26 +++++++++++++++++++ .../li/cil/oc/common/tileentity/Case.scala | 3 ++- .../li/cil/oc/common/tileentity/Charger.scala | 4 +-- .../cil/oc/common/tileentity/Computer.scala | 2 +- .../li/cil/oc/common/tileentity/Rack.scala | 2 +- .../li/cil/oc/common/tileentity/Robot.scala | 16 ++++++------ .../java/li/cil/oc/server/PacketHandler.scala | 6 ++--- .../component/machine/LuaArchitecture.scala | 2 +- .../machine/LuaJLuaArchitecture.scala | 4 +-- .../oc/server/component/machine/Machine.scala | 4 +-- .../machine/NativeLuaArchitecture.scala | 4 +-- .../oc/server/component/robot/Player.scala | 2 +- .../cil/oc/server/component/robot/Robot.scala | 7 +++-- 14 files changed, 56 insertions(+), 30 deletions(-) diff --git a/src/main/java/li/cil/oc/api/machine/Architecture.java b/src/main/java/li/cil/oc/api/machine/Architecture.java index d60461c3b..a07b19067 100644 --- a/src/main/java/li/cil/oc/api/machine/Architecture.java +++ b/src/main/java/li/cil/oc/api/machine/Architecture.java @@ -54,7 +54,7 @@ public interface Architecture { * * @return whether the architecture was initialized successfully. */ - boolean init(); + boolean initialize(); /** * Called when a machine stopped. Used to clean up any handles, memory and @@ -108,7 +108,7 @@ public interface Architecture { * Called when the owning machine was connected to the component network. *

* This can be useful for connecting custom file systems (read only memory) - * in case {@link #init()} was called from the machine's load logic (where + * in case {@link #initialize()} was called from the machine's load logic (where * it was not yet connected to the network). */ void onConnect(); diff --git a/src/main/java/li/cil/oc/api/machine/Machine.java b/src/main/java/li/cil/oc/api/machine/Machine.java index 23474b5dc..19c2cdc63 100644 --- a/src/main/java/li/cil/oc/api/machine/Machine.java +++ b/src/main/java/li/cil/oc/api/machine/Machine.java @@ -44,6 +44,19 @@ public interface Machine extends ManagedEnvironment, Context { */ Map components(); + /** + * The number of connected components. + *

+ * This number can differ from components().size(), since this is + * the number of actually connected components, which is used to + * determine whether the component limit has been exceeded, for example. It + * takes into account components added but not processed, yet (see also + * {@link #components()}). + * + * @return the number of connected components. + */ + int componentCount(); + /** * The address of the file system that holds the machine's file system for * temporary files (tmpfs). This may return null if either the @@ -57,6 +70,19 @@ public interface Machine extends ManagedEnvironment, Context { */ String tmpAddress(); + /** + * A string with the last error message. + *

+ * The error string is set either when the machine crashes (see the + * {@link #crash(String)} method), or when it fails to start (which, + * technically, is also a crash). + *

+ * When the machine started, this is reset to null. + * + * @return the last error message, or null. + */ + String lastError(); + /** * The current world time. This is updated each tick and provides a thread * safe way to access the world time for architectures. diff --git a/src/main/java/li/cil/oc/common/tileentity/Case.scala b/src/main/java/li/cil/oc/common/tileentity/Case.scala index da462b0bd..b4aefc650 100644 --- a/src/main/java/li/cil/oc/common/tileentity/Case.scala +++ b/src/main/java/li/cil/oc/common/tileentity/Case.scala @@ -4,6 +4,7 @@ import cpw.mods.fml.relauncher.{SideOnly, Side} import li.cil.oc.Settings import li.cil.oc.api.driver import li.cil.oc.api.driver.Slot +import li.cil.oc.api.network.Connector import li.cil.oc.server.driver.Registry import net.minecraft.entity.player.EntityPlayer import net.minecraft.item.ItemStack @@ -16,7 +17,7 @@ class Case(var tier: Int, isRemote: Boolean) extends Computer(isRemote) { @SideOnly(Side.CLIENT) override protected def hasConnector(side: ForgeDirection) = side != facing - override protected def connector(side: ForgeDirection) = Option(if (side != facing && computer != null) computer.node else null) + override protected def connector(side: ForgeDirection) = Option(if (side != facing && computer != null) computer.node.asInstanceOf[Connector] else null) var maxComponents = 0 diff --git a/src/main/java/li/cil/oc/common/tileentity/Charger.scala b/src/main/java/li/cil/oc/common/tileentity/Charger.scala index 271742154..913d0862e 100644 --- a/src/main/java/li/cil/oc/common/tileentity/Charger.scala +++ b/src/main/java/li/cil/oc/common/tileentity/Charger.scala @@ -1,7 +1,7 @@ package li.cil.oc.common.tileentity import cpw.mods.fml.relauncher.{SideOnly, Side} -import li.cil.oc.api.network.{Analyzable, Node, Visibility} +import li.cil.oc.api.network.{Connector, Analyzable, Node, Visibility} import li.cil.oc.server.{PacketSender => ServerPacketSender} import li.cil.oc.{Settings, api} import net.minecraft.entity.player.EntityPlayer @@ -34,7 +34,7 @@ class Charger extends Environment with RedstoneAware with Analyzable { val charge = Settings.get.chargeRate * chargeSpeed robots.collect { - case Some(proxy) => node.changeBuffer(proxy.robot.computer.node.changeBuffer(charge + node.changeBuffer(-charge))) + case Some(proxy) => node.changeBuffer(proxy.robot.node.changeBuffer(charge + node.changeBuffer(-charge))) } } else if (chargeSpeed > 0 && world.getWorldInfo.getWorldTotalTime % 10 == 0) { diff --git a/src/main/java/li/cil/oc/common/tileentity/Computer.scala b/src/main/java/li/cil/oc/common/tileentity/Computer.scala index 51c89c07c..36499ee43 100644 --- a/src/main/java/li/cil/oc/common/tileentity/Computer.scala +++ b/src/main/java/li/cil/oc/common/tileentity/Computer.scala @@ -180,7 +180,7 @@ abstract class Computer(isRemote: Boolean) extends Environment with ComponentInv override def onAnalyze(player: EntityPlayer, side: Int, hitX: Float, hitY: Float, hitZ: Float) = { computer.lastError match { - case Some(value) => + case value if value != null => player.sendChatToPlayer(ChatMessageComponent.createFromTranslationWithSubstitutions( Settings.namespace + "gui.Analyzer.LastError", ChatMessageComponent.createFromTranslationKey(value))) case _ => diff --git a/src/main/java/li/cil/oc/common/tileentity/Rack.scala b/src/main/java/li/cil/oc/common/tileentity/Rack.scala index 0f29aa9b8..381d643cb 100644 --- a/src/main/java/li/cil/oc/common/tileentity/Rack.scala +++ b/src/main/java/li/cil/oc/common/tileentity/Rack.scala @@ -126,7 +126,7 @@ class Rack extends Hub with PowerBalancer with Inventory with Rotatable with Bun if (slot >= 0 && slot <= 3 && servers(slot).isDefined) { val computer = servers(slot).get.machine computer.lastError match { - case Some(value) => + case value if value != null => player.sendChatToPlayer(ChatMessageComponent.createFromTranslationWithSubstitutions( Settings.namespace + "gui.Analyzer.LastError", ChatMessageComponent.createFromTranslationKey(value))) case _ => diff --git a/src/main/java/li/cil/oc/common/tileentity/Robot.scala b/src/main/java/li/cil/oc/common/tileentity/Robot.scala index 9d62a25b8..ffdca6ba8 100644 --- a/src/main/java/li/cil/oc/common/tileentity/Robot.scala +++ b/src/main/java/li/cil/oc/common/tileentity/Robot.scala @@ -51,7 +51,7 @@ class Robot(isRemote: Boolean) extends Computer(isRemote) with ISidedInventory w // ----------------------------------------------------------------------- // - override def node = if (isClient) null else computer.node + override def node: ComponentConnector = if (isClient) null else computer.node.asInstanceOf[ComponentConnector] override val _buffer = new common.component.Buffer(this) { override def maxResolution = (48, 14) @@ -115,7 +115,7 @@ class Robot(isRemote: Boolean) extends Computer(isRemote) with ISidedInventory w // pow(xp(level) - base, 1/exp) / const = level level = math.min((Math.pow(xp - Settings.get.baseXpToLevel, 1 / Settings.get.exponentialXpGrowth) / Settings.get.constantXpGrowth).toInt, 30) if (isServer) { - computer.node.setLocalBufferSize(Settings.get.bufferRobot + Settings.get.bufferPerLevel * level) + node.setLocalBufferSize(Settings.get.bufferRobot + Settings.get.bufferPerLevel * level) } } @@ -209,7 +209,7 @@ class Robot(isRemote: Boolean) extends Computer(isRemote) with ISidedInventory w if (stack.hasTagCompound) { xp = stack.getTagCompound.getDouble(Settings.namespace + "xp") updateXpInfo() - computer.node.changeBuffer(stack.getTagCompound.getInteger(Settings.namespace + "storedEnergy")) + node.changeBuffer(stack.getTagCompound.getInteger(Settings.namespace + "storedEnergy")) } } @@ -287,8 +287,8 @@ class Robot(isRemote: Boolean) extends Computer(isRemote) with ISidedInventory w super.updateEntity() if (isServer) { gpu.update() - globalBuffer = computer.node.globalBuffer - globalBufferSize = computer.node.globalBufferSize + globalBuffer = node.globalBuffer + globalBufferSize = node.globalBufferSize updatePowerInformation() if (xpChanged && world.getWorldInfo.getWorldTotalTime % 200 == 0) { xpChanged = false @@ -418,8 +418,8 @@ class Robot(isRemote: Boolean) extends Computer(isRemote) with ISidedInventory w override def onMachineConnect(node: Node) { super.onConnect(node) if (node == this.node) { - computer.node.connect(buffer.node) - computer.node.connect(gpu.node) + node.connect(buffer.node) + node.connect(gpu.node) buffer.node.connect(keyboard.node) // There's a chance the server sends a robot tile entity to its clients // before the tile entity's first update was called, in which case the @@ -438,7 +438,7 @@ class Robot(isRemote: Boolean) extends Computer(isRemote) with ISidedInventory w super.onDisconnect(node) if (node == this.node) { buffer.node.remove() - computer.node.remove() + node.remove() gpu.node.remove() keyboard.node.remove() } diff --git a/src/main/java/li/cil/oc/server/PacketHandler.scala b/src/main/java/li/cil/oc/server/PacketHandler.scala index 682a41889..88970e672 100644 --- a/src/main/java/li/cil/oc/server/PacketHandler.scala +++ b/src/main/java/li/cil/oc/server/PacketHandler.scala @@ -2,14 +2,14 @@ package li.cil.oc.server import cpw.mods.fml.common.network.Player import li.cil.oc.Settings +import li.cil.oc.api.machine.Machine import li.cil.oc.common.PacketType +import li.cil.oc.common.multipart.EventHandler import li.cil.oc.common.tileentity._ import li.cil.oc.common.{PacketHandler => CommonPacketHandler} -import li.cil.oc.server.component.machine.Machine import net.minecraft.entity.player.EntityPlayerMP import net.minecraft.util.ChatMessageComponent import net.minecraftforge.common.{ForgeDirection, DimensionManager} -import li.cil.oc.common.multipart.{EventHandler, MultiPart} class PacketHandler extends CommonPacketHandler { override protected def world(player: Player, dimension: Int) = @@ -52,7 +52,7 @@ class PacketHandler extends CommonPacketHandler { if (!computer.isPaused) { computer.start() computer.lastError match { - case Some(message) => player.sendChatToPlayer(ChatMessageComponent.createFromTranslationKey(message)) + case message => player.sendChatToPlayer(ChatMessageComponent.createFromTranslationKey(message)) case _ => } } diff --git a/src/main/java/li/cil/oc/server/component/machine/LuaArchitecture.scala b/src/main/java/li/cil/oc/server/component/machine/LuaArchitecture.scala index 0fc676a24..2ad597b93 100644 --- a/src/main/java/li/cil/oc/server/component/machine/LuaArchitecture.scala +++ b/src/main/java/li/cil/oc/server/component/machine/LuaArchitecture.scala @@ -11,7 +11,7 @@ abstract class LuaArchitecture(val machine: api.machine.Machine) extends Archite val rom = Option(FileSystem.asManagedEnvironment(FileSystem. fromClass(OpenComputers.getClass, Settings.resourceDomain, "lua/rom"), "rom")) - override def init() = { + override def initialize() = { if (machine.node.network != null) { rom.foreach(fs => machine.node.connect(fs.node)) } diff --git a/src/main/java/li/cil/oc/server/component/machine/LuaJLuaArchitecture.scala b/src/main/java/li/cil/oc/server/component/machine/LuaJLuaArchitecture.scala index 310476340..9d3e00678 100644 --- a/src/main/java/li/cil/oc/server/component/machine/LuaJLuaArchitecture.scala +++ b/src/main/java/li/cil/oc/server/component/machine/LuaJLuaArchitecture.scala @@ -135,8 +135,8 @@ class LuaJLuaArchitecture(machine: api.machine.Machine) extends LuaArchitecture( // ----------------------------------------------------------------------- // - override def init() = { - super.init() + override def initialize() = { + super.initialize() lua = JsePlatform.debugGlobals() lua.set("package", LuaValue.NIL) diff --git a/src/main/java/li/cil/oc/server/component/machine/Machine.scala b/src/main/java/li/cil/oc/server/component/machine/Machine.scala index 6df9e845a..75b823ea1 100644 --- a/src/main/java/li/cil/oc/server/component/machine/Machine.scala +++ b/src/main/java/li/cil/oc/server/component/machine/Machine.scala @@ -66,7 +66,7 @@ class Machine(val owner: Owner, constructor: Constructor[_ <: Architecture]) ext // ----------------------------------------------------------------------- // - def lastError = message + def lastError = message.orNull override def components = scala.collection.convert.WrapAsJava.mapAsJavaMap(_components) @@ -643,7 +643,7 @@ class Machine(val owner: Owner, constructor: Constructor[_ <: Architecture]) ext } try { - return architecture.init() + return architecture.initialize() } catch { case ex: Throwable => diff --git a/src/main/java/li/cil/oc/server/component/machine/NativeLuaArchitecture.scala b/src/main/java/li/cil/oc/server/component/machine/NativeLuaArchitecture.scala index e45a2d9e7..1d85ea283 100644 --- a/src/main/java/li/cil/oc/server/component/machine/NativeLuaArchitecture.scala +++ b/src/main/java/li/cil/oc/server/component/machine/NativeLuaArchitecture.scala @@ -179,8 +179,8 @@ class NativeLuaArchitecture(machine: api.machine.Machine) extends LuaArchitectur // ----------------------------------------------------------------------- // - override def init(): Boolean = { - super.init() + override def initialize(): Boolean = { + super.initialize() // Creates a new state with all base libraries and the persistence library // loaded into it. This means the state has much more power than it diff --git a/src/main/java/li/cil/oc/server/component/robot/Player.scala b/src/main/java/li/cil/oc/server/component/robot/Player.scala index 8cac76659..28befe072 100644 --- a/src/main/java/li/cil/oc/server/component/robot/Player.scala +++ b/src/main/java/li/cil/oc/server/component/robot/Player.scala @@ -368,7 +368,7 @@ class Player(val robot: tileentity.Robot) extends EntityPlayer(robot.world, Sett override def addExhaustion(amount: Float) { if (Settings.get.robotExhaustionCost > 0) { - robot.computer.node.changeBuffer(-Settings.get.robotExhaustionCost * amount) + robot.node.changeBuffer(-Settings.get.robotExhaustionCost * amount) } robot.addXp(Settings.get.robotExhaustionXpRate * amount) } diff --git a/src/main/java/li/cil/oc/server/component/robot/Robot.scala b/src/main/java/li/cil/oc/server/component/robot/Robot.scala index 8172ed585..ba4df297b 100644 --- a/src/main/java/li/cil/oc/server/component/robot/Robot.scala +++ b/src/main/java/li/cil/oc/server/component/robot/Robot.scala @@ -1,7 +1,6 @@ package li.cil.oc.server.component.robot import li.cil.oc.api -import li.cil.oc.api.machine.Owner import li.cil.oc.api.network._ import li.cil.oc.common.tileentity import li.cil.oc.server.component.machine.Machine @@ -502,7 +501,7 @@ class Robot(val robot: tileentity.Robot) extends Machine(robot, api.Machine.LuaA result(Unit, what) } else { - if (!robot.computer.node.tryChangeBuffer(-Settings.get.robotMoveCost)) { + if (!robot.node.tryChangeBuffer(-Settings.get.robotMoveCost)) { result(Unit, "not enough energy") } else if (robot.move(direction)) { @@ -511,7 +510,7 @@ class Robot(val robot: tileentity.Robot) extends Machine(robot, api.Machine.LuaA result(true) } else { - robot.computer.node.changeBuffer(Settings.get.robotMoveCost) + robot.node.changeBuffer(Settings.get.robotMoveCost) result(Unit, "impossible move") } } @@ -521,7 +520,7 @@ class Robot(val robot: tileentity.Robot) extends Machine(robot, api.Machine.LuaA @Callback def turn(context: Context, args: Arguments): Array[AnyRef] = { val clockwise = args.checkBoolean(0) - if (robot.computer.node.tryChangeBuffer(-Settings.get.robotTurnCost)) { + if (robot.node.tryChangeBuffer(-Settings.get.robotTurnCost)) { if (clockwise) robot.rotate(ForgeDirection.UP) else robot.rotate(ForgeDirection.DOWN) robot.animateTurn(clockwise, Settings.get.turnDelay)