From 302429f34028b0d1f3379f5b233aa0b802b113e7 Mon Sep 17 00:00:00 2001 From: bdew Date: Mon, 23 Dec 2013 13:27:00 +0200 Subject: [PATCH] Refactoring, cleanup and moving stuff to bdlib --- build.gradle | 2 +- src/net/bdew/lib/BdLib.scala | 9 +++ src/net/bdew/lib/Misc.scala | 4 +- src/net/bdew/lib/config/BlockManager.scala | 37 ++++++++++ src/net/bdew/lib/config/FluidManager.scala | 20 ++++++ src/net/bdew/lib/config/IdManager.scala | 46 +++++++++++++ src/net/bdew/lib/config/ItemManager.scala | 33 +++++++++ src/net/bdew/lib/config/MachineManager.scala | 23 +++++++ .../lib/gui/widgets/WidgetFluidGauge.scala | 2 +- src/net/bdew/lib/items/SimpleItem.scala | 25 +++++++ src/net/bdew/lib/machine/Machine.scala | 34 ++++++++++ src/net/bdew/lib/machine/PoweredMachine.scala | 19 ++++++ .../bdew/lib/machine/ProcessorMachine.scala | 15 +++++ src/net/bdew/lib/power/DataSlotPower.scala | 65 ++++++++++++++++++ src/net/bdew/lib/power/ItemPoweredBase.scala | 49 ++++++++++++++ .../bdew/lib/power/TileBaseProcessor.scala | 67 +++++++++++++++++++ .../bdew/lib/power/TileItemProcessor.scala | 26 +++++++ src/net/bdew/lib/power/TilePoweredBase.scala | 18 +++++ src/net/bdew/lib/power/WidgetPowerGauge.scala | 28 ++++++++ .../lib/recipes/gencfg/ConfigSection.scala | 5 +- 20 files changed, 521 insertions(+), 6 deletions(-) create mode 100644 src/net/bdew/lib/config/BlockManager.scala create mode 100644 src/net/bdew/lib/config/FluidManager.scala create mode 100644 src/net/bdew/lib/config/IdManager.scala create mode 100644 src/net/bdew/lib/config/ItemManager.scala create mode 100644 src/net/bdew/lib/config/MachineManager.scala create mode 100644 src/net/bdew/lib/items/SimpleItem.scala create mode 100644 src/net/bdew/lib/machine/Machine.scala create mode 100644 src/net/bdew/lib/machine/PoweredMachine.scala create mode 100644 src/net/bdew/lib/machine/ProcessorMachine.scala create mode 100644 src/net/bdew/lib/power/DataSlotPower.scala create mode 100644 src/net/bdew/lib/power/ItemPoweredBase.scala create mode 100644 src/net/bdew/lib/power/TileBaseProcessor.scala create mode 100644 src/net/bdew/lib/power/TileItemProcessor.scala create mode 100644 src/net/bdew/lib/power/TilePoweredBase.scala create mode 100644 src/net/bdew/lib/power/WidgetPowerGauge.scala diff --git a/build.gradle b/build.gradle index fe7c4e6..57be676 100644 --- a/build.gradle +++ b/build.gradle @@ -14,7 +14,7 @@ buildscript { apply plugin: 'forge' apply plugin: 'scala' -version = "0.9.2" +version = "0.9.3" if (project.hasProperty('buildnum')) { project.version = project.version + '.' + project.buildnum diff --git a/src/net/bdew/lib/BdLib.scala b/src/net/bdew/lib/BdLib.scala index 0da6c7f..cf2fa26 100644 --- a/src/net/bdew/lib/BdLib.scala +++ b/src/net/bdew/lib/BdLib.scala @@ -1,3 +1,12 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + package net.bdew.lib import cpw.mods.fml.common.Mod diff --git a/src/net/bdew/lib/Misc.scala b/src/net/bdew/lib/Misc.scala index 9bee546..7cf2c40 100644 --- a/src/net/bdew/lib/Misc.scala +++ b/src/net/bdew/lib/Misc.scala @@ -21,6 +21,8 @@ object Misc { def iterNbtList[X](list: NBTTagList) = for (i <- 0 until list.tagCount()) yield list.tagAt(i).asInstanceOf[X] + def getActiveModId = Loader.instance().activeModContainer().getModId + def toLocal(s: String) = StatCollector.translateToLocal(s) def toLocalF(s: String, params: Any*) = StatCollector.translateToLocal(s).format(params: _*) @@ -44,7 +46,7 @@ object Misc { def min[T](vals: T*)(implicit o: Ordering[T]): T = vals.min(o) def max[T](vals: T*)(implicit o: Ordering[T]): T = vals.max(o) - def clamp[T](value: T, min: T, max: T)(implicit o: Ordering[T]): T = if (o.gt(value,max)) max else if (o.lt(value,min)) min else value + def clamp[T](value: T, min: T, max: T)(implicit o: Ordering[T]): T = if (o.gt(value, max)) max else if (o.lt(value, min)) min else value def iterSome[T](list: Seq[T], indexes: Iterable[Int]): Iterable[T] = for (i <- indexes) yield list(i) def iterSomeEnum[T](list: Seq[T], indexes: Iterable[Int]): Iterable[(Int, T)] = for (i <- indexes) yield i -> list(i) diff --git a/src/net/bdew/lib/config/BlockManager.scala b/src/net/bdew/lib/config/BlockManager.scala new file mode 100644 index 0000000..bccb1ac --- /dev/null +++ b/src/net/bdew/lib/config/BlockManager.scala @@ -0,0 +1,37 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.config + +import net.minecraft.item.ItemStack +import net.minecraft.block.Block +import cpw.mods.fml.common.registry.GameRegistry +import net.bdew.lib.block.HasTE + +class BlockManager(val ids: IdManager) { + + def regBlockCls[T <: Block](blockCls: Class[T], name: String, addStack: Boolean = true): T = { + val block = blockCls.getConstructor(classOf[Int]).newInstance(ids.getBlockId(name): Integer) + regBlock[T](block, name, addStack) + } + + def regBlock[T <: Block](block: T, name: String, addStack: Boolean = true): T = { + GameRegistry.registerBlock(block, name) + + if (addStack) + GameRegistry.registerCustomItemStack(name, new ItemStack(block)) + + if (block.isInstanceOf[HasTE[_]]) + GameRegistry.registerTileEntity(block.asInstanceOf[HasTE[_]].TEClass, name) + + return block + } + + def load() {} +} diff --git a/src/net/bdew/lib/config/FluidManager.scala b/src/net/bdew/lib/config/FluidManager.scala new file mode 100644 index 0000000..e2fc382 --- /dev/null +++ b/src/net/bdew/lib/config/FluidManager.scala @@ -0,0 +1,20 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.config + +import net.minecraftforge.fluids.{FluidRegistry, Fluid} + +class FluidManager(val ids: IdManager) { + def regFluid[T <: Fluid](fluid: T): T = { + FluidRegistry.registerFluid(fluid) + return fluid + } + def load() {} +} diff --git a/src/net/bdew/lib/config/IdManager.scala b/src/net/bdew/lib/config/IdManager.scala new file mode 100644 index 0000000..8869fa1 --- /dev/null +++ b/src/net/bdew/lib/config/IdManager.scala @@ -0,0 +1,46 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.config + +import net.minecraft.block.Block +import net.minecraft.item.Item +import net.minecraftforge.common.Configuration + +class IdManager(cfg: Configuration, itemStart: Int, blockStart: Int) { + + class MyIter(start: Int, check: Array[_], offset: Int = 0) { + var now = start + def next(): Int = { + while (check(now + offset) != null) now += 1 + return now + } + } + + def getBlockId(name: String, comment: String) = cfg.getBlock(name, blockIds.next(), comment).getInt + def getBlockId(name: String) = cfg.getBlock(name, blockIds.next()).getInt + + def getItemId(name: String, comment: String) = cfg.getItem(name, itemIds.next(), comment).getInt + def getItemId(name: String) = cfg.getItem(name, itemIds.next()).getInt + + val blockIds = new MyIter(blockStart, Block.blocksList) + val itemIds = new MyIter(itemStart, Item.itemsList, 256) + + // figure out what's the max id we already use, because forge is stupid + // ignore ids in the top slots as they were probably assigned to resolve conflicts + + import scala.collection.JavaConverters._ + + val maxBlock = (cfg.getCategory(Configuration.CATEGORY_BLOCK).values().asScala.map(_.getInt).filter(_ < Block.blocksList.length - 100) ++ Seq(0)).max + 1 + val maxItem = (cfg.getCategory(Configuration.CATEGORY_ITEM).values().asScala.map(_.getInt).filter(_ < Item.itemsList.length - 1000) ++ Seq(0)).max + 1 + + if (maxBlock > blockIds.now) blockIds.now = maxBlock + if (maxItem > itemIds.now) itemIds.now = maxItem +} + diff --git a/src/net/bdew/lib/config/ItemManager.scala b/src/net/bdew/lib/config/ItemManager.scala new file mode 100644 index 0000000..56ab458 --- /dev/null +++ b/src/net/bdew/lib/config/ItemManager.scala @@ -0,0 +1,33 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.config + +import net.minecraft.item.{ItemStack, Item} +import cpw.mods.fml.common.registry.GameRegistry +import net.bdew.lib.items.SimpleItem + +class ItemManager(val ids: IdManager) { + def regSimpleItem(name: String): SimpleItem = regItem(new SimpleItem(ids.getItemId(name), name), name) + + def regItem[T <: SimpleItem](item: T): T = regItem[T](item, item.name) + + def regItemCls[T <: Item](itemCls: Class[T], name: String, addStack: Boolean = true): T = { + val item = itemCls.getConstructor(classOf[Int]).newInstance(ids.getItemId(name): Integer) + regItem[T](item, name, addStack) + } + + def regItem[T <: Item](item: T, name: String, addStack: Boolean = true): T = { + GameRegistry.registerItem(item, name) + if (addStack) + GameRegistry.registerCustomItemStack(name, new ItemStack(item)) + return item + } + def load() {} +} diff --git a/src/net/bdew/lib/config/MachineManager.scala b/src/net/bdew/lib/config/MachineManager.scala new file mode 100644 index 0000000..7205d54 --- /dev/null +++ b/src/net/bdew/lib/config/MachineManager.scala @@ -0,0 +1,23 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.config + +import net.bdew.lib.recipes.gencfg.ConfigSection +import net.bdew.lib.machine.Machine + +class MachineManager(ids: IdManager, val tuning: ConfigSection) { + def registerMachine[R <: Machine[_]](machine: R): R = { + machine.tuning = tuning.getSection(machine.name) + if (machine.tuning.getBoolean("Enabled")) + machine.regBlock(ids) + return machine + } + def load() {} +} diff --git a/src/net/bdew/lib/gui/widgets/WidgetFluidGauge.scala b/src/net/bdew/lib/gui/widgets/WidgetFluidGauge.scala index b51dd91..e785e21 100644 --- a/src/net/bdew/lib/gui/widgets/WidgetFluidGauge.scala +++ b/src/net/bdew/lib/gui/widgets/WidgetFluidGauge.scala @@ -25,7 +25,7 @@ class WidgetFluidGauge(val rect: Rect, overlay: TextureLocation, dslot: DataSlot tip += dslot.getFluid.getFluid.getLocalizedName tip += "%s/%s mB".format(formater.format(dslot.getFluidAmount), formater.format(dslot.getCapacity)) } else { - tip += Misc.toLocal("gendustry.label.empty") + tip += Misc.toLocal("bdlib.label.empty") } } diff --git a/src/net/bdew/lib/items/SimpleItem.scala b/src/net/bdew/lib/items/SimpleItem.scala new file mode 100644 index 0000000..c874d89 --- /dev/null +++ b/src/net/bdew/lib/items/SimpleItem.scala @@ -0,0 +1,25 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.items + +import net.minecraft.item.Item +import net.minecraft.client.renderer.texture.IconRegister +import cpw.mods.fml.relauncher.{Side, SideOnly} +import net.bdew.lib.Misc + +class SimpleItem(id: Int, val name: String) extends Item(id) { + val modId = Misc.getActiveModId + setUnlocalizedName(modId + "." + name) + + @SideOnly(Side.CLIENT) + override def registerIcons(reg: IconRegister) { + itemIcon = reg.registerIcon(modId + ":" + name.toLowerCase) + } +} diff --git a/src/net/bdew/lib/machine/Machine.scala b/src/net/bdew/lib/machine/Machine.scala new file mode 100644 index 0000000..3a0f4ee --- /dev/null +++ b/src/net/bdew/lib/machine/Machine.scala @@ -0,0 +1,34 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.machine + +import net.bdew.lib.recipes.gencfg.ConfigSection +import net.minecraft.block.Block +import cpw.mods.fml.common.registry.GameRegistry +import net.minecraft.item.ItemStack +import net.bdew.lib.block.HasTE +import net.bdew.lib.config.IdManager +import net.bdew.lib.Misc + +abstract class Machine[T <: Block](val name: String, val blockConstruct: (Int) => T) { + var block: T = null.asInstanceOf[T] + var tuning: ConfigSection = null + val modId = Misc.getActiveModId + + def regBlock(ids: IdManager) { + block = blockConstruct(ids.getBlockId(name)) + GameRegistry.registerBlock(block, name) + GameRegistry.registerCustomItemStack(name, new ItemStack(block)) + if (block.isInstanceOf[HasTE[_]]) { + GameRegistry.registerTileEntity(block.asInstanceOf[HasTE[_]].TEClass, "%s.%s".format(modId, name)) + } + + } +} diff --git a/src/net/bdew/lib/machine/PoweredMachine.scala b/src/net/bdew/lib/machine/PoweredMachine.scala new file mode 100644 index 0000000..8dfa2cd --- /dev/null +++ b/src/net/bdew/lib/machine/PoweredMachine.scala @@ -0,0 +1,19 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.machine + +import net.bdew.lib.recipes.gencfg.ConfigSection + +trait PoweredMachine { + def tuning: ConfigSection + lazy val maxReceivedEnergy = tuning.getFloat("MaxReceivedEnergy") + lazy val activationEnergy = tuning.getFloat("ActivationEnergy") + lazy val maxStoredEnergy = tuning.getFloat("MaxStoredEnergy") +} diff --git a/src/net/bdew/lib/machine/ProcessorMachine.scala b/src/net/bdew/lib/machine/ProcessorMachine.scala new file mode 100644 index 0000000..2d2a16d --- /dev/null +++ b/src/net/bdew/lib/machine/ProcessorMachine.scala @@ -0,0 +1,15 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.machine + +trait ProcessorMachine extends PoweredMachine { + lazy val mjPerItem = tuning.getFloat("MjPerItem") + lazy val powerUseRate = tuning.getFloat("PowerUseRate") +} diff --git a/src/net/bdew/lib/power/DataSlotPower.scala b/src/net/bdew/lib/power/DataSlotPower.scala new file mode 100644 index 0000000..bea74b5 --- /dev/null +++ b/src/net/bdew/lib/power/DataSlotPower.scala @@ -0,0 +1,65 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.power + +import net.minecraft.nbt.NBTTagCompound +import net.bdew.lib.data.base.{TileDataSlots, UpdateKind, DataSlot} +import net.bdew.lib.Misc +import net.bdew.lib.machine.PoweredMachine + +case class DataSlotPower(name: String, parent: TileDataSlots) extends DataSlot { + updateKind = Set(UpdateKind.GUI, UpdateKind.SAVE) + + var stored = 0F + var capacity = 0F + var maxReceive = 0F + + def inject(v: Float, simulate: Boolean): Float = { + val canAdd = Misc.clamp(v, 0F, Misc.min(capacity - stored, maxReceive)) + if ((canAdd > 0) && !simulate) { + stored += canAdd + parent.dataSlotChanged(this) + } + return canAdd + } + + def extract(v: Float, simulate: Boolean): Float = { + val canExtract = Misc.clamp(v, 0F, stored) + if ((canExtract > 0) && !simulate) { + stored -= canExtract + parent.dataSlotChanged(this) + } + return canExtract + } + + def configure(cfg: PoweredMachine) { + capacity = cfg.maxStoredEnergy + maxReceive = cfg.maxReceivedEnergy + } + + def save(t: NBTTagCompound, kind: UpdateKind.Value) { + val tag = new NBTTagCompound() + tag.setFloat("stored", stored) + if (kind == UpdateKind.GUI) + tag.setFloat("capacity", capacity) + t.setCompoundTag(name, tag) + } + + def load(t: NBTTagCompound, kind: UpdateKind.Value) { + if (t.hasKey(name)) { + val tag = t.getCompoundTag(name) + stored = tag.getFloat("stored") + if (kind == UpdateKind.GUI) + capacity = tag.getFloat("capacity") + } + } +} + + diff --git a/src/net/bdew/lib/power/ItemPoweredBase.scala b/src/net/bdew/lib/power/ItemPoweredBase.scala new file mode 100644 index 0000000..a02d4b4 --- /dev/null +++ b/src/net/bdew/lib/power/ItemPoweredBase.scala @@ -0,0 +1,49 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.power + +import net.minecraft.item.{ItemStack, Item} +import net.bdew.lib.Misc +import net.minecraft.entity.EntityLivingBase +import net.minecraft.nbt.NBTTagCompound + +trait ItemPoweredBase extends Item { + def maxCharge: Int + def mjPerCharge: Int + + def getCharge(stack: ItemStack): Int = { + if (!stack.hasTagCompound) setCharge(stack, 0) + return Misc.clamp(stack.getTagCompound.getInteger("charge"), 0, maxCharge) + } + + def useCharge(stack: ItemStack, uses: Int = 1, player: EntityLivingBase) { + setCharge(stack, Misc.clamp(getCharge(stack) - uses * mjPerCharge, 0, maxCharge)) + } + + def hasCharges(stack: ItemStack) = getCharge(stack) >= mjPerCharge + + def setCharge(stack: ItemStack, charge: Int) { + if (!stack.hasTagCompound) stack.setTagCompound(new NBTTagCompound()) + stack.getTagCompound.setInteger("charge", charge) + updateDamage(stack) + } + + def updateDamage(stack: ItemStack) { + setDamage(stack, Misc.clamp((100 * (1 - getCharge(stack).toFloat / maxCharge)).round, 1, 100)) + } + + def stackWithCharge(charge: Int): ItemStack = { + val n = new ItemStack(this) + setCharge(n, charge) + return n + } + + override def setDamage(stack: ItemStack, damage: Int) = super.setDamage(stack, Misc.clamp(damage, 1, 100)) +} diff --git a/src/net/bdew/lib/power/TileBaseProcessor.scala b/src/net/bdew/lib/power/TileBaseProcessor.scala new file mode 100644 index 0000000..189f834 --- /dev/null +++ b/src/net/bdew/lib/power/TileBaseProcessor.scala @@ -0,0 +1,67 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.power + +import net.bdew.lib.tile.TileExtended +import net.bdew.lib.data.base.{UpdateKind, TileDataSlots} +import net.bdew.lib.tile.inventory.{BreakableInventoryTile, SidedInventory, PersistentInventoryTile} +import net.bdew.lib.data.DataSlotFloat +import net.bdew.lib.machine.ProcessorMachine + +abstract class TileBaseProcessor extends TileExtended +with TileDataSlots +with PersistentInventoryTile +with BreakableInventoryTile +with SidedInventory +with TilePoweredBase { + def cfg: ProcessorMachine + val power = DataSlotPower("power", this) + val progress = DataSlotFloat("progress", this).setUpdate(UpdateKind.SAVE, UpdateKind.GUI) + + configurePower(cfg) + + override def tickServer() { + if (power.stored > cfg.activationEnergy) { + + if (!isWorking) + if (tryStart()) + progress := 0 + + if (isWorking) { + + if ((progress < 1) && (power.stored > cfg.activationEnergy)) { + val maxConsume = Math.min(Math.max(cfg.powerUseRate * power.stored, cfg.activationEnergy), cfg.mjPerItem * (1 - progress)) + val consumed = power.extract(maxConsume, false) + progress += consumed / cfg.mjPerItem + } + + if (progress >= 1) { + if (tryFinish()) + progress := 0 + } + } + } + } + + /** + * Return true when an operation is in progress + */ + def isWorking: Boolean + + /** + * Try starting a new operation, return true if succesful + */ + def tryStart(): Boolean + + /** + * Perform output when operation is done, return true if succesful + */ + def tryFinish(): Boolean +} diff --git a/src/net/bdew/lib/power/TileItemProcessor.scala b/src/net/bdew/lib/power/TileItemProcessor.scala new file mode 100644 index 0000000..0675e8c --- /dev/null +++ b/src/net/bdew/lib/power/TileItemProcessor.scala @@ -0,0 +1,26 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.power + +import net.bdew.lib.data.DataSlotItemStack +import net.bdew.lib.data.base.UpdateKind +import net.bdew.lib.items.ItemUtils + +abstract class TileItemProcessor extends TileBaseProcessor { + val output = DataSlotItemStack("output", this).setUpdate(UpdateKind.SAVE) + val outputSlots: Seq[Int] + + def isWorking = output :!= null + + def tryFinish(): Boolean = { + output := ItemUtils.addStackToSlots(output, this, outputSlots, false) + return output :== null + } +} diff --git a/src/net/bdew/lib/power/TilePoweredBase.scala b/src/net/bdew/lib/power/TilePoweredBase.scala new file mode 100644 index 0000000..d309648 --- /dev/null +++ b/src/net/bdew/lib/power/TilePoweredBase.scala @@ -0,0 +1,18 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.power + +import net.bdew.lib.tile.TileExtended +import net.bdew.lib.machine.PoweredMachine + +trait TilePoweredBase extends TileExtended { + def power: DataSlotPower + def configurePower(cfg: PoweredMachine) = power.configure(cfg) +} diff --git a/src/net/bdew/lib/power/WidgetPowerGauge.scala b/src/net/bdew/lib/power/WidgetPowerGauge.scala new file mode 100644 index 0000000..5384368 --- /dev/null +++ b/src/net/bdew/lib/power/WidgetPowerGauge.scala @@ -0,0 +1,28 @@ +/* + * Copyright (c) bdew, 2013 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt + */ + +package net.bdew.lib.power + +import net.bdew.lib.gui.{Point, TextureLocation, Rect} +import scala.collection.mutable +import java.text.DecimalFormat +import net.minecraft.client.Minecraft +import net.bdew.lib.gui.widgets.Widget + +class WidgetPowerGauge(val rect: Rect, texture: TextureLocation, dslot: DataSlotPower) extends Widget { + val formater = new DecimalFormat("#,###") + + override def draw() { + Minecraft.getMinecraft.renderEngine.bindTexture(texture.resource) + val fill = (dslot.stored / dslot.capacity * rect.h).round + parent.drawTexturedModalRect(rect.x, rect.y + rect.h - fill, texture.x, texture.y + rect.h - fill, rect.w, fill) + } + + override def handleTooltip(p: Point, tip: mutable.MutableList[String]) = tip += formater.format(dslot.stored) + "/" + formater.format(dslot.capacity) + " MJ" +} diff --git a/src/net/bdew/lib/recipes/gencfg/ConfigSection.scala b/src/net/bdew/lib/recipes/gencfg/ConfigSection.scala index 0d2a8c1..76438b5 100644 --- a/src/net/bdew/lib/recipes/gencfg/ConfigSection.scala +++ b/src/net/bdew/lib/recipes/gencfg/ConfigSection.scala @@ -16,10 +16,10 @@ case class EntryDouble(v: Double) extends CfgEntry case class EntryStr(v: String) extends CfgEntry case class ConfigSection(pfx: String = "") extends CfgEntry with Iterable[(String, CfgEntry)] { - var raw = Map.empty[String, CfgEntry].withDefault(x => sys.error("Config value '%s%s' is missing".format(pfx,x))) + var raw = Map.empty[String, CfgEntry].withDefault(x => sys.error("Config value '%s%s' is missing".format(pfx, x))) def iterator: Iterator[(String, CfgEntry)] = raw.iterator - def filterType[T<:CfgEntry](cls: Class[T]): Iterable[(String,T)] = raw.filter(x=>cls.isInstance(x._2)).map(x=>x.asInstanceOf[(String,T)]) + def filterType[T <: CfgEntry](cls: Class[T]): Iterable[(String, T)] = raw.filter(x => cls.isInstance(x._2)).map(x => x.asInstanceOf[(String, T)]) def rawget[T](id: String, t: Class[T]): T = { val v = raw(id) @@ -35,7 +35,6 @@ case class ConfigSection(pfx: String = "") extends CfgEntry with Iterable[(Strin def getString(id: String) = rawget(id, classOf[EntryStr]).v def getSection(id: String) = rawget(id, classOf[ConfigSection]) - final val trueVals = Set("y", "true", "yes", "on") def getBoolean(id: String) = trueVals.contains(getString(id).toLowerCase)