diff --git a/build.gradle b/build.gradle index 0dbde05..0bb1885 100644 --- a/build.gradle +++ b/build.gradle @@ -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 diff --git a/src/net/bdew/lib/network/ItemStackSerialize.scala b/src/net/bdew/lib/network/ItemStackSerialize.scala new file mode 100644 index 0000000..a9ce626 --- /dev/null +++ b/src/net/bdew/lib/network/ItemStackSerialize.scala @@ -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) +} + diff --git a/src/net/bdew/lib/network/NBTHelper.scala b/src/net/bdew/lib/network/NBTHelper.scala new file mode 100644 index 0000000..1c18659 --- /dev/null +++ b/src/net/bdew/lib/network/NBTHelper.scala @@ -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)) +} diff --git a/src/net/bdew/lib/network/NBTTagCompoundSerialize.scala b/src/net/bdew/lib/network/NBTTagCompoundSerialize.scala new file mode 100644 index 0000000..b131fb6 --- /dev/null +++ b/src/net/bdew/lib/network/NBTTagCompoundSerialize.scala @@ -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) +}