Example #1
0
func cmdNbt(args []string) (err error) {
	if len(args) != 1 {
		os.Stderr.WriteString("usage: " + os.Args[0] + " nbt <NBT file path>\n")
		return
	}

	file, err := os.Open(args[0])
	if err != nil {
		return
	}
	defer file.Close()

	gzipReader, err := gzip.NewReader(file)
	if err != nil {
		return
	}
	defer gzipReader.Close()

	namedTag, err := nbt.Read(gzipReader)
	if err != nil {
		return
	}

	displayNbt(1, namedTag)

	return
}
// Load a chunk from its NBT representation
func newNbtChunkReader(reader io.Reader) (r *nbtChunkReader, err error) {
	chunkTag, err := nbt.Read(reader)
	if err != nil {
		return
	}

	r = &nbtChunkReader{
		chunkTag: chunkTag,
	}

	return
}
Example #3
0
func (is *ItemSlot) MinecraftUnmarshal(reader io.Reader, ps *PacketSerializer) error {
	typeIdUint16, err := ps.readUint16(reader)
	if err != nil {
		return err
	}
	is.ItemTypeId = ItemTypeId(typeIdUint16)

	if is.ItemTypeId == -1 {
		is.Count = 0
		is.Data = 0
	} else {
		countUint8, err := ps.readUint8(reader)
		if err != nil {
			return err
		}
		dataUint16, err := ps.readUint16(reader)
		if err != nil {
			return err
		}

		is.Count = ItemCount(countUint8)
		is.Data = ItemData(dataUint16)

		if _, requiresNbt := nbtItemIds[is.ItemTypeId]; requiresNbt {
			// Read NBT data.
			lUint16, err := ps.readUint16(reader)
			if err != nil {
				return err
			}
			lInt16 := int16(lUint16)
			if lInt16 < 0 {
				return nil
			}

			zReader, err := gzip.NewReader(&io.LimitedReader{reader, int64(lInt16)})
			if err != nil {
				return err
			}

			err = zReader.Close()
			if err != nil {
				return err
			}

			is.Nbt, err = nbt.Read(zReader)
			if err != nil {
				return err
			}
		}
	}
	return nil
}
Example #4
0
func loadLevelData(worldPath string) (levelData nbt.ITag, err error) {
	filename := path.Join(worldPath, "level.dat")
	file, err := os.Open(filename)
	if err != nil {
		return
	}
	defer file.Close()

	gzipReader, err := gzip.NewReader(file)
	if err != nil {
		return
	}
	defer gzipReader.Close()

	levelData, err = nbt.Read(gzipReader)

	return
}
Example #5
0
// Load a chunk from its NBT representation
func loadChunk(reader io.Reader) (chunk *Chunk, err os.Error) {
	level, err := nbt.Read(reader)
	if err != nil {
		return
	}

	chunk = &Chunk{
		X:          ChunkCoord(level.Lookup("/Level/xPos").(*nbt.Int).Value),
		Z:          ChunkCoord(level.Lookup("/Level/zPos").(*nbt.Int).Value),
		Blocks:     level.Lookup("/Level/Blocks").(*nbt.ByteArray).Value,
		BlockData:  level.Lookup("/Level/Data").(*nbt.ByteArray).Value,
		SkyLight:   level.Lookup("/Level/SkyLight").(*nbt.ByteArray).Value,
		BlockLight: level.Lookup("/Level/BlockLight").(*nbt.ByteArray).Value,
		HeightMap:  level.Lookup("/Level/HeightMap").(*nbt.ByteArray).Value,
		players:    make(map[EntityID]*Player),
	}
	return
}
Example #6
0
func loadStartPosition(worldPath string) {
	file, err := os.Open(path.Join(worldPath, "level.dat"), os.O_RDONLY, 0)
	if err != nil {
		log.Exit("loadStartPosition: ", err.String())
	}

	level, err := nbt.Read(file)
	file.Close()
	if err != nil {
		log.Exit("loadStartPosition: ", err.String())
	}

	pos := level.Lookup("/Data/Player/Pos")
	StartPosition = XYZ{
		pos.(*nbt.List).Value[0].(*nbt.Double).Value,
		pos.(*nbt.List).Value[1].(*nbt.Double).Value,
		pos.(*nbt.List).Value[2].(*nbt.Double).Value,
	}
}
Example #7
0
func (world *WorldStore) PlayerData(user string) (playerData nbt.Compound, err error) {
	file, err := os.Open(path.Join(world.WorldPath, "players", user+".dat"))
	if err != nil {
		if os.IsNotExist(err) {
			// Player data simply doesn't exist. Not an error, playerData = nil is
			// the result.
			return nil, nil
		}
		return
	}
	defer file.Close()

	gzipReader, err := gzip.NewReader(file)
	if err != nil {
		return
	}
	defer gzipReader.Close()

	playerData, err = nbt.Read(gzipReader)

	return
}
Example #8
0
func (world *WorldStore) PlayerData(user string) (playerData nbt.ITag, err os.Error) {
	file, err := os.Open(path.Join(world.WorldPath, "players", user+".dat"))
	if err != nil {
		if errno, ok := util.Errno(err); ok && errno == os.ENOENT {
			// Player data simply doesn't exist. Not an error, playerData = nil is
			// the result.
			return nil, nil
		}
		return
	}
	defer file.Close()

	gzipReader, err := gzip.NewReader(file)
	if err != nil {
		return
	}
	defer gzipReader.Close()

	playerData, err = nbt.Read(gzipReader)

	return
}