some more fixes and adjustments; cleanup

This commit is contained in:
Florian Nücke 2014-03-03 20:23:28 +01:00
parent 3ff032b347
commit d65bd29886
14 changed files with 56 additions and 30 deletions

View File

@ -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.
* <p/>
* 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();

View File

@ -44,6 +44,19 @@ public interface Machine extends ManagedEnvironment, Context {
*/
Map<String, String> components();
/**
* The number of connected components.
* <p/>
* This number can differ from <tt>components().size()</tt>, since this is
* the number of actually <em>connected</em> 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 <tt>null</tt> if either the
@ -57,6 +70,19 @@ public interface Machine extends ManagedEnvironment, Context {
*/
String tmpAddress();
/**
* A string with the last error message.
* <p/>
* 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).
* <p/>
* When the machine started, this is reset to <tt>null</tt>.
*
* @return the last error message, or <tt>null</tt>.
*/
String lastError();
/**
* The current world time. This is updated each tick and provides a thread
* safe way to access the world time for architectures.

View File

@ -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

View File

@ -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) {

View File

@ -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 _ =>

View File

@ -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 _ =>

View File

@ -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()
}

View File

@ -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 _ =>
}
}

View File

@ -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))
}

View File

@ -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)

View File

@ -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 =>

View File

@ -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

View File

@ -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)
}

View File

@ -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)