fixed power converter not being properly cleaned up when broken; Lua: buffer won't allow writing after having been closed anymore; fixed server picking with analyzer (was upside down)

This commit is contained in:
Florian Nücke 2014-01-31 13:37:40 +01:00
parent 1aafa9657a
commit 37f8c4bfd1
5 changed files with 81 additions and 17 deletions

View File

@ -27,6 +27,7 @@ function buffer:close()
if self.mode.w or self.mode.a then
self:flush()
end
self.closed = true
return self.stream:close()
end
@ -211,6 +212,9 @@ function buffer:setvbuf(mode, size)
end
function buffer:write(...)
if self.closed then
return nil, "bad file descriptor"
end
local args = table.pack(...)
for i = 1, args.n do
if type(args[i]) == "number" then

View File

@ -58,25 +58,25 @@ end
local socketStream = {}
function socketStream.close(self)
function socketStream:close()
if self.handle then
self.inet.close(self.handle)
self.handle = nil
end
end
function socketStream.seek()
function socketStream:seek()
return nil, "bad file descriptor"
end
function socketStream.read(self, n)
function socketStream:read(n)
if not self.handle then
return nil, "connection is closed"
end
return self.inet.read(self.handle, n)
end
function socketStream.write(self, value)
function socketStream:write(value)
if not self.handle then
return nil, "connection is closed"
end

View File

@ -155,15 +155,15 @@ end
local memoryStream = {}
function memoryStream.close(self)
function memoryStream:close()
self.closed = true
end
function memoryStream.seek()
function memoryStream:seek()
return nil, "bad file descriptor"
end
function memoryStream.read(self, n)
function memoryStream:read(n)
if self.closed then
if self.buffer == "" and self.redirect.read then
return self.redirect.read:read(n)
@ -178,7 +178,7 @@ function memoryStream.read(self, n)
return result
end
function memoryStream.write(self, value)
function memoryStream:write(value)
local ok
if self.redirect.write then
ok = self.redirect.write:write(value)

View File

@ -1,21 +1,23 @@
package li.cil.oc.common.tileentity
import li.cil.oc.api.network._
import li.cil.oc.api.{Network, network}
import li.cil.oc.util.ExtendedNBT._
import li.cil.oc.{Settings, api}
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.nbt.NBTTagCompound
import net.minecraftforge.common.ForgeDirection
import universalelectricity.api.energy.{IEnergyContainer, IEnergyInterface}
import universalelectricity.api.{CompatibilityType, UniversalClass}
// Because @UniversalClass injects custom invalidate and validate methods for
// IC2 setup/teardown we have to use a base class and implement our own logic
// in a child class. This also means we can't use the Environment base class,
// since mixins are linked up at compile time, whereas UniversalClass injects
// its methods at runtime.
@UniversalClass
class PowerConverter extends Environment with Analyzable with IEnergyInterface with IEnergyContainer {
val node = api.Network.newNode(this, Visibility.Network).
withConnector(Settings.get.bufferConverter).
create()
def onAnalyze(player: EntityPlayer, side: Int, hitX: Float, hitY: Float, hitZ: Float) = null
def canConnect(direction: ForgeDirection) = direction != null && direction != ForgeDirection.UNKNOWN
abstract class PowerConverterBase extends TileEntity with network.Environment with IEnergyInterface with IEnergyContainer {
def node: Connector
def onReceiveEnergy(from: ForgeDirection, receive: Long, doReceive: Boolean) = {
if (!Settings.get.ignorePower && node != null) {
@ -44,3 +46,61 @@ class PowerConverter extends Environment with Analyzable with IEnergyInterface w
protected def fromUE(energy: Long) = energy * CompatibilityType.BUILDCRAFT.ratio
}
class PowerConverter extends PowerConverterBase with Analyzable {
val node = api.Network.newNode(this, Visibility.Network).
withConnector(Settings.get.bufferConverter).
create()
protected var addedToNetwork = false
// ----------------------------------------------------------------------- //
def onAnalyze(player: EntityPlayer, side: Int, hitX: Float, hitY: Float, hitZ: Float) = null
def canConnect(direction: ForgeDirection) = direction != null && direction != ForgeDirection.UNKNOWN
// ----------------------------------------------------------------------- //
override def updateEntity() {
super.updateEntity()
if (!addedToNetwork) {
addedToNetwork = true
Network.joinOrCreateNetwork(this)
}
}
override def onChunkUnload() {
super.onChunkUnload()
Option(node).foreach(_.remove)
}
override def invalidate() {
super.invalidate()
Option(node).foreach(_.remove)
}
// ----------------------------------------------------------------------- //
override def readFromNBT(nbt: NBTTagCompound) {
super.readFromNBT(nbt)
if (node != null) {
node.load(nbt.getCompoundTag(Settings.namespace + "node"))
}
}
override def writeToNBT(nbt: NBTTagCompound) {
super.writeToNBT(nbt)
if (node != null) {
nbt.setNewCompoundTag(Settings.namespace + "node", node.save)
}
}
// ----------------------------------------------------------------------- //
def onMessage(message: network.Message) {}
def onConnect(node: network.Node) {}
def onDisconnect(node: network.Node) {}
}

View File

@ -107,7 +107,7 @@ class Rack extends Hub with PowerBalancer with Inventory with Rotatable with Bun
if (side == facing.ordinal) {
val l = 2 / 16.0
val h = 14 / 16.0
val slot = ((hitY - l) / (h - l) * 4).toInt
val slot = (((1 - hitY) - l) / (h - l) * 4).toInt
if (slot >= 0 && slot <= 3 && servers(slot).isDefined) {
Array(servers(slot).get.machine.node)
}