Minor cleanup.

This commit is contained in:
Florian Nücke 2014-05-20 16:24:59 +02:00
parent 9225d8b01e
commit 4e9efca258
4 changed files with 13 additions and 11 deletions

View File

@ -12,7 +12,7 @@ import scala.io.Source
object MonospaceFontRenderer {
val (chars, fontWidth, fontHeight) = try {
val lines = Source.fromInputStream(Minecraft.getMinecraft.getResourceManager.getResource(new ResourceLocation(Settings.resourceDomain, "/textures/font/chars.txt")).getInputStream)("UTF-8").getLines
val lines = Source.fromInputStream(Minecraft.getMinecraft.getResourceManager.getResource(new ResourceLocation(Settings.resourceDomain, "/textures/font/chars.txt")).getInputStream)("UTF-8").getLines()
val chars = lines.next()
val (w, h) = if (lines.hasNext) {
val size = lines.next().split(" ", 2)

View File

@ -94,9 +94,10 @@ class OSAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
val mon = getField("month", -1)
val year = getField("year", -1)
val time = GameTimeFormatter.mktime(year, mon, mday, hour, min, sec)
if (time == null) lua.pushNil()
else lua.pushNumber(time: Int)
GameTimeFormatter.mktime(year, mon, mday, hour, min, sec) match {
case Some(time) =>lua.pushNumber(time)
case _ => lua.pushNil()
}
}
1
})

View File

@ -74,9 +74,10 @@ class OSAPI(owner: LuaJLuaArchitecture) extends LuaJAPI(owner) {
val mon = getField("month", -1)
val year = getField("year", -1)
val time = GameTimeFormatter.mktime(year, mon, mday, hour, min, sec)
if (time == null) LuaValue.NIL
else LuaValue.valueOf(time: Int)
GameTimeFormatter.mktime(year, mon, mday, hour, min, sec) match {
case Some(time) => LuaValue.valueOf(time)
case _ => LuaValue.NIL
}
}
})

View File

@ -102,12 +102,12 @@ object GameTimeFormatter {
result.toString()
}
def mktime(year: Int, mon: Int, mday: Int, hour: Int, min: Int, sec: Int): Integer = {
if (year < 1970 || mon < 1 || mon > 12) return null
def mktime(year: Int, mon: Int, mday: Int, hour: Int, min: Int, sec: Int): Option[Int] = {
if (year < 1970 || mon < 1 || mon > 12) return None
val monthLengths = monthLengthsForYear(year)
val days = ((year - 1970) * 365.2425).ceil.toInt + (0 until mon - 1).foldLeft(0)((d, m) => d + monthLengths(m)) + mday - 1
val secs = sec + (min + (hour - 1 + days * 24) * 60) * 60
if (secs < 0) null
else secs
if (secs < 0) None
else Option(secs)
}
}