Moving dataslot classes to bdlib

This commit is contained in:
bdew committed 2015-02-19 12:04:13 +02:00
1 parent 3431a6910e
commit eb75a26c40
3 files changed
+133

No files matched your search

@@ -0,0 +1,31 @@
/*
* Copyright (c) bdew, 2013 - 2015
* 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.data
import net.bdew.lib.data.base.{TileDataSlots, UpdateKind}
import net.minecraft.nbt.NBTTagCompound
import net.minecraftforge.fluids.{Fluid, FluidRegistry}
case class DataSlotFluidOption(name: String, parent: TileDataSlots) extends DataSlotOption[Fluid] {
setUpdate(UpdateKind.SAVE, UpdateKind.GUI)
override def save(t: NBTTagCompound, kind: UpdateKind.Value) {
value map { x => t.setString(name, x.getName) }
}
override def load(t: NBTTagCompound, kind: UpdateKind.Value): Unit = {
if (t.hasKey(name)) {
val fluidName = t.getString(name)
if (FluidRegistry.isFluidRegistered(fluidName)) {
value = Option(FluidRegistry.getFluid(fluidName))
} else unset()
} else unset()
}
}
@@ -0,0 +1,63 @@
/*
* Copyright (c) bdew, 2013 - 2015
* 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.data
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, DataInputStream, DataOutputStream}
import net.bdew.lib.data.base.{DataSlot, TileDataSlots, UpdateKind}
import net.minecraft.nbt.NBTTagCompound
import scala.collection.mutable
case class DataSlotMovingAverage(name: String, parent: TileDataSlots, size: Int) extends DataSlot {
val values = mutable.Queue.empty[Double]
var average: Double = 0
setUpdate(UpdateKind.GUI, UpdateKind.SAVE)
def reset() {
values.clear()
average = 0
}
def update(v: Double) = {
values += v
if (values.length > size)
values.dequeue()
average = values.sum / values.length
parent.dataSlotChanged(this)
}
override def save(t: NBTTagCompound, kind: UpdateKind.Value) {
if (kind == UpdateKind.GUI) {
t.setDouble(name, average)
} else {
val b = new ByteArrayOutputStream()
val o = new DataOutputStream(b)
o.writeByte(values.size)
values.foreach(o.writeDouble)
t.setByteArray(name, b.toByteArray)
o.close()
}
}
override def load(t: NBTTagCompound, kind: UpdateKind.Value) {
if (kind == UpdateKind.GUI) {
average = t.getDouble(name)
} else {
values.clear()
if (t.hasKey(name)) {
val i = new DataInputStream(new ByteArrayInputStream(t.getByteArray(name)))
val len = i.readByte()
values ++= (for (x <- 0 until Math.min(len, size)) yield i.readDouble())
i.close()
}
}
}
}
@@ -0,0 +1,39 @@
/*
* Copyright (c) bdew, 2013 - 2015
* 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.DataSlotOption
import net.bdew.lib.data.base.{TileDataSlots, UpdateKind}
import net.minecraft.nbt.NBTTagCompound
case class DataSlotResourceKindOption(name: String, parent: TileDataSlots) extends DataSlotOption[ResourceKind] {
setUpdate(UpdateKind.SAVE, UpdateKind.GUI)
override def save(t: NBTTagCompound, kind: UpdateKind.Value) {
value map { r =>
t.setTag(name,
Misc.applyMutator(new NBTTagCompound) { tag =>
r.helperObject.saveToNBT(tag, r)
tag.setString("kind", r.helperObject.id)
}
)
}
}
override def load(t: NBTTagCompound, kind: UpdateKind.Value) {
if (t.hasKey(name)) {
val tag = t.getCompoundTag(name)
value = if (tag.hasKey("kind")) {
ResourceManager.resourceHelpers.get(tag.getString("kind")).flatMap(_.loadFromNBT(tag))
} else None
} else unset()
}
}