From bd2fc7dd9215cc94abea9c6837c766c0928c7a79 Mon Sep 17 00:00:00 2001 From: Chicken-Bones Date: Tue, 24 Jun 2014 12:53:10 +1000 Subject: [PATCH] Fixed microblock registrations from config and IMC messages Added new plank microblocks --- .../microblock/ConfigContent.scala | 227 ++++++++---------- .../microblock/DefaultContent.scala | 2 +- 2 files changed, 96 insertions(+), 133 deletions(-) diff --git a/src/codechicken/microblock/ConfigContent.scala b/src/codechicken/microblock/ConfigContent.scala index 87e24d6..d5f6b44 100644 --- a/src/codechicken/microblock/ConfigContent.scala +++ b/src/codechicken/microblock/ConfigContent.scala @@ -13,174 +13,137 @@ import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage import net.minecraft.item.ItemStack import BlockMicroMaterial.createAndRegister import BlockMicroMaterial.materialKey -import java.util.ArrayList +import java.lang.{Iterable => JIterable} import scala.collection.JavaConversions._ -import net.minecraft.init.Blocks object ConfigContent { private val nameMap = MMap[String, Seq[Int]]() - private val idMap = new ArrayList[Seq[Int]]() - - def parse(cfgDir:File) - { + + def parse(cfgDir: File) { val cfgFile = new File(cfgDir, "microblocks.cfg") - try - { - if(!cfgFile.exists()) + try { + if (!cfgFile.exists()) generateDefault(cfgFile) else loadLines(cfgFile) } - catch - { - case e:IOException => e.printStackTrace() + catch { + case e: IOException => e.printStackTrace() } } - - def generateDefault(cfgFile:File) - { + + def generateDefault(cfgFile: File) { val writer = new PrintWriter(cfgFile) writer.println("#Configuration file for adding microblock materials for aesthetic blocks added by mods") - writer.println("#Each line needs to be of the form :") - writer.println("# may be a numerical id or the unlocalised name of the block/item enclosed in double quotes. NEI can help you find these") + writer.println("#Each line needs to be of the form :") + writer.println("# is the unlocalised name or registry key of the block/item enclosed in quotes. NEI can help you find these") writer.println("# may be ommitted, in which case it defaults to 0, otherwise it can be a number, a comma separated list of numbers, or a dash separated range") - writer.println("#Ex. 3. 5:3. 213:1,2,3,5. \"tile.blockIron\":0-15") + writer.println("#Ex. dirt minecraft:planks:3 iron_ore:1,2,3,5 ThermalFoundation:Storage:0-15") writer.close() } - - def loadLine(line:String) - { - if(line.startsWith("#")) + + def loadLine(line: String) { + if (line.startsWith("#") || line.length < 3) return - - var split1 = line.replace(" ", "").split(":") - if(split1.length > 2) - throw new IllegalArgumentException("Invalid number of ':' separators") - if(split1.length == 1) - split1 = Array(split1(0), "0") - - if(split1(0) == "") - return - - val split2 = split1(1).split(",") - val meta = split2.flatMap{s => - if(s.contains("-")) - { - val split2 = s.split("-") - if(split2.length != 2) - throw new IllegalArgumentException("Invalid - separated range") - split2(0).toInt to split2(1).toInt - } - else - { - Seq(s.toInt) + + if(line.charAt(0) != '\"') + throw new IllegalArgumentException("Line must begin with a quote") + val q2 = line.indexOf('\"', 1) + if(q2 < 0) + throw new IllegalArgumentException("Unmatched quotes") + + var name = line.substring(1, q2) + if(!name.contains('.') && !name.contains(':')) + name = "minecraft:"+name + + var meta = Seq(0) + if(line.length > q2+1) { + if(line.charAt(q2+1) != ':') + throw new IllegalArgumentException("Name must be followed by a colon separator") + + meta = line.substring(q2+2).split(",").flatMap{s => + if (s.contains("-")) { + val split2 = s.split("-") + if (split2.length != 2) + throw new IllegalArgumentException("Invalid - separated range") + split2(0).toInt to split2(1).toInt + } + else { + Seq(s.toInt) + } } } - - val s_id = split1(0) - if(s_id.charAt(0) == '\"') - { - if(!s_id.endsWith("\"")) - throw new IllegalArgumentException("Missing closing \" character") - - val name = s_id.substring(1, s_id.length-1) - nameMap.put(name, meta) - } - else - { - idMap(s_id.toInt) = meta - } + + nameMap.put(name, meta) } - - def loadLines(cfgFile:File) - { + + def loadLines(cfgFile: File) { val reader = new BufferedReader(new FileReader(cfgFile)) - var s:String = null - do - { + var s: String = null + do { s = reader.readLine - if(s != null) - { - try - { + if (s != null) { + try { loadLine(s) } - catch - { - case e:Exception => - logger.error("Invalid line in microblocks.cfg: "+s) + catch { + case e: Exception => + logger.error("Invalid line in microblocks.cfg: " + s) logger.error(e.getMessage) } } } - while(s != null) + while (s != null) reader.close() } - - def load() - { - for(i <- 0 until idMap.size) - { - val block = Block.getBlockById(i) - if(idMap(i) != null) - { - if(block == Blocks.air) - logger.warn("Unable to add micro material for block with ID "+i+" as it doesn't exist") - else - createAndRegister(block, idMap(i)) - } - else if(block != null) - { - val name = block.getUnlocalizedName - val value = nameMap.get(name) - if(value.isDefined) - { - nameMap.remove(name) - value.get.foreach{m => - try { - createAndRegister(block, m) - } - catch { - case e:IllegalStateException => logger.error("Unable to register micro material: "+ - materialKey(block, m)+"\n\t"+e.getMessage) - case e:Exception => - logger.error("Unable to register micro material: "+materialKey(block, m)) - e.printStackTrace() - } - } - } - } - } - - nameMap.foreach(e => logger.warn("Unable to add micro material for block with unlocalised name "+e._1+" as it doesn't exist")) - } - - def handleIMC(messages:Seq[IMCMessage]) { - messages.filter(_.key == "microMaterial").foreach{msg => - - def error(s:String) { - logger.error("Invalid microblock IMC message from "+msg.getSender+": "+s) - } - - if(msg.getMessageType != classOf[ItemStack]) - error("value is not an instanceof ItemStack") - else { - val stack = msg.getItemStackValue - if(Block.blockRegistry.containsId(Item.getIdFromItem(stack.getItem))) - error("Invalid Block: "+stack.getItem) - else if(stack.getItemDamage < 0 || stack.getItemDamage >= 16) - error("Invalid metadata: "+stack.getItemDamage) - else { + + def load() { + for(block <- Block.blockRegistry.asInstanceOf[JIterable[Block]]) { + val metas = Seq(block.getUnlocalizedName, Block.blockRegistry.getNameForObject(block)).flatMap(nameMap.remove).flatten + metas.foreach{m => try { - createAndRegister(Block.getBlockFromItem(stack.getItem), stack.getItemDamage) + createAndRegister(block, m) } catch { - case e:IllegalStateException => error("Unable to register micro material: "+ - materialKey(Block.getBlockFromItem(stack.getItem), stack.getItemDamage)+"\n\t"+e.getMessage) + case e: IllegalStateException => logger.error("Unable to register micro material: " + + materialKey(block, m) + "\n\t" + e.getMessage) + case e: Exception => + logger.error("Unable to register micro material: " + materialKey(block, m)) + e.printStackTrace() + } + } + } + + nameMap.foreach(e => logger.warn("Unable to add micro material for block with unlocalised name " + e._1 + " as it doesn't exist")) + } + + def handleIMC(messages: Seq[IMCMessage]) { + messages.filter(_.key == "microMaterial").foreach { + msg => + + def error(s: String) { + logger.error("Invalid microblock IMC message from " + msg.getSender + ": " + s) + } + + if (msg.getMessageType != classOf[ItemStack]) + error("value is not an instanceof ItemStack") + else { + val stack = msg.getItemStackValue + if (!Block.blockRegistry.containsId(Item.getIdFromItem(stack.getItem))) + error("Invalid Block: " + stack.getItem) + else if (stack.getItemDamage < 0 || stack.getItemDamage >= 16) + error("Invalid metadata: " + stack.getItemDamage) + else { + try { + createAndRegister(Block.getBlockFromItem(stack.getItem), stack.getItemDamage) + } + catch { + case e: IllegalStateException => error("Unable to register micro material: " + + materialKey(Block.getBlockFromItem(stack.getItem), stack.getItemDamage) + "\n\t" + e.getMessage) + } } } - } } } } \ No newline at end of file diff --git a/src/codechicken/microblock/DefaultContent.scala b/src/codechicken/microblock/DefaultContent.scala index 4cde73c..93855a2 100644 --- a/src/codechicken/microblock/DefaultContent.scala +++ b/src/codechicken/microblock/DefaultContent.scala @@ -17,7 +17,7 @@ object DefaultContent createAndRegister(stone) createAndRegister(dirt, 0 to 2) createAndRegister(cobblestone) - createAndRegister(planks, 0 to 3) + createAndRegister(planks, 0 to 5) createAndRegister(log, 0 to 3) createAndRegister(log2, 0 to 1, "tile.log2") createAndRegister(leaves, 0 to 3)