Added multiple power system support to grafter, refactored base chargeable item class

This commit is contained in:
bdew committed 2013-12-11 11:02:38 +02:00
1 parent 906037683a
commit 22f58706ee
5 files changed
+159 -52

No files matched your search

@@ -95,7 +95,7 @@ cfg Machines {
cfg Items {
cfg IndustrialGrafter {
Charges = 200 // Number of blocks that a fully charged grafter can break
RfPerCharge = 5000 // Cost of a single broken block in RF (by default 10RF = 1MJ)
MjPerCharge = 500 // Cost of a single broken block in MJ
AOE = 2 // Radius from the central block, 1 = 3x3x3, 2 = 5x5x5 and so on
}
}
@@ -9,14 +9,12 @@
package net.bdew.gendustry.items
import cofh.api.energy.IEnergyContainerItem
import net.minecraft.item.{ItemStack, EnumToolMaterial, ItemTool}
import forestry.api.arboriculture.IToolGrafter
import net.minecraft.world.World
import net.minecraft.entity.player.EntityPlayer
import net.bdew.gendustry.Gendustry
import net.bdew.gendustry.config.Tuning
import net.minecraft.nbt.NBTTagCompound
import net.bdew.lib.Misc
import net.minecraft.block.Block
import net.minecraft.block.material.Material
@@ -27,12 +25,13 @@ import cpw.mods.fml.relauncher.{Side, SideOnly}
import net.minecraft.client.renderer.texture.IconRegister
import cpw.mods.fml.common.Optional
import net.bdew.gendustry.compat.PowerProxy
import net.bdew.gendustry.power.ItemPowered
@Optional.Interface(modid = PowerProxy.TE_MOD_ID, iface = "cofh.api.energy.IEnergyContainerItem")
class IndustrialGrafter(id: Int) extends ItemTool(id, 0, EnumToolMaterial.IRON, Array.empty[Block]) with IEnergyContainerItem with IToolGrafter {
val cfg = Tuning.getSection("Items").getSection("IndustrialGrafter")
val rfPerCharge = cfg.getInt("RfPerCharge")
val maxCharge = cfg.getInt("Charges") * rfPerCharge
@Optional.Interface(modid = PowerProxy.IC2_MOD_ID, iface = "ic2.api.item.ISpecialElectricItem")
class IndustrialGrafter(id: Int) extends ItemTool(id, 0, EnumToolMaterial.IRON, Array.empty[Block]) with ItemPowered with IToolGrafter {
lazy val cfg = Tuning.getSection("Items").getSection("IndustrialGrafter")
lazy val mjPerCharge = cfg.getInt("MjPerCharge")
lazy val maxCharge = cfg.getInt("Charges") * mjPerCharge
val aoe = cfg.getInt("AOE")
setUnlocalizedName(Gendustry.modId + ".grafter")
@@ -41,36 +40,9 @@ class IndustrialGrafter(id: Int) extends ItemTool(id, 0, EnumToolMaterial.IRON,
efficiencyOnProperMaterial = 32
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) {
setCharge(stack, Misc.clamp(getCharge(stack) - uses * rfPerCharge, 0, maxCharge))
}
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))
override def getStrVsBlock(stack: ItemStack, block: Block, meta: Int): Float = getStrVsBlock(stack, block)
override def getStrVsBlock(stack: ItemStack, block: Block): Float = {
if (getCharge(stack) < rfPerCharge) return 0.1F
if (!hasCharges(stack)) return 0.1F
if (block.blockMaterial == Material.leaves) return efficiencyOnProperMaterial
return 0.1F
}
@@ -83,13 +55,13 @@ class IndustrialGrafter(id: Int) extends ItemTool(id, 0, EnumToolMaterial.IRON,
dz <- -1 * aoe to aoe
if dy + y > 0 && dy + y < world.getHeight) {
val bl = Block.blocksList(world.getBlockId(x + dx, y + dy, z + dz))
if (bl != null && bl.blockMaterial == Material.leaves && getCharge(stack) > rfPerCharge) {
if (bl != null && bl.blockMaterial == Material.leaves && hasCharges(stack)) {
bl.removeBlockByPlayer(world, player.asInstanceOf[EntityPlayer], x + dx, y + dy, z + dz)
useCharge(stack)
useCharge(stack, 1, player)
}
}
}
useCharge(stack)
useCharge(stack, 1, player)
return true
}
return false
@@ -101,7 +73,7 @@ class IndustrialGrafter(id: Int) extends ItemTool(id, 0, EnumToolMaterial.IRON,
import scala.collection.JavaConverters._
val tip = l.asInstanceOf[util.List[String]].asScala
tip += Misc.toLocalF("gendustry.label.charges", getCharge(stack) / rfPerCharge)
tip += Misc.toLocalF("gendustry.label.charges", getCharge(stack) / mjPerCharge)
}
override def getSubItems(par1: Int, tabs: CreativeTabs, l: util.List[_]) {
@@ -115,18 +87,7 @@ class IndustrialGrafter(id: Int) extends ItemTool(id, 0, EnumToolMaterial.IRON,
override def getIsRepairable(par1ItemStack: ItemStack, par2ItemStack: ItemStack): Boolean = false
override def isBookEnchantable(itemstack1: ItemStack, itemstack2: ItemStack): Boolean = false
def receiveEnergy(container: ItemStack, maxReceive: Int, simulate: Boolean): Int = {
val charge = getCharge(container)
val canCharge = Misc.clamp(maxCharge - charge, 0, maxReceive)
if (!simulate) setCharge(container, charge + canCharge)
return canCharge
}
def extractEnergy(container: ItemStack, maxExtract: Int, simulate: Boolean): Int = 0
def getEnergyStored(container: ItemStack): Int = getCharge(container)
def getMaxEnergyStored(container: ItemStack): Int = maxCharge
def getSaplingModifier(stack: ItemStack, world: World, player: EntityPlayer, x: Int, y: Int, z: Int): Float = if (getCharge(stack) > rfPerCharge) 100 else 0
def getSaplingModifier(stack: ItemStack, world: World, player: EntityPlayer, x: Int, y: Int, z: Int): Float = if (hasCharges(stack)) 100 else 0
@SideOnly(Side.CLIENT)
override def registerIcons(reg: IconRegister) {
@@ -0,0 +1,51 @@
/*
* Copyright (c) bdew, 2013
* https://github.com/bdew/gendustry
*
* 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/gendustry/master/MMPL-1.0.txt
*/
package net.bdew.gendustry.power
import net.minecraft.item.{ItemStack, Item}
import net.bdew.lib.Misc
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.entity.EntityLivingBase
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))
}
trait ItemPowered extends ItemPoweredBase with ItemPoweredRF with ItemPoweredEU
@@ -0,0 +1,62 @@
/*
* Copyright (c) bdew, 2013
* https://github.com/bdew/gendustry
*
* 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/gendustry/master/MMPL-1.0.txt
*/
package net.bdew.gendustry.power
import ic2.api.item.{ElectricItem, IElectricItemManager, ISpecialElectricItem}
import net.minecraft.item.ItemStack
import net.bdew.gendustry.config.Tuning
import net.minecraft.entity.EntityLivingBase
import net.bdew.gendustry.compat.PowerProxy
import net.bdew.lib.Misc
import cpw.mods.fml.common.Optional
@Optional.Interface(modid = PowerProxy.IC2_MOD_ID, iface = "ic2.api.energy.tile.ISpecialElectricItem")
trait ItemPoweredEU extends ItemPoweredBase with ISpecialElectricItem {
private lazy val ratio = Tuning.getSection("Power").getFloat("EU_MJ_Ratio")
private lazy val manager = new ItemPoweredEUManager(this)
override def useCharge(stack: ItemStack, uses: Int, player: EntityLivingBase) = {
if (PowerProxy.haveIC2)
ElectricItem.rawManager.chargeFromArmor(stack, player)
super.useCharge(stack, uses, player)
}
def canProvideEnergy(itemStack: ItemStack) = false
def getChargedItemId(itemStack: ItemStack) = itemID
def getEmptyItemId(itemStack: ItemStack) = itemID
def getMaxCharge(itemStack: ItemStack) = (maxCharge * ratio).round
def getTier(itemStack: ItemStack) = 2
def getTransferLimit(itemStack: ItemStack) = 2048
def getManager(itemStack: ItemStack) = manager
}
class ItemPoweredEUManager(item: ItemPoweredEU) extends IElectricItemManager {
private lazy val ratio = Tuning.getSection("Power").getFloat("EU_MJ_Ratio")
def charge(itemStack: ItemStack, amount: Int, tier: Int, ignoreTransferLimit: Boolean, simulate: Boolean): Int = {
val charge = item.getCharge(itemStack)
val canCharge = Misc.clamp(item.maxCharge.toFloat - charge, 0F, amount / ratio).floor.toInt
if (!simulate) item.setCharge(itemStack, charge + canCharge)
return (canCharge * ratio).round
}
def discharge(itemStack: ItemStack, amount: Int, tier: Int, ignoreTransferLimit: Boolean, simulate: Boolean) = 0
def getCharge(itemStack: ItemStack) = (item.getCharge(itemStack) * ratio).round.toInt
def use(itemStack: ItemStack, amount: Int, entity: EntityLivingBase) =
ElectricItem.rawManager.use(itemStack, amount, entity)
def canUse(itemStack: ItemStack, amount: Int) =
ElectricItem.rawManager.canUse(itemStack, amount)
def chargeFromArmor(itemStack: ItemStack, entity: EntityLivingBase) =
ElectricItem.rawManager.chargeFromArmor(itemStack, entity)
def getToolTip(itemStack: ItemStack) =
ElectricItem.rawManager.getToolTip(itemStack)
}
@@ -0,0 +1,33 @@
/*
* Copyright (c) bdew, 2013
* https://github.com/bdew/gendustry
*
* 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/gendustry/master/MMPL-1.0.txt
*/
package net.bdew.gendustry.power
import cpw.mods.fml.common.Optional
import net.bdew.gendustry.compat.PowerProxy
import cofh.api.energy.IEnergyContainerItem
import net.minecraft.item.ItemStack
import net.bdew.lib.Misc
import net.bdew.gendustry.config.Tuning
@Optional.Interface(modid = PowerProxy.TE_MOD_ID, iface = "cofh.api.energy.IEnergyContainerItem")
trait ItemPoweredRF extends ItemPoweredBase with IEnergyContainerItem {
private lazy val ratio = Tuning.getSection("Power").getFloat("RF_MJ_Ratio")
def receiveEnergy(container: ItemStack, maxReceive: Int, simulate: Boolean): Int = {
val charge = getCharge(container)
val canCharge = Misc.clamp(maxCharge.toFloat - charge, 0F, maxReceive.toFloat / ratio).floor.toInt
if (!simulate) setCharge(container, charge + canCharge)
return (canCharge * ratio).round
}
def extractEnergy(container: ItemStack, maxExtract: Int, simulate: Boolean): Int = 0
def getEnergyStored(container: ItemStack): Int = (getCharge(container) * ratio).round
def getMaxEnergyStored(container: ItemStack): Int = (maxCharge * ratio).round
}