From 4f9dbf3881b4e4d6808c80a41672a65b9272c6ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Wed, 12 Feb 2014 14:41:33 +0100 Subject: [PATCH 1/2] component inventory now clears the tag items are saved to before calling their save method, to behave like tile entities when saving (i.e. get a clean tag, so old stuff doesn't have to be deleted manually); reworked recipe registration to add a pre-registration step that's done right in the item instantiation, keeping things in one place (less likely to forget adding recipes) --- src/main/java/li/cil/oc/Blocks.scala | 40 +++---- src/main/java/li/cil/oc/Items.scala | 99 +++++++++------- src/main/java/li/cil/oc/Recipes.scala | 111 ++++-------------- .../common/inventory/ComponentInventory.scala | 32 +++-- .../server/component/UpgradeGenerator.scala | 7 +- 5 files changed, 115 insertions(+), 174 deletions(-) diff --git a/src/main/java/li/cil/oc/Blocks.scala b/src/main/java/li/cil/oc/Blocks.scala index 6404d1084..7055dccce 100644 --- a/src/main/java/li/cil/oc/Blocks.scala +++ b/src/main/java/li/cil/oc/Blocks.scala @@ -2,11 +2,11 @@ package li.cil.oc import cpw.mods.fml.common.registry.GameRegistry import li.cil.oc.common.block._ -import li.cil.oc.common.{block, tileentity} +import li.cil.oc.common.tileentity import net.minecraft.block.Block import net.minecraft.item.ItemStack -import net.minecraftforge.oredict.OreDictionary import net.minecraft.nbt.NBTTagCompound +import net.minecraftforge.oredict.OreDictionary object Blocks { var blockSimple: SimpleDelegator = _ @@ -67,24 +67,24 @@ object Blocks { // IMPORTANT: the multi block must come first, since the sub blocks will // try to register with it. Also, the order the sub blocks are created in // must not be changed since that order determines their actual IDs. - adapter = new Adapter(blockSimple) - cable = new Cable(blockSpecial) - capacitor = new Capacitor(blockSimple) - case1 = new Case.Tier1(blockSimpleWithRedstone) - case2 = new Case.Tier2(blockSimpleWithRedstone) - case3 = new Case.Tier3(blockSimpleWithRedstone) - charger = new Charger(blockSimpleWithRedstone) - diskDrive = new DiskDrive(blockSimple) - keyboard = new Keyboard(blockSpecial) - powerDistributor = new PowerDistributor(blockSimple) - powerConverter = new PowerConverter(blockSimple) - redstone = new Redstone(blockSimpleWithRedstone) + adapter = Recipes.addBlock(new Adapter(blockSimple), "adapter") + cable = Recipes.addBlock(new Cable(blockSpecial), "cable") + capacitor = Recipes.addBlock(new Capacitor(blockSimple), "capacitor") + case1 = Recipes.addBlock(new Case.Tier1(blockSimpleWithRedstone), "case1") + case2 = Recipes.addBlock(new Case.Tier2(blockSimpleWithRedstone), "case2") + case3 = Recipes.addBlock(new Case.Tier3(blockSimpleWithRedstone), "case3") + charger = Recipes.addBlock(new Charger(blockSimpleWithRedstone), "charger") + diskDrive = Recipes.addBlock(new DiskDrive(blockSimple), "diskDrive") + keyboard = Recipes.addBlock(new Keyboard(blockSpecial), "keyboard") + powerDistributor = Recipes.addBlock(new PowerDistributor(blockSimple), "powerDistributor") + powerConverter = Recipes.addBlock(new PowerConverter(blockSimple), "powerConverter") + redstone = Recipes.addBlock(new Redstone(blockSimpleWithRedstone), "redstone") robotAfterimage = new RobotAfterimage(blockSpecial) - robotProxy = new RobotProxy(blockSpecialWithRedstone) - router = new Router(blockSimple) - screen1 = new Screen.Tier1(blockSimpleWithRedstone) - screen2 = new Screen.Tier2(blockSimpleWithRedstone) - screen3 = new Screen.Tier3(blockSimpleWithRedstone) + robotProxy = Recipes.addBlock(new RobotProxy(blockSpecialWithRedstone), "robot") + router = Recipes.addBlock(new Router(blockSimple), "router") + screen1 = Recipes.addBlock(new Screen.Tier1(blockSimpleWithRedstone), "screen1") + screen2 = Recipes.addBlock(new Screen.Tier2(blockSimpleWithRedstone), "screen2") + screen3 = Recipes.addBlock(new Screen.Tier3(blockSimpleWithRedstone), "screen3") // For automatic conversion from old format (when screens did not take // redstone inputs) to keep save format compatible. @@ -93,7 +93,7 @@ object Blocks { blockSimple.subBlocks += screen3 // v1.2.0 - serverRack = new Rack(blockSpecialWithRedstone) + serverRack = Recipes.addBlock(new Rack(blockSpecialWithRedstone), "rack") register("oc:craftingCable", cable.createItemStack()) register("oc:craftingCapacitor", capacitor.createItemStack()) diff --git a/src/main/java/li/cil/oc/Items.scala b/src/main/java/li/cil/oc/Items.scala index c167743b0..1535c50e5 100644 --- a/src/main/java/li/cil/oc/Items.scala +++ b/src/main/java/li/cil/oc/Items.scala @@ -2,9 +2,11 @@ package li.cil.oc import cpw.mods.fml.common.registry.GameRegistry import li.cil.oc.common.item +import li.cil.oc.util.mods.StargateTech2 import net.minecraft.block.Block import net.minecraft.item.{Item, ItemStack} import net.minecraftforge.oredict.OreDictionary +import scala.collection.convert.WrapAsScala._ object Items { var multi: item.Delegator = _ @@ -71,67 +73,74 @@ object Items { GameRegistry.registerItem(multi, Settings.namespace + "item") - analyzer = new item.Analyzer(multi) + analyzer = Recipes.addItem(new item.Analyzer(multi), "analyzer") - ram1 = new item.Memory(multi, 0) - ram2 = new item.Memory(multi, 1) - ram3 = new item.Memory(multi, 2) + ram1 = Recipes.addItem(new item.Memory(multi, 0), "ram1") + ram2 = Recipes.addItem(new item.Memory(multi, 1), "ram2") + ram3 = Recipes.addItem(new item.Memory(multi, 2), "ram3") - floppyDisk = new item.FloppyDisk(multi) - hdd1 = new item.HardDiskDrive(multi, 0) - hdd2 = new item.HardDiskDrive(multi, 1) - hdd3 = new item.HardDiskDrive(multi, 2) + floppyDisk = Recipes.addItem(new item.FloppyDisk(multi), "floppy") + hdd1 = Recipes.addItem(new item.HardDiskDrive(multi, 0), "hdd1") + hdd2 = Recipes.addItem(new item.HardDiskDrive(multi, 1), "hdd2") + hdd3 = Recipes.addItem(new item.HardDiskDrive(multi, 2), "hdd3") - gpu1 = new item.GraphicsCard(multi, 0) - gpu2 = new item.GraphicsCard(multi, 1) - gpu3 = new item.GraphicsCard(multi, 2) - lan = new item.NetworkCard(multi) - rs = new item.RedstoneCard(multi) - wlan = new item.WirelessNetworkCard(multi) + gpu1 = Recipes.addItem(new item.GraphicsCard(multi, 0), "graphicsCard1") + gpu2 = Recipes.addItem(new item.GraphicsCard(multi, 1), "graphicsCard2") + gpu3 = Recipes.addItem(new item.GraphicsCard(multi, 2), "graphicsCard3") + lan = Recipes.addItem(new item.NetworkCard(multi), "lanCard") + rs = Recipes.addItem(new item.RedstoneCard(multi), "redstoneCard") + wlan = Recipes.addItem(new item.WirelessNetworkCard(multi), "wlanCard") - upgradeCrafting = new item.UpgradeCrafting(multi) - upgradeGenerator = new item.UpgradeGenerator(multi) + upgradeCrafting = Recipes.addItem(new item.UpgradeCrafting(multi), "craftingUpgrade") + upgradeGenerator = Recipes.addItem(new item.UpgradeGenerator(multi), "generatorUpgrade") ironNugget = new item.IronNugget(multi) - cuttingWire = new item.CuttingWire(multi) - acid = new item.Acid(multi) - disk = new item.Disk(multi) + if (OreDictionary.getOres("nuggetIron").exists(ironNugget.createItemStack().isItemEqual)) { + Recipes.addItem(ironNugget, "nuggetIron") + } - buttonGroup = new item.ButtonGroup(multi) - arrowKeys = new item.ArrowKeys(multi) - numPad = new item.NumPad(multi) + cuttingWire = Recipes.addItem(new item.CuttingWire(multi), "cuttingWire") + acid = Recipes.addItem(new item.Acid(multi), "acid") + disk = Recipes.addItem(new item.Disk(multi), "disk") - transistor = new item.Transistor(multi) - chip1 = new item.Microchip(multi, 0) - chip2 = new item.Microchip(multi, 1) - chip3 = new item.Microchip(multi, 2) - alu = new item.ALU(multi) - cu = new item.ControlUnit(multi) - cpu0 = new item.CPU(multi, 0) + buttonGroup = Recipes.addItem(new item.ButtonGroup(multi), "buttonGroup") + arrowKeys = Recipes.addItem(new item.ArrowKeys(multi), "arrowKeys") + numPad = Recipes.addItem(new item.NumPad(multi), "numPad") - rawCircuitBoard = new item.RawCircuitBoard(multi) - circuitBoard = new item.CircuitBoard(multi) - pcb = new item.PrintedCircuitBoard(multi) - card = new item.CardBase(multi) + transistor = Recipes.addItem(new item.Transistor(multi), "transistor") + chip1 = Recipes.addItem(new item.Microchip(multi, 0), "chip1") + chip2 = Recipes.addItem(new item.Microchip(multi, 1), "chip2") + chip3 = Recipes.addItem(new item.Microchip(multi, 2), "chip3") + alu = Recipes.addItem(new item.ALU(multi), "alu") + cu = Recipes.addItem(new item.ControlUnit(multi), "cu") + cpu0 = Recipes.addItem(new item.CPU(multi, 0), "cpu0") + + rawCircuitBoard = Recipes.addItem(new item.RawCircuitBoard(multi), "rawCircuitBoard") + circuitBoard = Recipes.addItem(new item.CircuitBoard(multi), "circuitBoard") + pcb = Recipes.addItem(new item.PrintedCircuitBoard(multi), "printedCircuitBoard") + card = Recipes.addItem(new item.CardBase(multi), "card") // v1.1.0 - upgradeSolarGenerator = new item.UpgradeSolarGenerator(multi) - upgradeSign = new item.UpgradeSign(multi) - upgradeNavigation = new item.UpgradeNavigation(multi) + upgradeSolarGenerator = Recipes.addItem(new item.UpgradeSolarGenerator(multi), "solarGeneratorUpgrade") + upgradeSign = Recipes.addItem(new item.UpgradeSign(multi), "signUpgrade") + upgradeNavigation = Recipes.addItem(new item.UpgradeNavigation(multi), "navigationUpgrade") abstractBus = new item.AbstractBusCard(multi) + if (StargateTech2.isAvailable) { + Recipes.addItem(abstractBus, "abstractBusCard") + } - ram4 = new item.Memory(multi, 3) - ram5 = new item.Memory(multi, 4) + ram4 = Recipes.addItem(new item.Memory(multi, 3), "ram4") + ram5 = Recipes.addItem(new item.Memory(multi, 4), "ram5") // v1.2.0 - server3 = new item.Server(multi, 2) - terminal = new item.Terminal(multi) - cpu1 = new item.CPU(multi, 1) - cpu2 = new item.CPU(multi, 2) - internet = new item.InternetCard(multi) - server1 = new item.Server(multi, 0) - server2 = new item.Server(multi, 1) + server3 = Recipes.addItem(new item.Server(multi, 2), "server3") + terminal = Recipes.addItem(new item.Terminal(multi), "terminal") + cpu1 = Recipes.addItem(new item.CPU(multi, 1), "cpu1") + cpu2 = Recipes.addItem(new item.CPU(multi, 2), "cpu2") + internet = Recipes.addItem(new item.InternetCard(multi), "internetCard") + server1 = Recipes.addItem(new item.Server(multi, 0), "server1") + server2 = Recipes.addItem(new item.Server(multi, 1), "server2") // ----------------------------------------------------------------------- // diff --git a/src/main/java/li/cil/oc/Recipes.scala b/src/main/java/li/cil/oc/Recipes.scala index c5641e90a..fd972b870 100644 --- a/src/main/java/li/cil/oc/Recipes.scala +++ b/src/main/java/li/cil/oc/Recipes.scala @@ -5,18 +5,28 @@ import cpw.mods.fml.common.Loader import cpw.mods.fml.common.registry.GameRegistry import java.io.{FileReader, File} import java.util.logging.Level -import li.cil.oc.common.block.Delegator -import li.cil.oc.util.mods.{StargateTech2, GregTech} +import li.cil.oc.util.mods.GregTech import net.minecraft.block.Block import net.minecraft.item.crafting.FurnaceRecipes import net.minecraft.item.{ItemStack, Item} import net.minecraftforge.oredict.{OreDictionary, ShapelessOreRecipe, ShapedOreRecipe} import org.apache.commons.io.FileUtils -import scala.Some -import scala.collection.convert.wrapAsScala._ -import scala.collection.mutable.ArrayBuffer +import scala.collection.convert.WrapAsScala._ +import scala.collection.mutable object Recipes { + val list = mutable.LinkedHashMap.empty[ItemStack, String] + + def addBlock[T <: common.block.Delegate](block: T, name: String) = { + list += block.createItemStack() -> name + block + } + + def addItem[T <: common.item.Delegate](item: T, name: String) = { + list += item.createItemStack() -> name + item + } + def init() { try { val defaultRecipes = new File(Loader.instance.getConfigDir + File.separator + "opencomputers" + File.separator + "default.recipes") @@ -52,89 +62,10 @@ object Recipes { }) val recipes = ConfigFactory.parseFile(userRecipes, config) - // Try to keep this in the same order as the fields in the Items class - // to make it easier to match them and check if anything is missing. - addRecipe(Items.analyzer.createItemStack(), recipes, "analyzer") - addRecipe(Items.terminal.createItemStack(), recipes, "terminal") - - addRecipe(Items.ram1.createItemStack(), recipes, "ram1") - addRecipe(Items.ram2.createItemStack(), recipes, "ram2") - addRecipe(Items.ram3.createItemStack(), recipes, "ram3") - addRecipe(Items.ram4.createItemStack(), recipes, "ram4") - addRecipe(Items.ram5.createItemStack(), recipes, "ram5") - - addRecipe(Items.floppyDisk.createItemStack(), recipes, "floppy") - addRecipe(Items.hdd1.createItemStack(), recipes, "hdd1") - addRecipe(Items.hdd2.createItemStack(), recipes, "hdd2") - addRecipe(Items.hdd3.createItemStack(), recipes, "hdd3") - - addRecipe(Items.server1.createItemStack(), recipes, "server1") - addRecipe(Items.server2.createItemStack(), recipes, "server2") - addRecipe(Items.server3.createItemStack(), recipes, "server3") - - if (StargateTech2.isAvailable) { - addRecipe(Items.abstractBus.createItemStack(), recipes, "abstractBusCard") + // Register all known recipes. + for ((stack, name) <- list) { + addRecipe(stack, recipes, name) } - addRecipe(Items.gpu1.createItemStack(), recipes, "graphicsCard1") - addRecipe(Items.gpu2.createItemStack(), recipes, "graphicsCard2") - addRecipe(Items.gpu3.createItemStack(), recipes, "graphicsCard3") - addRecipe(Items.internet.createItemStack(), recipes, "internetCard") - addRecipe(Items.lan.createItemStack(), recipes, "lanCard") - addRecipe(Items.rs.createItemStack(), recipes, "redstoneCard") - addRecipe(Items.wlan.createItemStack(), recipes, "wlanCard") - - addRecipe(Items.upgradeCrafting.createItemStack(), recipes, "craftingUpgrade") - addRecipe(Items.upgradeGenerator.createItemStack(), recipes, "generatorUpgrade") - addRecipe(Items.upgradeNavigation.createItemStack(), recipes, "navigationUpgrade") - addRecipe(Items.upgradeSign.createItemStack(), recipes, "signUpgrade") - addRecipe(Items.upgradeSolarGenerator.createItemStack(), recipes, "solarGeneratorUpgrade") - - if (OreDictionary.getOres("nuggetIron").exists(Items.ironNugget.createItemStack().isItemEqual)) { - addRecipe(Items.ironNugget.createItemStack(), recipes, "nuggetIron") - } - addRecipe(Items.cuttingWire.createItemStack(), recipes, "cuttingWire") - addRecipe(Items.acid.createItemStack(), recipes, "acid") - addRecipe(Items.disk.createItemStack(), recipes, "disk") - - addRecipe(Items.buttonGroup.createItemStack(), recipes, "buttonGroup") - addRecipe(Items.arrowKeys.createItemStack(), recipes, "arrowKeys") - addRecipe(Items.numPad.createItemStack(), recipes, "numPad") - - addRecipe(Items.transistor.createItemStack(), recipes, "transistor") - addRecipe(Items.chip1.createItemStack(), recipes, "chip1") - addRecipe(Items.chip2.createItemStack(), recipes, "chip2") - addRecipe(Items.chip3.createItemStack(), recipes, "chip3") - addRecipe(Items.alu.createItemStack(), recipes, "alu") - addRecipe(Items.cpu0.createItemStack(), recipes, "cpu0") - addRecipe(Items.cpu1.createItemStack(), recipes, "cpu1") - addRecipe(Items.cpu2.createItemStack(), recipes, "cpu2") - addRecipe(Items.cu.createItemStack(), recipes, "cu") - - addRecipe(Items.rawCircuitBoard.createItemStack(), recipes, "rawCircuitBoard") - addRecipe(Items.circuitBoard.createItemStack(), recipes, "circuitBoard") - addRecipe(Items.pcb.createItemStack(), recipes, "printedCircuitBoard") - addRecipe(Items.card.createItemStack(), recipes, "card") - - // Try to keep this in the same order as the fields in the Blocks class - // to make it easier to match them and check if anything is missing.Point") - addRecipe(Blocks.adapter.createItemStack(), recipes, "adapter") - addRecipe(Blocks.cable.createItemStack(), recipes, "cable") - addRecipe(Blocks.capacitor.createItemStack(), recipes, "capacitor") - addRecipe(Blocks.charger.createItemStack(), recipes, "charger") - addRecipe(Blocks.case1.createItemStack(), recipes, "case1") - addRecipe(Blocks.case2.createItemStack(), recipes, "case2") - addRecipe(Blocks.case3.createItemStack(), recipes, "case3") - addRecipe(Blocks.diskDrive.createItemStack(), recipes, "diskDrive") - addRecipe(Blocks.keyboard.createItemStack(), recipes, "keyboard") - addRecipe(Blocks.powerConverter.createItemStack(), recipes, "powerConverter") - addRecipe(Blocks.powerDistributor.createItemStack(), recipes, "powerDistributor") - addRecipe(Blocks.redstone.createItemStack(), recipes, "redstone") - addRecipe(Blocks.robotProxy.createItemStack(), recipes, "robot") - addRecipe(Blocks.router.createItemStack(), recipes, "router") - addRecipe(Blocks.screen1.createItemStack(), recipes, "screen1") - addRecipe(Blocks.screen2.createItemStack(), recipes, "screen2") - addRecipe(Blocks.screen3.createItemStack(), recipes, "screen3") - addRecipe(Blocks.serverRack.createItemStack(), recipes, "rack") // Navigation upgrade recrafting. GameRegistry.addRecipe(new ShapelessOreRecipe(Items.upgradeNavigation.createItemStack(), Items.upgradeNavigation.createItemStack(), new ItemStack(Item.map, 1, OreDictionary.WILDCARD_VALUE))) @@ -184,8 +115,8 @@ object Recipes { output.stackSize = tryGetCount(recipe) var number = -1 - var shape = ArrayBuffer.empty[String] - val input = ArrayBuffer.empty[AnyRef] + var shape = mutable.ArrayBuffer.empty[String] + val input = mutable.ArrayBuffer.empty[AnyRef] for (row <- rows) { val (pattern, ingredients) = row.foldLeft((new StringBuilder, Seq.empty[AnyRef]))((acc, ingredient) => { val (pattern, ingredients) = acc @@ -344,7 +275,7 @@ object Recipes { private def hide(value: ItemStack) { Items.multi.subItem(value) match { case Some(stack) => stack.showInItemList = false - case _ => Delegator.subBlock(value) match { + case _ => common.block.Delegator.subBlock(value) match { case Some(block) => block.showInItemList = false case _ => } diff --git a/src/main/java/li/cil/oc/common/inventory/ComponentInventory.scala b/src/main/java/li/cil/oc/common/inventory/ComponentInventory.scala index 46b4c0a1c..bd82f7081 100644 --- a/src/main/java/li/cil/oc/common/inventory/ComponentInventory.scala +++ b/src/main/java/li/cil/oc/common/inventory/ComponentInventory.scala @@ -8,8 +8,9 @@ import li.cil.oc.api.network.{Node, ManagedEnvironment} import li.cil.oc.server.driver.Registry import li.cil.oc.server.driver.item.Item import net.minecraft.item.ItemStack -import net.minecraft.nbt.NBTTagCompound +import net.minecraft.nbt.{NBTBase, NBTTagCompound} import net.minecraft.tileentity.TileEntity +import scala.collection.convert.WrapAsScala._ import scala.collection.mutable trait ComponentInventory extends Inventory with network.Environment { @@ -75,12 +76,7 @@ trait ComponentInventory extends Inventory with network.Environment { case (stack, slot) => components(slot) match { case Some(component) => // We're guaranteed to have a driver for entries. - val driver = Registry.itemDriverFor(stack).get - try { - component.save(dataTag(driver, stack)) - } catch { - case e: Throwable => OpenComputers.log.log(Level.WARNING, "An item component of type '%s' (provided by driver '%s') threw an error while saving.".format(component.getClass.getName, driver.getClass.getName), e) - } + save(component, Registry.itemDriverFor(stack).get, stack) case _ => // Nothing special to save. } } @@ -106,7 +102,7 @@ trait ComponentInventory extends Inventory with network.Environment { assert(!updatingComponents.contains(component)) updatingComponents += component } - component.save(dataTag(driver, stack)) + save(component, driver, stack) } case _ => // No environment (e.g. RAM). } @@ -125,11 +121,7 @@ trait ComponentInventory extends Inventory with network.Environment { components(slot) = None updatingComponents -= component component.node.remove() - Registry.itemDriverFor(stack).foreach(driver => try { - component.save(dataTag(driver, stack)) - } catch { - case e: Throwable => OpenComputers.log.log(Level.WARNING, "An item component of type '%s' (provided by driver '%s') threw an error while saving.".format(component.getClass.getName, driver.getClass.getName), e) - }) + Registry.itemDriverFor(stack).foreach(driver => save(component, driver, stack)) } case _ => // Nothing to do. } @@ -143,4 +135,18 @@ trait ComponentInventory extends Inventory with network.Environment { protected def dataTag(driver: ItemDriver, stack: ItemStack) = Option(driver.dataTag(stack)).getOrElse(Item.dataTag(stack)) + + protected def save(component: ManagedEnvironment, driver: ItemDriver, stack: ItemStack) { + try { + val tag = dataTag(driver, stack) + // Clear the tag compound before saving to get the same behavior as + // in tile entities (otherwise entries have to be cleared manually). + for (key <- tag.getTags.map(_.asInstanceOf[NBTBase].getName)) { + tag.removeTag(key) + } + component.save(tag) + } catch { + case e: Throwable => OpenComputers.log.log(Level.WARNING, "An item component of type '%s' (provided by driver '%s') threw an error while saving.".format(component.getClass.getName, driver.getClass.getName), e) + } + } } diff --git a/src/main/java/li/cil/oc/server/component/UpgradeGenerator.scala b/src/main/java/li/cil/oc/server/component/UpgradeGenerator.scala index 91f8b7b14..c420570c9 100644 --- a/src/main/java/li/cil/oc/server/component/UpgradeGenerator.scala +++ b/src/main/java/li/cil/oc/server/component/UpgradeGenerator.scala @@ -134,16 +134,11 @@ class UpgradeGenerator(val owner: TileEntity) extends ManagedComponent { override def save(nbt: NBTTagCompound) { super.save(nbt) inventory match { - case Some(stack) => - nbt.setNewCompoundTag("inventory", stack.writeToNBT) + case Some(stack) => nbt.setNewCompoundTag("inventory", stack.writeToNBT) case _ => - nbt.removeTag("inventory") } if (remainingTicks > 0) { nbt.setInteger("remainingTicks", remainingTicks) } - else { - nbt.removeTag("remainingTicks") - } } } From b931baeb723d6008f91f53727557d7b466037a02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Wed, 12 Feb 2014 14:51:52 +0100 Subject: [PATCH 2/2] changed a couple of failure cases to return `nil, message` instead of `false, message`, for consistency --- .../li/cil/oc/server/component/AbstractBus.scala | 2 +- .../li/cil/oc/server/component/GraphicsCard.scala | 10 +++++----- .../li/cil/oc/server/component/InternetCard.scala | 8 +++++--- .../cil/oc/server/component/UpgradeGenerator.scala | 8 +++++--- .../li/cil/oc/server/component/robot/Robot.scala | 12 ++++++------ 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/main/java/li/cil/oc/server/component/AbstractBus.scala b/src/main/java/li/cil/oc/server/component/AbstractBus.scala index 62b4c4bf2..12b77b79a 100644 --- a/src/main/java/li/cil/oc/server/component/AbstractBus.scala +++ b/src/main/java/li/cil/oc/server/component/AbstractBus.scala @@ -81,7 +81,7 @@ class AbstractBus(val device: IBusDevice) extends ManagedComponent with IBusDriv busInterface.sendAllPackets() result(true) } - else result(false, "not enough energy") + else result(Unit, "not enough energy") } @Callback(direct = true, doc = """function():number -- The maximum packet size that can be sent over the bus.""") diff --git a/src/main/java/li/cil/oc/server/component/GraphicsCard.scala b/src/main/java/li/cil/oc/server/component/GraphicsCard.scala index db7079ef4..0a919c5d1 100644 --- a/src/main/java/li/cil/oc/server/component/GraphicsCard.scala +++ b/src/main/java/li/cil/oc/server/component/GraphicsCard.scala @@ -53,7 +53,7 @@ abstract class GraphicsCard extends ManagedComponent { def bind(context: Context, args: Arguments): Array[AnyRef] = { val address = args.checkString(0) node.network.node(address) match { - case null => result(false, "invalid address") + case null => result(Unit, "invalid address") case node: Node if node.host.isInstanceOf[Buffer] => screenAddress = Option(address) screenInstance = Some(node.host.asInstanceOf[Buffer]) @@ -66,7 +66,7 @@ abstract class GraphicsCard extends ManagedComponent { s.background = 0x000000 result(true) }) - case _ => result(false, "not a screen") + case _ => result(Unit, "not a screen") } } @@ -166,7 +166,7 @@ abstract class GraphicsCard extends ManagedComponent { s.set(x, y, value) result(true) } - else result(false) + else result(Unit, "not enough energy") }) } @@ -182,7 +182,7 @@ abstract class GraphicsCard extends ManagedComponent { s.copy(x, y, w, h, tx, ty) result(true) } - else result(false) + else result(Unit, "not enough energy") }) } @@ -200,7 +200,7 @@ abstract class GraphicsCard extends ManagedComponent { result(true) } else { - result(false) + result(Unit, "not enough energy") } }) else throw new Exception("invalid fill value") diff --git a/src/main/java/li/cil/oc/server/component/InternetCard.scala b/src/main/java/li/cil/oc/server/component/InternetCard.scala index 538ae6bca..743c586ff 100644 --- a/src/main/java/li/cil/oc/server/component/InternetCard.scala +++ b/src/main/java/li/cil/oc/server/component/InternetCard.scala @@ -54,12 +54,12 @@ class InternetCard extends ManagedComponent { } val address = args.checkString(0) if (!Settings.get.httpEnabled) { - return result(false, "http requests are unavailable") + return result(Unit, "http requests are unavailable") } val post = if (args.isString(1)) Option(args.checkString(1)) else None this.synchronized { if (request.isDefined || queue.isDefined) { - return result(false, "already busy with another request") + return result(Unit, "already busy with another request") } scheduleRequest(address, post) } @@ -137,7 +137,9 @@ class InternetCard extends ManagedComponent { def connect(context: Context, args: Arguments): Array[AnyRef] = { val address = args.checkString(0) val port = if (args.count > 1) args.checkInteger(1) else -1 - if (!Settings.get.tcpEnabled) return result(false, "tcp connections are unavailable") + if (!Settings.get.tcpEnabled) { + return result(Unit, "tcp connections are unavailable") + } if (connections.size >= Settings.get.maxConnections) { throw new IOException("too many open connections") } diff --git a/src/main/java/li/cil/oc/server/component/UpgradeGenerator.scala b/src/main/java/li/cil/oc/server/component/UpgradeGenerator.scala index c420570c9..f6d926a3c 100644 --- a/src/main/java/li/cil/oc/server/component/UpgradeGenerator.scala +++ b/src/main/java/li/cil/oc/server/component/UpgradeGenerator.scala @@ -27,16 +27,18 @@ class UpgradeGenerator(val owner: TileEntity) extends ManagedComponent { val player = context.player val stack = player.inventory.getStackInSlot(context.selectedSlot) if (stack == null) throw new IllegalArgumentException("selected slot is empty") - if (!TileEntityFurnace.isItemFuel(stack)) return result(false, "selected slot does not contain fuel") + if (!TileEntityFurnace.isItemFuel(stack)) { + return result(Unit, "selected slot does not contain fuel") + } inventory match { case Some(existingStack) => if (!existingStack.isItemEqual(stack) || !ItemStack.areItemStackTagsEqual(existingStack, stack)) { - return result(false, "different fuel type already queued") + return result(Unit, "different fuel type already queued") } val space = existingStack.getMaxStackSize - existingStack.stackSize if (space <= 0) { - return result(false, "queue is full") + return result(Unit, "queue is full") } val moveCount = math.min(stack.stackSize, math.min(space, count)) existingStack.stackSize += moveCount diff --git a/src/main/java/li/cil/oc/server/component/robot/Robot.scala b/src/main/java/li/cil/oc/server/component/robot/Robot.scala index c59c8fc0c..5ee2460b2 100644 --- a/src/main/java/li/cil/oc/server/component/robot/Robot.scala +++ b/src/main/java/li/cil/oc/server/component/robot/Robot.scala @@ -227,7 +227,7 @@ class Robot(val robot: tileentity.Robot) extends Machine(robot) with RobotContex val sneaky = args.isBoolean(2) && args.checkBoolean(2) val stack = player.robotInventory.selectedItemStack if (stack == null || stack.stackSize == 0) { - return result(false, "nothing selected") + return result(Unit, "nothing selected") } for (side <- sides) { @@ -494,16 +494,16 @@ class Robot(val robot: tileentity.Robot) extends Machine(robot) with RobotContex if (robot.isAnimatingMove) { // This shouldn't really happen due to delays being enforced, but just to // be on the safe side... - result(false, "already moving") + result(Unit, "already moving") } else { val (something, what) = blockContent(robot.player(direction), direction) if (something) { - result(false, what) + result(Unit, what) } else { if (!robot.computer.node.tryChangeBuffer(-Settings.get.robotMoveCost)) { - result(false, "not enough energy") + result(Unit, "not enough energy") } else if (robot.move(direction)) { context.pause(Settings.get.moveDelay) @@ -512,7 +512,7 @@ class Robot(val robot: tileentity.Robot) extends Machine(robot) with RobotContex } else { robot.computer.node.changeBuffer(Settings.get.robotMoveCost) - result(false, "impossible move") + result(Unit, "impossible move") } } } @@ -529,7 +529,7 @@ class Robot(val robot: tileentity.Robot) extends Machine(robot) with RobotContex result(true) } else { - result(false, "not enough energy") + result(Unit, "not enough energy") } }