anvil is a simple library for reading and writing minecraft anvil files.
go get github.com/FireworkMC/anvil
a, err := anvil.Open("/path/to/anvil/dir")
if err != nil{
// handle error
}
var buffer bytes.Buffer
_, err = a.ReadTo(chunkX, chunkZ, &buffer)
// do stuff with the buffer
err = a.Write(chunkX, chunkZ, buffer.Bytes())
err = a.Close()
The anvil.Anvil
returned by Open
contains a cache of opened anvil files.
It is recommended to use Open
instead of OpenFile
since opening anvil files is an expensive operation.
f, err := anvil.OpenFile("/path/to/anvil/file")
if err != nil{
// handle error
}
var buffer bytes.Buffer
// relative coordinates of the chunk data must be used
// If the chunk exists at chunkX, chunkZ: chunkX % 32, chunkZ % 32 should be used.
_, err = f.ReadTo(chunkX%32, chunkZ%32, &buffer)
// do stuff with buffer
_, err = f.Write(chunkX%32, chunkZ%32, buffer.Bytes())