mapparser

package module
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2022 License: MIT Imports: 10 Imported by: 0

README

Minetest map parser library for go

Coverage Status

Features

  • Extracts metadata/inventories
  • NodeID/Param1/Param2 handling
  • Supports zlib compressed mapblocks
  • zstd compressed mapblocks (minetest 5.5+)

Example

// read mapblock data from db/file/somewhere else
data, err := ioutil.ReadFile("mapblock.bin")
if err != nil {
    panic(err)
}

// parse
mapblock, err := Parse(data)
if err != nil {
    panic(err)
}

// mapblock version
fmt.Printf("Version: %d", mapblock.Version)

// nodes
fmt.Printf("%s", mapblock.GetNodeName(10,0,2)) // node-name
fmt.Printf("%s", mapblock.GetNodeId(10,0,2)) //raw nodeid
fmt.Printf("%s", mapblock.GetParam2(10,0,2)) //param2

// node-id mapping
for id, name := range mapblock.BlockMapping {
    fmt.Printf("%d = %s", id, name)
}

// inventories
invMap := mapblock.Metadata.GetInventoryMapAtPos(10,0,0)
mainInv := invMap["main"]
for _, item := range mainInv {
    fmt.Printf("%s %d", item.Name, item.Count)
}

// metadata
md := mapblock.Metadata.GetMetadata(10,0,1)
for key, value := range md {
    fmt.Printf("%s = %s", key, value)
}

License

Code: MIT

Documentation

Index

Constants

View Source
const (
	INVENTORY_TERMINATOR = "EndInventory"
	INVENTORY_END        = "EndInventoryList"
	INVENTORY_START      = "List"
)
View Source
const MapDataSize = 16384

Variables

View Source
var ErrMapblockVersion = errors.New("mapblock version unsupported")
View Source
var ErrNoData = errors.New("no data")

Functions

func FromNodePos added in v0.1.1

func FromNodePos(pos int) (int, int, int)

converts the index back to a vector

func GetNodePos added in v0.1.1

func GetNodePos(x, y, z int) int

converts a vector to an integer for indexing the internal mapblock positions

Types

type CountedReader

type CountedReader struct {
	Reader *bytes.Reader
	Count  int
}

func (*CountedReader) Read

func (r *CountedReader) Read(p []byte) (int, error)

func (*CountedReader) ReadByte

func (r *CountedReader) ReadByte() (byte, error)

type Inventory

type Inventory struct {
	Size  int     `json:"size"`
	Items []*Item `json:"items"`
}

func (*Inventory) IsEmpty

func (inv *Inventory) IsEmpty() bool

type Item

type Item struct {
	Name  string `json:"name"`
	Count int    `json:"count"`
	Wear  int    `json:"wear"`
}

func (*Item) IsEmpty

func (i *Item) IsEmpty() bool

type MapBlock

type MapBlock struct {
	Size         int            `json:"size"`
	Version      byte           `json:"version"`
	Underground  bool           `json:"underground"`
	Timestamp    uint32         `json:"timestamp"`
	Mapdata      *MapData       `json:"mapdata"`
	Metadata     *Metadata      `json:"metadata"`
	BlockMapping map[int]string `json:"blockmapping"`
}

func NewMapblock

func NewMapblock() *MapBlock

func Parse

func Parse(data []byte) (*MapBlock, error)

func (*MapBlock) GetNodeId

func (mb *MapBlock) GetNodeId(x, y, z int) int

func (*MapBlock) GetNodeName

func (mb *MapBlock) GetNodeName(x, y, z int) string

func (*MapBlock) GetParam2

func (mb *MapBlock) GetParam2(x, y, z int) int

func (*MapBlock) IsEmpty

func (mb *MapBlock) IsEmpty() bool

returns true if the mapblock is empty (air-only)

type MapData

type MapData struct {
	ContentId []int `json:"contentid"`
	Param1    []int `json:"param1"`
	Param2    []int `json:"param2"`
}

type Metadata

type Metadata struct {
	Inventories map[int]map[string]*Inventory `json:"inventories"`
	Pairs       map[int]map[string]string     `json:"pairs"`
}

func NewMetadata

func NewMetadata() *Metadata

func (*Metadata) GetInventory

func (md *Metadata) GetInventory(pos int, name string) *Inventory

func (*Metadata) GetInventoryMap

func (md *Metadata) GetInventoryMap(pos int) map[string]*Inventory

func (*Metadata) GetInventoryMapAtPos

func (md *Metadata) GetInventoryMapAtPos(x, y, z int) map[string]*Inventory

func (*Metadata) GetMetadata

func (md *Metadata) GetMetadata(x, y, z int) map[string]string

func (*Metadata) GetPairsMap

func (md *Metadata) GetPairsMap(pos int) map[string]string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL