From 5065044b749abfd7c28c034804ba4ef499953a4c Mon Sep 17 00:00:00 2001 From: repo_alt Date: Fri, 24 Jun 2022 12:10:19 +0400 Subject: [PATCH] moved machinePosition() to MachineHost --- src/main/java/li/cil/oc/api/machine/MachineHost.java | 9 +++++++++ src/main/scala/li/cil/oc/server/machine/Machine.scala | 7 ++----- .../server/machine/luac/NativeLuaArchitecture.scala | 11 ++++------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/main/java/li/cil/oc/api/machine/MachineHost.java b/src/main/java/li/cil/oc/api/machine/MachineHost.java index 67c7eae4a..b8ab09467 100644 --- a/src/main/java/li/cil/oc/api/machine/MachineHost.java +++ b/src/main/java/li/cil/oc/api/machine/MachineHost.java @@ -55,4 +55,13 @@ public interface MachineHost extends EnvironmentHost { * @param node the node that was disconnected from the network. */ void onMachineDisconnect(Node node); + + /** Helper method for printing the machine position in error messages and debug statements. */ + default String machinePosition() + { + if (world() != null && world().provider != null) + return String.format("(%g, %g, %g, %d)", xPosition(), yPosition(), zPosition(), world().provider.dimensionId); + else + return String.format("(%g, %g, %g)", xPosition(), yPosition(), zPosition()); + } } diff --git a/src/main/scala/li/cil/oc/server/machine/Machine.scala b/src/main/scala/li/cil/oc/server/machine/Machine.scala index 2b187c1ff..a42d6288b 100644 --- a/src/main/scala/li/cil/oc/server/machine/Machine.scala +++ b/src/main/scala/li/cil/oc/server/machine/Machine.scala @@ -780,7 +780,7 @@ class Machine(val host: MachineHost) extends prefab.ManagedEnvironment with mach catch { case t: Throwable => OpenComputers.log.error( - s"""Unexpected error loading a state of computer at ${machinePosition}. """ + + s"""Unexpected error loading a state of computer at ${host.machinePosition()}. """ + s"""State: ${state.headOption.fold("no state")(_.toString)}. Unless you're upgrading/downgrading across a major version, please report this! Thank you.""", t) close() } @@ -862,7 +862,7 @@ class Machine(val host: MachineHost) extends prefab.ManagedEnvironment with mach catch { case t: Throwable => OpenComputers.log.error( - s"""Unexpected error saving a state of computer at ${machinePosition}. """ + + s"""Unexpected error saving a state of computer at ${host.machinePosition()}. """ + s"""State: ${state.headOption.fold("no state")(_.toString)}. Unless you're upgrading/downgrading across a major version, please report this! Thank you.""", t) } }) @@ -1039,9 +1039,6 @@ class Machine(val host: MachineHost) extends prefab.ManagedEnvironment with mach // Keep track of time spent executing the computer. cpuTotal += System.nanoTime() - cpuStart } - - /** Helper method for printing the machine position in error messages and debug statements. */ - private def machinePosition = s"(${host.xPosition}, ${host.yPosition}, ${host.zPosition}, ${host.world().provider.dimensionId})" } object Machine extends MachineAPI { diff --git a/src/main/scala/li/cil/oc/server/machine/luac/NativeLuaArchitecture.scala b/src/main/scala/li/cil/oc/server/machine/luac/NativeLuaArchitecture.scala index ec110a64e..ed13eb4e3 100644 --- a/src/main/scala/li/cil/oc/server/machine/luac/NativeLuaArchitecture.scala +++ b/src/main/scala/li/cil/oc/server/machine/luac/NativeLuaArchitecture.scala @@ -376,7 +376,7 @@ abstract class NativeLuaArchitecture(val machine: api.machine.Machine) extends A try lua.gc(LuaState.GcAction.COLLECT, 0) catch { case t: Throwable => - OpenComputers.log.warn(s"Error cleaning up loaded computer @ ${machinePosition}. This either means the server is badly overloaded or a user created an evil __gc method, accidentally or not.") + OpenComputers.log.warn(s"Error cleaning up loaded computer @ ${machine.host().machinePosition()}. This either means the server is badly overloaded or a user created an evil __gc method, accidentally or not.") machine.crash("error in garbage collector, most likely __gc method timed out") } } catch { @@ -414,22 +414,19 @@ abstract class NativeLuaArchitecture(val machine: api.machine.Machine) extends A try lua.gc(LuaState.GcAction.COLLECT, 0) catch { case t: Throwable => - OpenComputers.log.warn(s"Error cleaning up loaded computer @ ${machinePosition}. This either means the server is badly overloaded or a user created an evil __gc method, accidentally or not.") + OpenComputers.log.warn(s"Error cleaning up loaded computer @ ${machine.host().machinePosition()}. This either means the server is badly overloaded or a user created an evil __gc method, accidentally or not.") machine.crash("error in garbage collector, most likely __gc method timed out") } } catch { case e: LuaRuntimeException => - OpenComputers.log.warn(s"Could not persist computer @ ${machinePosition}.\n${e.toString}" + (if (e.getLuaStackTrace.isEmpty) "" else "\tat " + e.getLuaStackTrace.mkString("\n\tat "))) + OpenComputers.log.warn(s"Could not persist computer @ ${machine.host().machinePosition()}.\n${e.toString}" + (if (e.getLuaStackTrace.isEmpty) "" else "\tat " + e.getLuaStackTrace.mkString("\n\tat "))) nbt.removeTag("state") case e: LuaGcMetamethodException => - OpenComputers.log.warn(s"Could not persist computer @ ${machinePosition}.\n${e.toString}") + OpenComputers.log.warn(s"Could not persist computer @ ${machine.host().machinePosition()}.\n${e.toString}") nbt.removeTag("state") } // Limit memory again. recomputeMemory(machine.host.internalComponents) } - - /** Helper method for printing the machine position in error messages and debug statements. */ - private def machinePosition = s"(${machine.host.xPosition}, ${machine.host.yPosition}, ${machine.host.zPosition}, ${machine.host.world().provider.dimensionId})" }