Added serialization helpers for ItemStack and NBTTagCompound

This commit is contained in:
bdew committed 2014-10-28 10:04:57 +02:00
1 parent 8bbab862ab
commit 5854625700
4 files changed
+93 -1

No files matched your search

+1 -1
View File
@@ -21,7 +21,7 @@ buildscript {
apply plugin: 'scala'
apply plugin: 'forge'
version = "1.5.0"
version = "1.5.1"
if (project.hasProperty('buildnum')) {
project.version = project.version + '.' + project.buildnum
@@ -0,0 +1,42 @@
/*
* 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.network
import java.io.{ObjectInputStream, ObjectOutputStream}
import net.minecraft.item.{Item, ItemStack}
class ItemStackSerialize(var stack: ItemStack) extends Serializable {
private def writeObject(out: ObjectOutputStream) {
out.writeShort(Item.getIdFromItem(stack.getItem))
out.writeByte(stack.stackSize)
out.writeShort(stack.getItemDamage)
if (stack.hasTagCompound)
out.writeObject(NBTHelper.toBytes(stack.getTagCompound))
else
out.writeObject(null)
}
private def readObject(in: ObjectInputStream) {
val id = in.readShort()
val sz = in.readByte()
val dmg = in.readShort()
stack = new ItemStack(Item.getItemById(id), sz, dmg)
val obj = in.readObject().asInstanceOf[Array[Byte]]
if (obj != null)
stack.setTagCompound(NBTHelper.fromBytes(obj))
}
}
object ItemStackSerialize {
implicit def ser2content(v: ItemStackSerialize) = v.stack
implicit def content2ser(v: ItemStack) = new ItemStackSerialize(v)
}
+20
View File
@@ -0,0 +1,20 @@
/*
* 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.network
import net.minecraft.nbt.{CompressedStreamTools, NBTSizeTracker, NBTTagCompound}
object NBTHelper {
def toBytes(t: NBTTagCompound): Array[Byte] =
CompressedStreamTools.compress(t)
def fromBytes(v: Array[Byte], maxSize: Int = 1024 * 1024) =
CompressedStreamTools.func_152457_a(v, new NBTSizeTracker(maxSize))
}
@@ -0,0 +1,30 @@
/*
* 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.network
import java.io.{ObjectInputStream, ObjectOutputStream}
import net.minecraft.nbt.NBTTagCompound
class NBTTagCompoundSerialize(var tag: NBTTagCompound = new NBTTagCompound) extends Serializable {
private def writeObject(out: ObjectOutputStream) {
out.writeObject(NBTHelper.toBytes(tag))
}
private def readObject(in: ObjectInputStream) {
tag = NBTHelper.fromBytes(in.readObject().asInstanceOf[Array[Byte]])
}
}
object NBTTagCompoundSerialize {
implicit def ser2content(v: NBTTagCompoundSerialize) = v.tag
implicit def content2ser(v: NBTTagCompound) = new NBTTagCompoundSerialize(v)
}