Ejemplo n.º 1
0
func (inv *Inventory) MarshalNbt(tag *nbt.Compound) (err error) {
	occupiedSlots := 0
	for i := range inv.slots {
		if inv.slots[i].Count > 0 {
			occupiedSlots++
		}
	}

	itemList := &nbt.List{nbt.TagCompound, make([]nbt.ITag, 0, occupiedSlots)}
	for i := range inv.slots {
		slot := &inv.slots[i]
		if slot.Count > 0 {
			slotTag := nbt.NewCompound()
			slotTag.Set("Slot", &nbt.Byte{int8(i)})
			if err = slot.MarshalNbt(slotTag); err != nil {
				return
			}
			itemList.Value = append(itemList.Value, slotTag)
		}
	}

	tag.Set("Items", itemList)

	return nil
}
Ejemplo n.º 2
0
func (item *Item) UnmarshalNbt(tag *nbt.Compound) (err os.Error) {
	if err = item.PointObject.UnmarshalNbt(tag); err != nil {
		return
	}

	itemInfo, ok := tag.Lookup("Item").(*nbt.Compound)
	if !ok {
		return os.NewError("bad item data")
	}

	// Grab the basic item data
	id, idOk := itemInfo.Lookup("id").(*nbt.Short)
	count, countOk := itemInfo.Lookup("Count").(*nbt.Byte)
	data, dataOk := itemInfo.Lookup("Damage").(*nbt.Short)
	if !idOk || !countOk || !dataOk {
		return os.NewError("bad item data")
	}

	item.Slot = Slot{
		ItemTypeId: ItemTypeId(id.Value),
		Count:      ItemCount(count.Value),
		Data:       ItemData(data.Value),
	}

	return nil
}
Ejemplo n.º 3
0
func (inv *Inventory) UnmarshalNbt(tag *nbt.Compound) (err error) {
	itemList, ok := tag.Lookup("Items").(*nbt.List)
	if !ok {
		return errors.New("bad inventory - not a list")
	}

	for _, slotTagITag := range itemList.Value {
		slotTag, ok := slotTagITag.(*nbt.Compound)
		if !ok {
			return errors.New("inventory slot not a compound")
		}

		var slotIdTag *nbt.Byte
		if slotIdTag, ok = slotTag.Lookup("Slot").(*nbt.Byte); !ok {
			return errors.New("Slot ID not a byte")
		}
		slotId := SlotId(slotIdTag.Value)

		if err = inv.SlotUnmarshalNbt(slotTag, slotId); err != nil {
			return
		}
	}

	return nil
}
Ejemplo n.º 4
0
func (recordPlayer *recordPlayerTileEntity) MarshalNbt(tag *nbt.Compound) (err os.Error) {
	if err = recordPlayer.tileEntity.MarshalNbt(tag); err != nil {
		return
	}

	tag.Set("id", &nbt.String{"RecordPlayer"})
	tag.Set("Record", &nbt.Int{recordPlayer.record})

	return nil
}
Ejemplo n.º 5
0
func (music *musicTileEntity) MarshalNbt(tag *nbt.Compound) (err os.Error) {
	if err = music.tileEntity.MarshalNbt(tag); err != nil {
		return
	}

	tag.Set("id", &nbt.String{"Music"})
	tag.Set("note", &nbt.Byte{int8(music.note)})

	return nil
}
Ejemplo n.º 6
0
func (item *Item) MarshalNbt(tag *nbt.Compound) (err os.Error) {
	if err = item.PointObject.MarshalNbt(tag); err != nil {
		return
	}
	tag.Set("id", &nbt.String{"Item"})
	tag.Set("Item", &nbt.Compound{map[string]nbt.ITag{
		"id":     &nbt.Short{int16(item.ItemTypeId)},
		"Count":  &nbt.Byte{int8(item.Count)},
		"Damage": &nbt.Short{int16(item.Data)},
	}})
	return nil
}
Ejemplo n.º 7
0
func (object *Object) MarshalNbt(tag nbt.Compound) (err error) {
	objTypeName, ok := ObjNameByType[object.ObjTypeId]
	if !ok {
		return errors.New("unknown object type")
	}
	if err = object.PointObject.MarshalNbt(tag); err != nil {
		return
	}
	tag.Set("id", &nbt.String{objTypeName})
	// TODO unknown fields
	return
}
Ejemplo n.º 8
0
func (recordPlayer *recordPlayerTileEntity) UnmarshalNbt(tag *nbt.Compound) (err os.Error) {
	if err = recordPlayer.tileEntity.UnmarshalNbt(tag); err != nil {
		return
	}

	if recordTag, ok := tag.Lookup("Record").(*nbt.Int); !ok {
		return os.NewError("missing or incorrect type for RecordPlayer Record")
	} else {
		recordPlayer.record = recordTag.Value
	}

	return nil
}
Ejemplo n.º 9
0
func (music *musicTileEntity) UnmarshalNbt(tag *nbt.Compound) (err os.Error) {
	if err = music.tileEntity.UnmarshalNbt(tag); err != nil {
		return
	}

	if noteTag, ok := tag.Lookup("note").(*nbt.Byte); !ok {
		return os.NewError("missing or incorrect type for Music note")
	} else {
		music.note = NotePitch(noteTag.Value)
	}

	return nil
}
Ejemplo n.º 10
0
func (sign *signTileEntity) UnmarshalNbt(tag *nbt.Compound) (err error) {
	if err = sign.tileEntity.UnmarshalNbt(tag); err != nil {
		return
	}

	var textTags [4]*nbt.String
	var ok bool

	if textTags[0], ok = tag.Lookup("Text1").(*nbt.String); !ok {
		return errors.New("tile entity sign missing or bad type Text1")
	}
	if textTags[1], ok = tag.Lookup("Text2").(*nbt.String); !ok {
		return errors.New("tile entity sign missing or bad type Text2")
	}
	if textTags[2], ok = tag.Lookup("Text3").(*nbt.String); !ok {
		return errors.New("tile entity sign missing or bad type Text3")
	}
	if textTags[3], ok = tag.Lookup("Text4").(*nbt.String); !ok {
		return errors.New("tile entity sign missing or bad type Text4")
	}

	for i, textTag := range textTags {
		sign.text[i] = textTag.Value
	}

	return nil
}
Ejemplo n.º 11
0
func (w *PlayerInventory) MarshalNbt(tag *nbt.Compound) (err os.Error) {
	slots := make([]nbt.ITag, 0, 0)

	// Add the holding inventory
	for i := 0; i < int(w.holding.NumSlots()); i++ {
		slot := w.holding.Slot(SlotId(i))
		if !slot.IsEmpty() {
			slotTag := nbt.NewCompound()
			slotTag.Set("Slot", &nbt.Byte{int8(i)})
			if err = slot.MarshalNbt(slotTag); err != nil {
				return
			}
			slots = append(slots, slotTag)
		}
	}

	// Add the main inventory
	for i := 0; i < int(w.main.NumSlots()); i++ {
		slot := w.main.Slot(SlotId(i))
		if !slot.IsEmpty() {
			slotTag := nbt.NewCompound()
			slotTag.Set("Slot", &nbt.Byte{int8(i + playerInvHoldingNum)})
			if err = slot.MarshalNbt(slotTag); err != nil {
				return
			}
			slots = append(slots, slotTag)
		}
	}

	// Add the armor inventory
	for i := 0; i < int(w.armor.NumSlots()); i++ {
		slot := w.armor.Slot(SlotId(i))
		if !slot.IsEmpty() {
			slotTag := nbt.NewCompound()
			slotTag.Set("Slot", &nbt.Byte{int8(i + 100)})
			if err = slot.MarshalNbt(slotTag); err != nil {
				return
			}
			slots = append(slots, slotTag)
		}
	}

	tag.Set("Inventory", &nbt.List{nbt.TagCompound, slots})

	return nil
}
Ejemplo n.º 12
0
func (obj *PointObject) UnmarshalNbt(tag *nbt.Compound) (err error) {
	// Position within the chunk
	if obj.position, err = nbtutil.ReadAbsXyz(tag, "Pos"); err != nil {
		return
	}
	obj.LastSentPosition = *obj.position.ToAbsIntXyz()

	// Motion
	if obj.velocity, err = nbtutil.ReadAbsVelocity(tag, "Motion"); err != nil {
		return
	}
	obj.LastSentVelocity = *obj.velocity.ToVelocity()

	if onGround, ok := tag.Lookup("OnGround").(*nbt.Byte); ok {
		obj.onGround = onGround.Value != 0
	}

	return nil
}
Ejemplo n.º 13
0
func (mobSpawner *mobSpawnerTileEntity) UnmarshalNbt(tag *nbt.Compound) (err os.Error) {
	if err = mobSpawner.tileEntity.UnmarshalNbt(tag); err != nil {
		return
	}

	if entityIdTag, ok := tag.Lookup("EntityId").(*nbt.String); !ok {
		return os.NewError("missing or incorrect type for MobSpawner EntityId")
	} else {
		mobSpawner.entityMobType = entityIdTag.Value
	}

	if delayTag, ok := tag.Lookup("Delay").(*nbt.Short); !ok {
		return os.NewError("missing or incorrect type for MobSpawner Delay")
	} else {
		mobSpawner.delay = types.Ticks(delayTag.Value)
	}

	return nil
}
Ejemplo n.º 14
0
func (object *Object) UnmarshalNbt(tag nbt.Compound) (err error) {
	if err = object.PointObject.UnmarshalNbt(tag); err != nil {
		return
	}

	var typeName string
	if entityObjectId, ok := tag.Lookup("id").(*nbt.String); !ok {
		return errors.New("missing object type id")
	} else {
		typeName = entityObjectId.Value
	}

	var ok bool
	if object.ObjTypeId, ok = ObjTypeByName[typeName]; !ok {
		return errors.New("unknown object type id")
	}

	// TODO load orientation

	return
}
Ejemplo n.º 15
0
func (inv *FurnaceInventory) UnmarshalNbt(tag *nbt.Compound) (err os.Error) {
	if err = inv.Inventory.UnmarshalNbt(tag); err != nil {
		return
	}

	if burnTimeTag, ok := tag.Lookup("BurnTime").(*nbt.Short); !ok {
		return os.NewError("Bad or missing BurnTime tag in Furnace NBT")
	} else {
		inv.burnTime = types.Ticks(burnTimeTag.Value)

		// We don't know what the burnTimeMax was, as it is not stored, so taking
		// BurnTime for this value as well.
		inv.burnTimeMax = inv.burnTime
	}

	if cookTimeTag, ok := tag.Lookup("CookTime").(*nbt.Short); !ok {
		return os.NewError("Bad or missing CookTime tag in Furnace NBT")
	} else {
		inv.cookTime = types.Ticks(cookTimeTag.Value)
	}

	return nil
}
Ejemplo n.º 16
0
func (mobSpawner *mobSpawnerTileEntity) MarshalNbt(tag *nbt.Compound) (err os.Error) {
	if err = mobSpawner.tileEntity.MarshalNbt(tag); err != nil {
		return
	}

	tag.Set("id", &nbt.String{"MobSpawner"})
	tag.Set("EntityId", &nbt.String{mobSpawner.entityMobType})
	tag.Set("Delay", &nbt.Short{int16(mobSpawner.delay)})

	return nil
}
Ejemplo n.º 17
0
func (sign *signTileEntity) MarshalNbt(tag *nbt.Compound) (err error) {
	if err = sign.tileEntity.MarshalNbt(tag); err != nil {
		return
	}

	tag.Set("id", &nbt.String{"Sign"})
	tag.Set("Text1", &nbt.String{sign.text[0]})
	tag.Set("Text2", &nbt.String{sign.text[1]})
	tag.Set("Text3", &nbt.String{sign.text[2]})
	tag.Set("Text4", &nbt.String{sign.text[3]})

	return nil
}
Ejemplo n.º 18
0
func (s *Slot) UnmarshalNbt(tag nbt.Compound) (err error) {
	var ok bool
	var idTag, damageTag *nbt.Short
	var countTag *nbt.Byte

	if idTag, ok = tag.Lookup("id").(*nbt.Short); !ok {
		return errors.New("id tag not Short")
	}
	if countTag, ok = tag.Lookup("Count").(*nbt.Byte); !ok {
		return errors.New("Count tag not Byte")
	}
	if damageTag, ok = tag.Lookup("Damage").(*nbt.Short); !ok {
		return errors.New("Damage tag not Short")
	}

	s.ItemTypeId = ItemTypeId(idTag.Value)
	s.Count = ItemCount(countTag.Value)
	s.Data = ItemData(damageTag.Value)

	return
}
Ejemplo n.º 19
0
func (mob *Mob) MarshalNbt(tag nbt.Compound) (err error) {
	mobTypeName, ok := MobNameByType[mob.mobType]
	if !ok {
		return errors.New("unknown mob type")
	}
	if err = mob.PointObject.MarshalNbt(tag); err != nil {
		return
	}
	tag.Set("id", &nbt.String{mobTypeName})
	tag.Set("Rotation", &nbt.List{nbt.TagFloat, []nbt.ITag{
		&nbt.Float{float32(mob.look.Yaw)},
		&nbt.Float{float32(mob.look.Pitch)},
	}})
	// TODO
	tag.Set("Air", &nbt.Short{0})
	tag.Set("AttackTime", &nbt.Short{0})
	tag.Set("DeathTime", &nbt.Short{0})
	tag.Set("FallDistance", &nbt.Float{0})
	tag.Set("Fire", &nbt.Short{0})
	tag.Set("Health", &nbt.Short{0})
	tag.Set("HurtTime", &nbt.Short{0})
	return nil
}
Ejemplo n.º 20
0
func (inv *CraftingInventory) MarshalNbt(tag nbt.Compound) (err error) {
	tag.Set("id", &nbt.String{"Workbench"})
	return inv.Inventory.MarshalNbt(tag)
}
Ejemplo n.º 21
0
func (inv *ChestInventory) MarshalNbt(tag *nbt.Compound) (err error) {
	tag.Set("id", &nbt.String{"Furnace"})
	return inv.Inventory.MarshalNbt(tag)
}
Ejemplo n.º 22
0
func (inv *DispenserInventory) MarshalNbt(tag *nbt.Compound) (err os.Error) {
	tag.Set("id", &nbt.String{"Trap"})
	return inv.Inventory.MarshalNbt(tag)
}
Ejemplo n.º 23
0
// UnmarshalNbt unpacks the player data from their persistantly stored NBT
// data. It must only be called before Player.Run().
func (player *Player) UnmarshalNbt(tag *nbt.Compound) (err error) {
	if player.position, err = nbtutil.ReadAbsXyz(tag, "Pos"); err != nil {
		return
	}

	if player.look, err = nbtutil.ReadLookDegrees(tag, "Rotation"); err != nil {
		return
	}

	health, err := nbtutil.ReadShort(tag, "Health")
	if err != nil {
		return
	}
	player.health = Health(health)

	if err = player.inventory.UnmarshalNbt(tag.Lookup("Inventory")); err != nil {
		return
	}

	if player.onGround, err = nbtutil.ReadByte(tag, "OnGround"); err != nil {
		return
	}

	if player.dimension, err = nbtutil.ReadInt(tag, "Dimension"); err != nil {
		return
	}

	if player.sleeping, err = nbtutil.ReadByte(tag, "Sleeping"); err != nil {
		return
	}

	if player.fallDistance, err = nbtutil.ReadFloat(tag, "FallDistance"); err != nil {
		return
	}

	if player.sleepTimer, err = nbtutil.ReadShort(tag, "SleepTimer"); err != nil {
		return
	}

	if player.attackTime, err = nbtutil.ReadShort(tag, "AttackTime"); err != nil {
		return
	}

	if player.deathTime, err = nbtutil.ReadShort(tag, "DeathTime"); err != nil {
		return
	}

	if player.motion, err = nbtutil.ReadAbsVelocity(tag, "Motion"); err != nil {
		return
	}

	if player.hurtTime, err = nbtutil.ReadShort(tag, "HurtTime"); err != nil {
		return
	}

	if player.air, err = nbtutil.ReadShort(tag, "Air"); err != nil {
		return
	}

	if player.fire, err = nbtutil.ReadShort(tag, "Fire"); err != nil {
		return
	}

	return nil
}
Ejemplo n.º 24
0
func (inv *FurnaceInventory) MarshalNbt(tag *nbt.Compound) (err os.Error) {
	tag.Set("id", &nbt.String{"Furnace"})
	tag.Set("BurnTime", &nbt.Short{int16(inv.burnTime)})
	tag.Set("CookTime", &nbt.Short{int16(inv.cookTime)})
	return inv.Inventory.MarshalNbt(tag)
}
Ejemplo n.º 25
0
// MarshalNbt packs the player data into a nbt.Compound so it can be written to
// persistant storage.
func (player *Player) MarshalNbt(tag *nbt.Compound) (err error) {
	if err = player.inventory.MarshalNbt(tag); err != nil {
		return
	}

	tag.Set("OnGround", &nbt.Byte{player.onGround})
	tag.Set("Dimension", &nbt.Int{player.dimension})
	tag.Set("Sleeping", &nbt.Byte{player.sleeping})
	tag.Set("FallDistance", &nbt.Float{player.fallDistance})
	tag.Set("SleepTimer", &nbt.Short{player.sleepTimer})
	tag.Set("AttackTime", &nbt.Short{player.attackTime})
	tag.Set("DeathTime", &nbt.Short{player.deathTime})
	tag.Set("Motion", &nbt.List{nbt.TagDouble, []nbt.ITag{
		&nbt.Double{float64(player.motion.X)},
		&nbt.Double{float64(player.motion.Y)},
		&nbt.Double{float64(player.motion.Z)},
	}})
	tag.Set("HurtTime", &nbt.Short{player.hurtTime})
	tag.Set("Air", &nbt.Short{player.air})
	tag.Set("Rotation", &nbt.List{nbt.TagFloat, []nbt.ITag{
		&nbt.Float{float32(player.look.Yaw)},
		&nbt.Float{float32(player.look.Pitch)},
	}})
	tag.Set("Pos", &nbt.List{nbt.TagDouble, []nbt.ITag{
		&nbt.Double{float64(player.position.X)},
		&nbt.Double{float64(player.position.Y)},
		&nbt.Double{float64(player.position.Z)},
	}})
	tag.Set("Fire", &nbt.Short{player.fire})
	tag.Set("Health", &nbt.Short{int16(player.health)})

	return nil
}
Ejemplo n.º 26
0
func (mob *Mob) UnmarshalNbt(tag nbt.Compound) (err error) {
	if err = mob.PointObject.UnmarshalNbt(tag); err != nil {
		return
	}

	if mob.look, err = nbtutil.ReadLookDegrees(tag, "Rotation"); err != nil {
		return
	}

	// TODO
	_ = tag.Lookup("Air").(*nbt.Short).Value
	_ = tag.Lookup("AttackTime").(*nbt.Short).Value
	_ = tag.Lookup("DeathTime").(*nbt.Short).Value
	_ = tag.Lookup("FallDistance").(*nbt.Float).Value
	_ = tag.Lookup("Fire").(*nbt.Short).Value
	_ = tag.Lookup("Health").(*nbt.Short).Value
	_ = tag.Lookup("HurtTime").(*nbt.Short).Value

	return nil
}
Ejemplo n.º 27
0
func (s *Slot) MarshalNbt(tag nbt.Compound) (err error) {
	tag.Set("id", &nbt.Short{int16(s.ItemTypeId)})
	tag.Set("Count", &nbt.Byte{int8(s.Count)})
	tag.Set("Damage", &nbt.Short{int16(s.Data)})
	return nil
}