Trying to keep tablets running across dimension changes, closes #1145, closes #1146.

Somewhat hacky, seems to work fine from initial tests (single and multiplayer) but will need broader testing.
This commit is contained in:
Florian Nücke 2015-05-12 12:18:27 +02:00
parent 9deaf7101f
commit a03966748e

View File

@ -227,6 +227,8 @@ class TabletWrapper(var stack: ItemStack, var player: EntityPlayer) extends Comp
val tablet = if (world.isRemote) null else new component.Tablet(this) val tablet = if (world.isRemote) null else new component.Tablet(this)
var autoSave = true
private var isInitialized = !world.isRemote private var isInitialized = !world.isRemote
private var lastRunning = false private var lastRunning = false
@ -250,7 +252,7 @@ class TabletWrapper(var stack: ItemStack, var player: EntityPlayer) extends Comp
} }
} }
def writeToNBT() { def writeToNBT(clearState: Boolean = true) {
if (!stack.hasTagCompound) { if (!stack.hasTagCompound) {
stack.setTagCompound(new NBTTagCompound()) stack.setTagCompound(new NBTTagCompound())
} }
@ -262,17 +264,18 @@ class TabletWrapper(var stack: ItemStack, var player: EntityPlayer) extends Comp
data.setNewCompoundTag(Settings.namespace + "component", tablet.save) data.setNewCompoundTag(Settings.namespace + "component", tablet.save)
data.setNewCompoundTag(Settings.namespace + "data", machine.save) data.setNewCompoundTag(Settings.namespace + "data", machine.save)
if (clearState) {
// Force tablets into stopped state to avoid errors when trying to // Force tablets into stopped state to avoid errors when trying to
// load deleted machine states. // load deleted machine states.
data.getCompoundTag(Settings.namespace + "data").removeTag("state") data.getCompoundTag(Settings.namespace + "data").removeTag("state")
} }
}
save(data) save(data)
} }
readFromNBT() readFromNBT()
if (!world.isRemote) { if (!world.isRemote) {
api.Network.joinNewNetwork(machine.node) api.Network.joinNewNetwork(machine.node)
machine.stop()
val charge = math.max(0, this.data.energy - tablet.node.globalBuffer) val charge = math.max(0, this.data.energy - tablet.node.globalBuffer)
tablet.node.changeBuffer(charge) tablet.node.changeBuffer(charge)
writeToNBT() writeToNBT()
@ -483,6 +486,8 @@ object Tablet {
// Force re-load on world change, in case some components store a // Force re-load on world change, in case some components store a
// reference to the world object. // reference to the world object.
if (holder.worldObj != wrapper.world) { if (holder.worldObj != wrapper.world) {
wrapper.writeToNBT(clearState = false)
wrapper.autoSave = false
cache.invalidate(id) cache.invalidate(id)
cache.cleanUp() cache.cleanUp()
wrapper = cache.get(id, this) wrapper = cache.get(id, this)
@ -502,12 +507,12 @@ object Tablet {
val tablet = e.getValue val tablet = e.getValue
if (tablet.node != null) { if (tablet.node != null) {
// Server. // Server.
tablet.writeToNBT() if (tablet.autoSave) tablet.writeToNBT()
tablet.machine.stop() tablet.machine.stop()
for (node <- tablet.machine.node.network.nodes) { for (node <- tablet.machine.node.network.nodes) {
node.remove() node.remove()
} }
tablet.writeToNBT() if (tablet.autoSave) tablet.writeToNBT()
} }
} }