示例#1
0
func (r *nbtChunkReader) Entities() (entities []gamerules.INonPlayerEntity) {
	entityListTag, ok := r.chunkTag.Lookup("Level/Entities").(*nbt.List)
	if !ok {
		return
	}

	entities = make([]gamerules.INonPlayerEntity, 0, len(entities))

	for _, entityTag := range entityListTag.Value {
		entityObjectId, ok := entityTag.Lookup("id").(*nbt.String)

		if !ok {
			log.Printf("missing or bad entity type ID in NBT: %s", entityObjectId)
		} else {
			if entity := gamerules.NewEntityByTypeName(entityObjectId.Value); entity == nil {
				log.Printf("Found unhandled entity type: %s", entityObjectId.Value)
			} else {
				if err := entity.ReadNbt(entityTag); err != nil {
					log.Printf("Error reading entity NBT: %s", err)
				} else {
					entities = append(entities, entity)
				}
			}
		}
	}

	return
}
func (r *nbtChunkReader) Entities() (entities []gamerules.INonPlayerEntity) {
	entityListTag, ok := r.chunkTag.Lookup("Level/Entities").(*nbt.List)
	if !ok {
		return
	}

	entities = make([]gamerules.INonPlayerEntity, 0, len(entityListTag.Value))
	for _, tag := range entityListTag.Value {
		compound, ok := tag.(nbt.Compound)
		if !ok {
			log.Printf("Found non-compound in entities list: %T", tag)
			continue
		}

		entityObjectId, ok := compound.Lookup("id").(*nbt.String)
		if !ok {
			log.Printf("Missing or bad entity type ID in NBT: %s", entityObjectId)
			continue
		}

		entity := gamerules.NewEntityByTypeName(entityObjectId.Value)
		if entity == nil {
			log.Printf("Found unhandled entity type: %s", entityObjectId.Value)
			continue
		}

		err := entity.UnmarshalNbt(compound)
		if err != nil {
			log.Printf("Error unmarshalling entity NBT: %s", err)
			continue
		}

		entities = append(entities, entity)
	}

	return
}