mirror of
https://github.com/GTNewHorizons/bdlib.git
synced 2026-11-20 12:19:40 +00:00
Added resource system
This commit is contained in:
1 parent
9c977904ab
commit
610be18c74
5 files changed
+339
No files matched your search
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* http://bdew.net/minecraft-mod-public-license/
|
||||
*/
|
||||
|
||||
package net.bdew.lib.resource
|
||||
|
||||
import net.bdew.lib.Misc
|
||||
import net.bdew.lib.data.base.{DataSlot, TileDataSlots, UpdateKind}
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import net.minecraftforge.fluids.{FluidStack, FluidTankInfo}
|
||||
|
||||
class DataSlotResource(val name: String, val parent: TileDataSlots, initCapacity: Int) extends DataSlot {
|
||||
var resource: Option[Resource] = None
|
||||
var amount = 0D
|
||||
var capacity = initCapacity
|
||||
val sendCapacityOnUpdateKind = Set(UpdateKind.GUI)
|
||||
|
||||
setUpdate(UpdateKind.SAVE, UpdateKind.GUI)
|
||||
|
||||
def getEffectiveCapacity = resource.map(_.kind.capacityMultiplier).getOrElse(0D) * capacity
|
||||
|
||||
/**
|
||||
* Low level fill method that operates on Resource's
|
||||
* @param res Resource to fill
|
||||
* @param onlyRound Only fill integer amounts
|
||||
* @param doFill If false, fill will only be simulated
|
||||
* @return how much was or would be filled
|
||||
*/
|
||||
def rawFill(res: Resource, onlyRound: Boolean, doFill: Boolean) = {
|
||||
if (resource.isEmpty || resource.get.kind == res.kind) {
|
||||
val current = if (resource.isEmpty) 0D else resource.get.amount
|
||||
var canFill = Misc.clamp(res.amount, 0D, capacity * res.kind.capacityMultiplier - current)
|
||||
if (onlyRound) canFill = canFill.floor
|
||||
if (doFill) execWithChangeNotify {
|
||||
resource = Some(Resource(res.kind, current + canFill))
|
||||
}
|
||||
canFill
|
||||
} else 0D
|
||||
}
|
||||
|
||||
/**
|
||||
* Low level drain method that operates on Resource's
|
||||
* @param maxDrain maximum amount to drain
|
||||
* @param onlyRound Only fill integer amounts
|
||||
* @param doDrain If false, drain will only be simulated
|
||||
* @return how much was or would be drained
|
||||
*/
|
||||
def rawDrain(maxDrain: Double, onlyRound: Boolean, doDrain: Boolean): Option[Resource] = {
|
||||
resource map { res =>
|
||||
var canDrain = Misc.clamp(res.amount, 0D, maxDrain)
|
||||
if (onlyRound) canDrain = canDrain.floor
|
||||
if (doDrain) execWithChangeNotify {
|
||||
resource =
|
||||
if (canDrain == res.amount)
|
||||
None
|
||||
else
|
||||
Some(Resource(res.kind, res.amount - canDrain))
|
||||
}
|
||||
Resource(res.kind, canDrain)
|
||||
}
|
||||
}
|
||||
|
||||
def fillFluid(resource: FluidStack, doFill: Boolean): Int = rawFill(Resource.from(resource), onlyRound = true, doFill).toInt
|
||||
def drainFluid(maxDrain: Int, doDrain: Boolean): FluidStack = {
|
||||
(for {
|
||||
res <- rawDrain(maxDrain, true, doDrain)
|
||||
rKind <- Misc.asInstanceOpt(res.kind, classOf[FluidResource])
|
||||
} yield {
|
||||
if (doDrain)
|
||||
rawDrain(res.amount, false, true)
|
||||
new FluidStack(rKind.fluid, res.amount.toInt)
|
||||
}).orNull
|
||||
}
|
||||
def getTankInfo = {
|
||||
(for {
|
||||
res <- resource
|
||||
rKind <- Misc.asInstanceOpt(res.kind, classOf[FluidResource])
|
||||
} yield {
|
||||
new FluidTankInfo(new FluidStack(rKind.fluid, res.amount.toInt), getEffectiveCapacity.toInt)
|
||||
}).getOrElse(new FluidTankInfo(null, capacity))
|
||||
}
|
||||
|
||||
override def load(t: NBTTagCompound, kind: UpdateKind.Value) {
|
||||
val tag = t.getCompoundTag(name)
|
||||
|
||||
resource = if (tag.hasKey("resource"))
|
||||
ResourceManager.loadFromNBT(tag.getCompoundTag("resource"))
|
||||
else
|
||||
None
|
||||
|
||||
|
||||
if (sendCapacityOnUpdateKind.contains(kind))
|
||||
tag.getInteger("capacity")
|
||||
}
|
||||
|
||||
override def save(t: NBTTagCompound, kind: UpdateKind.Value) {
|
||||
val tag = new NBTTagCompound()
|
||||
|
||||
resource map { res => tag.setTag("resource", ResourceManager.saveToNBT(res))}
|
||||
|
||||
if (sendCapacityOnUpdateKind.contains(kind))
|
||||
tag.setInteger("capacity", capacity)
|
||||
|
||||
t.setTag(name, tag)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* http://bdew.net/minecraft-mod-public-license/
|
||||
*/
|
||||
|
||||
package net.bdew.lib.resource
|
||||
|
||||
import net.bdew.lib.gui.{Color, IconWrapper, Texture}
|
||||
import net.bdew.lib.{DecFormat, Misc}
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import net.minecraftforge.fluids.{Fluid, FluidRegistry, FluidStack}
|
||||
|
||||
case class FluidResource(fluid: Fluid) extends ResourceKind {
|
||||
override def getTexture = new IconWrapper(Texture.BLOCKS, fluid.getIcon)
|
||||
override def getColor = Color.fromInt(fluid.getColor)
|
||||
override def getLocalizedName = fluid.getLocalizedName(new FluidStack(fluid, 1))
|
||||
override def getUnlocalizedName = fluid.getUnlocalizedName
|
||||
override def getFormattedString(amount: Double, capacity: Double) =
|
||||
Misc.toLocalF("resource.fluid.format", DecFormat.round(amount), DecFormat.round(capacity))
|
||||
override def capacityMultiplier = 1
|
||||
override def toString = "FluidResource(%s)".format(getUnlocalizedName)
|
||||
override def helperObject = FluidResourceHelper
|
||||
}
|
||||
|
||||
object FluidResourceHelper extends ResourceHelper[FluidResource]("fluid") {
|
||||
override def loadFromNBT(tag: NBTTagCompound) = {
|
||||
if (tag.hasKey("fluid")) {
|
||||
val fName = tag.getString("fluid")
|
||||
Option(FluidRegistry.getFluid(fName)) map FluidResource
|
||||
} else None
|
||||
}
|
||||
override def saveToNBT(tag: NBTTagCompound, r: FluidResource) {
|
||||
tag.setString("fluid", r.fluid.getName)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* http://bdew.net/minecraft-mod-public-license/
|
||||
*/
|
||||
|
||||
package net.bdew.lib.resource
|
||||
|
||||
import net.bdew.lib.gui.{Color, IconWrapper, Texture}
|
||||
import net.bdew.lib.{DecFormat, Misc}
|
||||
import net.minecraft.item.{Item, ItemStack}
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
|
||||
case class ItemResource(item: Item, meta: Int) extends ResourceKind {
|
||||
override def getTexture = new IconWrapper(
|
||||
if (item.getSpriteNumber == 0) Texture.BLOCKS else Texture.ITEMS,
|
||||
item.getIconFromDamageForRenderPass(meta, 0))
|
||||
|
||||
override def getColor = Color.fromInt(item.getColorFromItemStack(makeStack(1), 0))
|
||||
|
||||
override def getLocalizedName = item.getItemStackDisplayName(makeStack(1))
|
||||
override def getUnlocalizedName = item.getUnlocalizedName(makeStack(1))
|
||||
|
||||
override def getFormattedString(amount: Double, capacity: Double) =
|
||||
Misc.toLocalF("resource.item.format", DecFormat.dec2(amount), DecFormat.round(capacity))
|
||||
|
||||
override def capacityMultiplier = 1 / 250D
|
||||
|
||||
def makeStack(n: Int) = new ItemStack(item, n, meta)
|
||||
|
||||
override def toString = "ItemResource(%s,%d)".format(getUnlocalizedName, meta)
|
||||
override def helperObject = ItemResourceHelper
|
||||
}
|
||||
|
||||
object ItemResourceHelper extends ResourceHelper[ItemResource]("item") {
|
||||
override def loadFromNBT(tag: NBTTagCompound) = {
|
||||
val item = Option(Item.getItemById(tag.getInteger("id")))
|
||||
val meta = tag.getInteger("meta")
|
||||
item.map(ItemResource(_, meta))
|
||||
}
|
||||
override def saveToNBT(tag: NBTTagCompound, r: ItemResource) {
|
||||
tag.setInteger("id", Item.getIdFromItem(r.item))
|
||||
tag.setInteger("meta", r.meta)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* http://bdew.net/minecraft-mod-public-license/
|
||||
*/
|
||||
|
||||
package net.bdew.lib.resource
|
||||
|
||||
import net.bdew.lib.Misc
|
||||
import net.bdew.lib.gui.{Color, Texture}
|
||||
import net.minecraft.item.ItemStack
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import net.minecraftforge.fluids.FluidStack
|
||||
|
||||
trait ResourceKind {
|
||||
def getTexture: Texture
|
||||
def getColor: Color
|
||||
def getLocalizedName: String
|
||||
def getUnlocalizedName: String
|
||||
def getFormattedString(amount: Double, capacity: Double): String
|
||||
def capacityMultiplier: Double
|
||||
def helperObject: ResourceHelper[_ >: this.type]
|
||||
// Type bound is required because some co(ntra)variance bullshit that i don't fully understand
|
||||
}
|
||||
|
||||
abstract class ResourceHelper[T <: ResourceKind](val id: String) {
|
||||
def loadFromNBT(tag: NBTTagCompound): Option[T]
|
||||
def saveToNBT(tag: NBTTagCompound, resource: T): Unit
|
||||
}
|
||||
|
||||
object ResourceManager {
|
||||
var resourceHelpers = Map.empty[String, ResourceHelper[_ <: ResourceKind]]
|
||||
def register[T <: ResourceKind](helper: ResourceHelper[T]) = resourceHelpers += helper.id -> helper
|
||||
|
||||
register(FluidResourceHelper)
|
||||
register(ItemResourceHelper)
|
||||
|
||||
def loadFromNBT(tag: NBTTagCompound): Option[Resource] = {
|
||||
val rKind = if (tag.hasKey("kind")) {
|
||||
resourceHelpers.get(tag.getString("kind")).flatMap(_.loadFromNBT(tag))
|
||||
} else None
|
||||
rKind.map(Resource(_, tag.getDouble("amount")))
|
||||
}
|
||||
|
||||
def saveToNBT(r: Resource) = {
|
||||
Misc.applyMutator(new NBTTagCompound) { tag =>
|
||||
r.kind.helperObject.saveToNBT(tag, r.kind)
|
||||
tag.setDouble("amount", r.amount)
|
||||
tag.setString("kind", r.kind.helperObject.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case class Resource(kind: ResourceKind, amount: Double)
|
||||
|
||||
object Resource {
|
||||
def from(fs: FluidStack) = Resource(FluidResource(fs.getFluid), fs.amount)
|
||||
def from(is: ItemStack) = Resource(ItemResource(is.getItem, is.getItemDamage), is.stackSize)
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) bdew, 2013 - 2014
|
||||
* 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
|
||||
* http://bdew.net/minecraft-mod-public-license/
|
||||
*/
|
||||
|
||||
package net.bdew.lib.resource
|
||||
|
||||
import net.bdew.lib.Misc
|
||||
import net.minecraft.entity.player.EntityPlayer
|
||||
import net.minecraft.inventory.IInventory
|
||||
import net.minecraft.item.ItemStack
|
||||
|
||||
case class ResourceInventoryOutput(res: DataSlotResource) extends IInventory {
|
||||
override def getSizeInventory = 1
|
||||
override def getInventoryStackLimit = 64
|
||||
|
||||
def getResource =
|
||||
res.resource match {
|
||||
case Some(Resource(kind: ItemResource, amount)) => Some((kind, amount))
|
||||
case _ => None
|
||||
}
|
||||
|
||||
override def getStackInSlot(slot: Int) =
|
||||
if (slot == 0) {
|
||||
(getResource map { case (kind, amount) =>
|
||||
val amt = Misc.clamp(amount.floor.toInt, 0, kind.makeStack(1).getMaxStackSize)
|
||||
if (amt > 0)
|
||||
kind.makeStack(amt)
|
||||
else
|
||||
null
|
||||
}).orNull
|
||||
} else null
|
||||
|
||||
override def isItemValidForSlot(slot: Int, stack: ItemStack) = slot == 0
|
||||
|
||||
override def decrStackSize(slot: Int, amt: Int) = {
|
||||
if (slot == 0) {
|
||||
(for {
|
||||
(kind, stored) <- getResource
|
||||
drained <- res.rawDrain(amt, true, false)
|
||||
} yield {
|
||||
val stackSize = Misc.clamp(drained.amount.floor.toInt, 0, kind.makeStack(1).getMaxStackSize)
|
||||
if (stackSize > 0) {
|
||||
res.rawDrain(stackSize, true, true)
|
||||
kind.makeStack(stackSize)
|
||||
} else null
|
||||
}).orNull
|
||||
} else null
|
||||
}
|
||||
|
||||
override def setInventorySlotContents(slot: Int, stack: ItemStack) = {
|
||||
if (slot == 0) {
|
||||
if (stack == null) {
|
||||
decrStackSize(0, Int.MaxValue)
|
||||
} else {
|
||||
val stackRes = Resource.from(stack)
|
||||
for ((kind, stored) <- getResource if kind == stackRes.kind && stackRes.amount < stored) {
|
||||
res.rawDrain(stored - stackRes.amount, true, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override def getStackInSlotOnClosing(slot: Int) = getStackInSlot(slot)
|
||||
|
||||
override def openInventory() {}
|
||||
override def markDirty() {}
|
||||
|
||||
override def isUseableByPlayer(player: EntityPlayer) = true
|
||||
override def hasCustomInventoryName = false
|
||||
override def getInventoryName = ""
|
||||
override def closeInventory() {}
|
||||
}
|
||||
Reference in new issue
Block a user