From f4f6881bdd3b25c2ce855a03acf302959312b587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sat, 28 Feb 2015 20:36:04 +0100 Subject: [PATCH 1/2] Trying to prevent NPE in item cost computation. --- src/main/scala/li/cil/oc/util/ItemCosts.scala | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/main/scala/li/cil/oc/util/ItemCosts.scala b/src/main/scala/li/cil/oc/util/ItemCosts.scala index bb436b667..2a1f4e5e1 100644 --- a/src/main/scala/li/cil/oc/util/ItemCosts.scala +++ b/src/main/scala/li/cil/oc/util/ItemCosts.scala @@ -100,24 +100,27 @@ object ItemCosts { } else { val recipes = CraftingManager.getInstance.getRecipeList.map(_.asInstanceOf[IRecipe]) - val recipe = recipes.find(recipe => recipe.getRecipeOutput != null && fuzzyEquals(stack, recipe.getRecipeOutput)) - val (ingredients, output) = recipe match { - case Some(recipe: ShapedRecipes) => (recipe.recipeItems.flatMap(accumulate(_, path :+ stack)).toIterable, recipe.getRecipeOutput.stackSize) - case Some(recipe: ShapelessRecipes) => (recipe.recipeItems.flatMap(accumulate(_, path :+ stack)).toIterable, recipe.getRecipeOutput.stackSize) - case Some(recipe: ShapedOreRecipe) => (recipe.getInput.flatMap(accumulate(_, path :+ stack)).toIterable, recipe.getRecipeOutput.stackSize) - case Some(recipe: ShapelessOreRecipe) => (recipe.getInput.flatMap(accumulate(_, path :+ stack)).toIterable, recipe.getRecipeOutput.stackSize) - case _ => FurnaceRecipes.smelting.getSmeltingList.asInstanceOf[util.Map[ItemStack, ItemStack]].find { - case (_, value) => fuzzyEquals(stack, value) - } match { - case Some((rein, raus)) => (accumulate(rein, path :+ stack), raus.stackSize) - case _ => (Iterable((stack, 1.0)), 1) + if (recipes == null) Iterable((stack, 1.0)) + else { + val recipe = recipes.filter(_ != null).find(recipe => recipe.getRecipeOutput != null && fuzzyEquals(stack, recipe.getRecipeOutput)) + val (ingredients, output) = recipe match { + case Some(recipe: ShapedRecipes) => (recipe.recipeItems.flatMap(accumulate(_, path :+ stack)).toIterable, recipe.getRecipeOutput.stackSize) + case Some(recipe: ShapelessRecipes) => (recipe.recipeItems.flatMap(accumulate(_, path :+ stack)).toIterable, recipe.getRecipeOutput.stackSize) + case Some(recipe: ShapedOreRecipe) => (recipe.getInput.flatMap(accumulate(_, path :+ stack)).toIterable, recipe.getRecipeOutput.stackSize) + case Some(recipe: ShapelessOreRecipe) => (recipe.getInput.flatMap(accumulate(_, path :+ stack)).toIterable, recipe.getRecipeOutput.stackSize) + case _ => FurnaceRecipes.smelting.getSmeltingList.asInstanceOf[util.Map[ItemStack, ItemStack]].find { + case (_, value) => fuzzyEquals(stack, value) + } match { + case Some((rein, raus)) => (accumulate(rein, path :+ stack), raus.stackSize) + case _ => (Iterable((stack, 1.0)), 1) + } } + val scaled = deflate(ingredients.map { + case (ingredient, count) => (ingredient.copy(), count / output) + }).toArray.sortBy(_._1.getUnlocalizedName) + cache += new ItemStackWrapper(stack.copy()) -> scaled + scaled } - val scaled = deflate(ingredients.map { - case (ingredient, count) => (ingredient.copy(), count / output) - }).toArray.sortBy(_._1.getUnlocalizedName) - cache += new ItemStackWrapper(stack.copy()) -> scaled - scaled } } case list: util.ArrayList[ItemStack]@unchecked if !list.isEmpty => From f498deba26e8130ff5ec965a16781c5750e8fae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sun, 1 Mar 2015 19:52:26 +0100 Subject: [PATCH 2/2] Fix potential NPE in directory listing, should close #947. --- src/main/scala/li/cil/oc/common/SaveHandler.scala | 4 ++-- src/main/scala/li/cil/oc/server/fs/Buffered.scala | 2 +- .../scala/li/cil/oc/server/fs/FileInputStreamFileSystem.scala | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/scala/li/cil/oc/common/SaveHandler.scala b/src/main/scala/li/cil/oc/common/SaveHandler.scala index c7fb5b454..2fdc003a8 100644 --- a/src/main/scala/li/cil/oc/common/SaveHandler.scala +++ b/src/main/scala/li/cil/oc/common/SaveHandler.scala @@ -168,7 +168,7 @@ object SaveHandler { val chunk = e.getChunk.getChunkCoordIntPair val dimPath = new io.File(path, dimension.toString) val chunkPath = new io.File(dimPath, s"${chunk.chunkXPos}.${chunk.chunkZPos}") - if (chunkPath.exists && chunkPath.isDirectory) { + if (chunkPath.exists && chunkPath.isDirectory && chunkPath.list() != null) { for (file <- chunkPath.listFiles() if System.currentTimeMillis() - file.lastModified() > TimeToHoldOntoOldSaves) file.delete() } saveData.get(dimension) match { @@ -207,7 +207,7 @@ object SaveHandler { // But that's really not something I'm bothered by, it's a fallback. def recurse(file: File) { file.setLastModified(System.currentTimeMillis()) - if (file.isDirectory) file.listFiles().foreach(recurse) + if (file.exists() && file.isDirectory && file.list() != null) file.listFiles().foreach(recurse) } recurse(statePath) } diff --git a/src/main/scala/li/cil/oc/server/fs/Buffered.scala b/src/main/scala/li/cil/oc/server/fs/Buffered.scala index c09c83d82..f6038e16b 100644 --- a/src/main/scala/li/cil/oc/server/fs/Buffered.scala +++ b/src/main/scala/li/cil/oc/server/fs/Buffered.scala @@ -42,7 +42,7 @@ trait Buffered extends OutputStreamFileSystem { for (child <- directory.listFiles() if isValidFilename(child.getName)) { val childPath = path + child.getName val childFile = new io.File(directory, child.getName) - if (child.isDirectory) { + if (child.exists() && child.isDirectory && child.list() != null) { recurse(childPath + "/", childFile) } else if (!exists(childPath) || !isDirectory(childPath)) { diff --git a/src/main/scala/li/cil/oc/server/fs/FileInputStreamFileSystem.scala b/src/main/scala/li/cil/oc/server/fs/FileInputStreamFileSystem.scala index 56322c7d4..a405bb967 100644 --- a/src/main/scala/li/cil/oc/server/fs/FileInputStreamFileSystem.scala +++ b/src/main/scala/li/cil/oc/server/fs/FileInputStreamFileSystem.scala @@ -3,7 +3,7 @@ package li.cil.oc.server.fs import java.io trait FileInputStreamFileSystem extends InputStreamFileSystem { - protected val root: io.File + protected def root: io.File // ----------------------------------------------------------------------- // @@ -36,7 +36,7 @@ trait FileInputStreamFileSystem extends InputStreamFileSystem { override def list(path: String) = new io.File(root, path) match { case file if file.exists() && file.isFile => Array(file.getName) - case directory if directory.exists() && directory.isDirectory => + case directory if directory.exists() && directory.isDirectory && directory.list() != null => directory.listFiles().map(file => if (file.isDirectory) file.getName + "/" else file.getName) case _ => throw new io.FileNotFoundException("no such file or directory") }